用PDFTron (PdfNet)绘制矩形注释

本文关键字:注释 绘制 PDFTron PdfNet | 更新日期: 2023-09-27 17:52:12

我想让用户在pdf中创建一个矩形。用户可以在屏幕上拖动一个矩形。

在mouseup事件中,创建一个Rect对象并传递给方法AddRectAnnotationToPage(Rectangle Rect)

问题是矩形注释与用户拖动的矩形不同。当用户单击或滚动时,注释矩形获得正确的大小(等于作为参数传递的拖动矩形)

为什么它不能立即取到正确的尺寸?为什么每当我点击pdf中的随机点时,它都会调整到正确的大小?

    private void AddRectAnnotationToPage(Rectangle rect)
    {
        if (rect != null)
        {
            if (this.m_pageNumber < 0)
            {
                m_pageNumber = this.GetCurrentPage();
            }
            _cur_page = this.m_pageNumber;
            if (_cur_page < 0)
            {
                return;
            }

            double width = rect.Width;
            double height = rect.Height;
            double startX = rect.Left;
            double startY = rect.Bottom;
            double endX   = rect.Right;
            double endY   = rect.Top;
            double X1 = startX;
            double Y1 = startY;
            double X2 = endX;
            double Y2 = endY;
            ConvScreenPtToPagePt(ref X1, ref Y1, this.GetCurrentPage());
            ConvScreenPtToPagePt(ref X2, ref Y2, this.GetCurrentPage());
            Rect pos = new Rect(X1,Y1,X2,X2);
            Polygon poly = pdftron.PDF.Annots.Polygon.Create(m_PdfDocument.GetSDFDoc(), pos);
            pdftron.PDF.Point p = new pdftron.PDF.Point();
            p.x = X1; p.y = Y1;
            poly.SetVertex (0, p);
            p.x = X1; p.y = Y2;
            poly.SetVertex (1, p);
            p.x = X2; p.y = Y2;
            poly.SetVertex (2, p);
            p.x = X2; p.y = Y1;
            poly.SetVertex (3, p);

            PDFDoc doc = GetDoc();
            doc.Lock();
            pdftron.PDF.Page pag = doc.GetPage(_cur_page);
            Obj annots = pag.GetAnnots();
            if (annots == null)
            {
                // If there are no annotations, create a new annotation 
                // array for the page.
                annots = m_PdfDocument.CreateIndirectArray();
                pag.GetSDFObj().Put("Annots", annots);
            }
            pag.AnnotPushBack(poly);
            doc.Unlock();
        }
    }

用PDFTron (PdfNet)绘制矩形注释

修复了在设置顶点后调用refreshaappear()的问题