We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to replace the existing text in the PDF document?

Platform: WinForms |
Control: PdfViewer |
Published Date: March 2, 2018 |
Last Revised Date: March 5, 2018

We can replace the existing text in the PDF document by drawing the replacement text on the graphics using the following methods,

 

  1. Retrieve the bounds of the searched text using the FindText method. FindText method returns the bounds of the searched text across all the pages.

Dictionary<int, List<RectangleF>>  matchedTextbounds = new Dictionary<int, List<RectangleF>>();

 

//Loads the PDF document.

PdfLoadedDocument loadedDocument = new PdfLoadedDocument("../../Data/PDFDocument.pdf");

 

// Retrieves the bounds of the searched text using FindText method.

loadedDocument.FindText("Pdf", out  matchedTextbounds);

 

              

  1. Iterate through the retrieved bounds from FindText method and fill the rectangle with white color like below,

 

PdfLoadedPage page = null;

 

// Font which is used to draw the replacement text.

PdfFont font = new PdfTrueTypeFont(new Font("Arial", 11));

 

// Iterate throught the pages of the PDF document.       

for (int i =0;i< matchedTextbounds.Count;i++)

{

    page= (PdfLoadedPage)loadedDocument.Pages[i];

               

    // Iterate through the instance of the searched text in page.

    foreach (RectangleF rectangle in matchedTextbounds[i])

      {

         // Draws the white filled rectangle over the searched text.

         page.Graphics.DrawRectangle(PdfBrushes.White, rectangle);

 

         // Draws the replacedment text with respect to the font and bounds of the

            searched text.

         page.Graphics.DrawString("PDF", font, PdfBrushes.Black, rectangle.X-2,

         rectangle.Y-2);

       }

 }

 // Saves the PDF document to specified location.

 loadedDocument.Save("../../Data/PDFDocument_Replaced.pdf");

 

 

Remark:

  1.  Background color will not always be white color. 
  2. Font, style and size of the newly added text will not be same as the other text in the paragraph. 
  3. The replaced text should occupy the same space occupied by the older text, if it occupies more space it might lead to text overlap.

 

We have also created a sample with the above code example and the sample can be downloaded from the following link,

http://www.syncfusion.com/downloads/support/directtrac/general/ze/ExtractTextFromRegion284615895

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Andreas
Nov 26, 2020

Is it really not possible to simply update an existing text, preserving the original fonts and changing any background color? I am not familiar with the PDF format, but I was hoping that if the PDF was constructed with text elements these could be changed.

Reply
Roberto
Aug 23, 2021

I second that, this works as expected but I fear that if a background color is used other than white and no exact color match is present it will not have the desired result.

Also, another issue is that it just adds a new layer on top of old content, if you edit the PDF you can remove the new layer and see previous text, may not be a big problem but it should be replacing text not just drawing on top of it.

Roberto
Aug 23, 2021

Actually found a solution for this, at least for me on the layers issue, you can use a redaction and that way no previous content will show, it would be best to just edit but this works for me.

foreach (RectangleF rectangle in matchedTextbounds[i])

      {

// Redact old content
redaction = new PdfRedaction(rectangle, System.Drawing.Color.White);
         // Draws the white filled rectangle over the searched text.
         page.Graphics.DrawRectangle(PdfBrushes.White, rectangle);



         // Draws the replacedment text with respect to the font and bounds of the

            searched text.

         page.Graphics.DrawString("PDF", font, PdfBrushes.Black, rectangle.X-2,

         rectangle.Y-2);

       }
Deepak Gunasekaran [Syncfusion]
Sep 03, 2021

Hi Roberto,

As you have mentioned, currently redaction is the proper way to remove the text on the existing PDF document. We additionally provided the support to draw text on the redaction bounds. So, drawing text on the redaction graphics is the suggested way. and there is not difference and PDF page graphics and redaction graphics. We can draw the replaced text without losing of background by using the following approach.

PdfLoadedDocument ldoc = new PdfLoadedDocument(@"background.pdf");
Dictionary<int, List<RectangleF>> matchedTextbounds = new Dictionary<int, List<RectangleF>>();
PdfLoadedPage page = ldoc.Pages[0] as PdfLoadedPage;
ldoc.FindText("Ipsum", out matchedTextbounds);
for (int i = 0; i < matchedTextbounds.Count; i++)
{
    List<RectangleF> rectangles = matchedTextbounds[i];
    for (int j = 0; j < rectangles.Count; j++)
    {
        //Create PDF redaction and set the fill color as transparent.
        PdfRedaction redaction = new PdfRedaction(rectangles[j], Color.Transparent);
        //Draw the replacement text on the redaction appearance.
        redaction.Appearance.Graphics.DrawString("Hello", new PdfStandardFont(PdfFontFamily.Helvetica, 9), PdfBrushes.White, PointF.Empty);
        page.AddRedaction(redaction);
    }
}
ldoc.Save("output.pdf");

Note: It is not feasible to detect the page background, because it is a graphical content, it may have created using various elements like color, shapes, brushes, images, more. However, replacing text on the existing PDF document with the same font, color, size is currently will be considered when implementing the “Replace text” feature which is currently not available in our control. You can track the feature implementation from the following feedback link: https://www.syncfusion.com/feedback/2568/search-and-replace-the-same-or-small-text-from-existing-pdf-document

Regards, Deepak G

Reply

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile