Hi,
I am using a license version of Spire.PDF
I would like to replace text on a specific region or rectangle.
I see PdfTextReplacer but this function can not check that the found text is inside the requested region.
Any tip?
Thanks in advance.
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("D:in.pdf");
//Define a rectangle
RectangleF rctg = new RectangleF(0, 0, 240, 240);
PdfPageBase pdfPageBase = doc.Pages[0];
PdfTextFinder finder = new PdfTextFinder(pdfPageBase);
finder.Options.Parameter = Spire.Pdf.Texts.TextFindParameter.WholeWord;
finder.Options.Area = rctg;
//Find text in the rectangle
List<PdfTextFragment> findouts = finder.Find("PDF");
String newText = "AAA";
// Creates a brush
PdfBrush brush = new PdfSolidBrush(Color.DarkBlue);
// Defines a font
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Regular));
RectangleF rec;
foreach (PdfTextFragment find in findouts)
{
// Gets the bound of the found text in page
rec = find.Bounds[0];
pdfPageBase.Canvas.DrawRectangle(PdfBrushes.White, rec);
// Draws new text as defined font and color
pdfPageBase.Canvas.DrawString(newText, font, brush, rec);
}
String result = "ReplaceAllSearchedText_out.pdf";
//Save the document
doc.SaveToFile(result);