PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

Is it possible to highlight the text of a PdfTextField?
http://forum.pdfsharp.com/viewtopic.php?f=2&t=4553
Page 1 of 1

Author:  henryDev [ Thu Feb 22, 2024 10:33 pm ]
Post subject:  Is it possible to highlight the text of a PdfTextField?

Hello everyone,

I have been struggling to figure out how to highlight a textfield. My current code looks like this:

public static void highlightPDF(PdfAcroForm form, string fieldName)
{
PdfAcroField field = form.Fields[fieldName];
PdfTextField txtField;
if ((txtField = field as PdfTextField) != null)
{
txtField.BackColor = PdfSharp.Drawing.XColors.Yellow;
}
}

Nothing happens with this solution however, the variable is showing that it is the color yellow but it doesn't appear that way on my pdf file.

I have tried finding the position of a textfield then drawing a rectangle over it but that solution fell short since it only works for the first element on my pdf for some reason.
Any advice would be greatly appreciated.

Author:  henryDev [ Mon Mar 04, 2024 10:11 pm ]
Post subject:  Re: Is it possible to highlight the text of a PdfTextField?

If anyone runs into this problem, here is what I did:

PdfTextField txtField = (PdfTextField)field;
PdfArray fieldRect = (PdfArray)txtField.Elements.GetValue(PdfAnnotation.Keys.Rect);
PdfRectangle rect = txtField.Elements.GetRectangle(PdfAnnotation.Keys.Rect);
XRect rectangle = new XRect(rect.X1, rect.Y1, rect.Width, rect.Height);
XRect pdfPosition = gfx.Transformer.WorldToDefaultPage(rectangle);
XBrush backgroundBrush = new XSolidBrush(XColor.FromArgb(64, XColors.Yellow));
XBrush borderBrush = new XSolidBrush(XColor.FromArgb(128, XColors.Yellow));

gfx.DrawRectangle(backgroundBrush, pdfPosition);
gfx.DrawRectangle(borderBrush, pdfPosition);

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/