Articles in this section
Category / Section

How to find and replace text in PDF Document?

2 mins read

The Syncfusion Essential PDF is a feature-rich and high-performance .NET PDF library used to create, read, and edit PDF documents programmatically without Adobe dependencies. Using this library, you can find and replace text from the existing PDF document with the help of our redaction feature.

Steps to find and replace text in a PDF programmatically

  1. Create a new console application project.image.png

  2. Install the Syncfusion.Pdf.Imaging.Net.Core NuGet package as a reference to your .NET Core application from NuGet.org.

    Screenshot (1384).png

  3. Include the following namespaces in the Program.cs file.

C#

using Syncfusion.Drawing;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf.Redaction;
  1. Use the following code sample in Program.cs to find and replace text from an existing PDF document.

C#

//Load the existing PDF document.
FileStream docStream = new FileStream(@"../../../Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
Dictionary<int, List<RectangleF>> matchedTextbounds = new Dictionary<int, List<RectangleF>>();
//Find the text using FindText method
loadedDocument.FindText("Fusce", out matchedTextbounds);

// Iterate through the matchedTextbounds dictionary.
foreach (KeyValuePair<int, List<RectangleF>> matchedText in matchedTextbounds)
{
   //Create a font to draw the replacement text.
   PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 9);
   int pageIndex = matchedText.Key; // Get the page index
   List<RectangleF> rectangles = matchedText.Value;
   // Access the page using the page index
   PdfLoadedPage lPage = loadedDocument.Pages[pageIndex] as PdfLoadedPage;

   // Now, you can work with the page as needed.
   for (int j = 0; j < rectangles.Count; j++)
   {
       //Create a 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("Thats", font, PdfBrushes.Black, PointF.Empty);
       lPage.AddRedaction(redaction);
   }
}

//Apply redaction.
loadedDocument.Redact();

//Creating the stream object.
MemoryStream stream = new MemoryStream();
//Save a PDF document to a stream.
loadedDocument.Save(stream);
// Write the content of the memory stream to an output PDF file.
File.WriteAllBytes(@"../../../Output.pdf", stream.ToArray());
//Close the document.
loadedDocument.Close(true);

A complete working sample can be downloaded from Find_and_replace_text_in_a_PDF.zip

By executing the program, you will get the PDF document as follows.Screenshot (1383).png

Conclusion
I hope you enjoyed learning about how to find and replace text in PDF Document.

You can refer to our .NET PDF library page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our .NET PDF library Example to understand how to create and manipulate data.

For current customers, you can check out our ASP.NET Core 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 forums, Direct-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 (0)
Please sign in to leave a comment
Access denied
Access denied