Articles in this section
Category / Section

How to replace the existing text in the WinForms PDF?

4 mins read

We can replace the existing text in the PDF document by drawing the replacement text on the graphics using the following methods in our Winforms PDF feature tour page.

 

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);

 

    

2)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,

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


Click here to explore how to search and replace text in a PDF document.

Conclusion

I hope you enjoyed learning about how to replace the existing text in the WinForms PDF.

You can refer to our Winforms PDF feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please  to leave a comment
Access denied
Access denied