I am working with Pdfsharp c# library to highlight the text and add annotations when mouse hover on the highlighted text.
I would like to annotate a rectangular area/highlighted text on my PDF. i.e. I would like to hover the mouse anywhere within the given rectangle area and have the annotation pop up.
It seems that a Text annotation only appears when hovering in the upper left corner of the annotation Rectangle. I tried using the combination of Rectangle and Text Annotation but that only seems to pop up when I'm hovering over the top left corner not the interior.
Can you tell me how to have the annotation appear when I hover anywhere within the given Rectangle?
I went through the documentation of PDFsharp and iTextSharp I can see only TextAnnotation example.
The PdfSquareAnnotation
that I tried draws a rectangle but, pops up the annotation text only on hover of the top-left corner
Below is my code to DrawRectangle and Add Annotation from PDfSharp.
Thank you in advance
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);
}
After trying multiple times and follow up with pdfsharp forum. I got the solution
Below is the Annotation Code. I believe it helps someone in future. Happy coding!
Kudos to the Original Author from 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);
}