Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Fri Mar 17, 2023 7:47 am

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.

igotz.urgoitia
 
Posts: 3
Joined: Fri Mar 17, 2023 7:44 am

Fri Mar 17, 2023 8:07 am

Just to be clear...

Could be nice to have something similar to:

PdfTextReplacer replacer = new PdfTextReplacer(new RectangleF(240, 100, 300, 100));

Replace a text, but only inside a region or rectangle....

igotz.urgoitia
 
Posts: 3
Joined: Fri Mar 17, 2023 7:44 am

Fri Mar 17, 2023 10:48 am

Hi,

Thank you for your message.
Sorry our PdfTextReplacer at present doesn't support basing on the RectangleF. However, you can try to use the following code to replace text on a specific region.If there is still any issue, please share your PDF file with us for further testing. You can upload it here or send it to us via email(support@e-iceblue.com).

Code: Select all
  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);


Best Regards,
Herman
E-iceblue support team
User avatar

Herman.Yan
 
Posts: 115
Joined: Wed Mar 08, 2023 2:00 am

Return to Spire.PDF