使用 PDFTron (PdfNet) 绘制矩形注释

Drawing a rectangle annotation with PDFTron (PdfNet)

我正在尝试让用户在 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();
        }
    }

已通过设置顶点后调用 Refreshappearance() 修复