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

  • 本文关键字:注释 悬停 Pdfsharp c# pdfsharp
  • 更新时间 :
  • 英文 :


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

我想在我的PDF上注释一个矩形区域/高亮显示的文本。例如,我想将鼠标悬停在给定的矩形区域内的任何地方,并有注释弹出。

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

你能告诉我如何有注释出现当我悬停在给定矩形内的任何地方?

我看了PDFsharp和iTextSharp的文档,我只能看到textnotation的例子。

我尝试的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论坛的原作者致敬

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);
}

最新更新