Pdfsharp - 悬停在矩形上时无法弹出注释

Pdfsharp - Unable to pop up the annotation when hovering on the rectangle

我正在使用 Pdfsharp c# 库来突出显示文本并在鼠标悬停在突出显示的文本上时添加注释。

我想在我的 PDF 上注释矩形 area/highlighted 文本。即我想将鼠标悬停在给定矩形区域内的任意位置并弹出注释。

似乎只有将鼠标悬停在注释矩形的左上角时才会出现文本注释。我尝试使用矩形和文本注释的组合,但是当我将鼠标悬停在左上角而不是内部时,它似乎只会弹出。

你能告诉我当我悬停在给定矩形内的任何地方时如何显示注释吗?

我查看了 PDFsharp 和 iTextSharp 的文档,我只能看到 TextAnnotation 示例。

我试过的PdfSquareAnnotation画了一个矩形,但是,只在鼠标悬停在左上角时弹出注释文本

下面是我从 PDfSharp 绘制矩形和添加注释的代码。

提前致谢

 private static void DrawRectangle(string text, int padding, XGraphics graphics, double x, double y, double rectHeight, double rectWidth)
    {
        var font = new XFont("Courier New", 12, XFontStyle.Regular);

        var textMeasurements = graphics.MeasureString(text, font);


        var rectangle = new XRect(x, y, rectWidth, rectHeight);

        var pen = new XPen(XColors.Black, 1) { DashStyle = XDashStyle.Solid };

        var brush = new XSolidBrush(XColor.FromArgb(128, 168, 216, 239));

        graphics.DrawRectangle(brush, rectangle);
        graphics.DrawRectangle(pen, rectangle);
        graphics.DrawString(string.Empty, font, XBrushes.Black, rectangle, XStringFormats.Center);

        
    }
    private static void AddAnnotation(XGraphics graphics, PdfPage page, double x, double y, double height, double width)
    {
        PdfSharp.Drawing.XRect rect = graphics.Transformer.WorldToDefaultPage(new XRect(new XPoint(x, y), new XSize(width, height)));

        //    // Create a PDF text annotation
        PdfTextAnnotation textAnnot = new PdfTextAnnotation();
        textAnnot.Title = "This is the title";
        textAnnot.Subject = "This is the subject";
        textAnnot.Contents = "This is the contents of the annotation.\rThis is the 2nd line.";
        //textAnnot.Icon = PdfTextAnnotationIcon.Note;            
        textAnnot.Rectangle = new PdfSharp.Pdf.PdfRectangle(rect);
        // Add the annotation to the page
        page.Annotations.Add(textAnnot);
               
    }

尝试多次后,在 pdfsharp 论坛跟进。我得到了解决方案

下面是注释代码。我相信它可以帮助将来的人。编码愉快!

感谢来自 pdfsharp 的原作者 forum

 private static void AddAnnotation(XGraphics graphics, PdfPage page, double x, double y, double height, double width)
    {
        PdfSharp.Drawing.XRect rect = graphics.Transformer.WorldToDefaultPage(new XRect(new XPoint(x, y), new XSize(width, height)));
        XRect annotrect = new XRect(new XPoint(x, y), new XSize(width, height));
        // Create a PDF text annotation
        PdfTextAnnotation textAnnot = new PdfTextAnnotation();
        textAnnot.Title = "This is the title";
        textAnnot.Subject = "This is the subject";
        textAnnot.Contents = "This is the contents of the annotation.\rThis is the 2nd line.";            
        textAnnot.Rectangle = new PdfSharp.Pdf.PdfRectangle(rect);
        // The following lines of code did the trick
        textAnnot.Elements.Remove("/Subtype");
        textAnnot.Elements.Add("/Subtype", new PdfLiteral("/Square"));
        textAnnot.Elements.Add("/IC", new PdfLiteral("[0 0 0]"));
        textAnnot.Elements.Add("/CA", new PdfLiteral("0"));

        // Add the annotation to the page
        page.Annotations.Add(textAnnot);
    }