Articles in this section

How to replace the existing text in a PDF using the .NET PDF library

The Syncfusion Essential® PDF library is a powerful and feature-rich .NET PDF framework that allows developers to create, read, and manipulate PDF documents programmatically. Using this library, you can efficiently replace existing text in a PDF document using the .NET PDF library.

Steps to replace existing text in a PDF programmatically:

  1. Create a new project: Start a new Console application in .NET Core.
    ConsoleFramework.png
  2. Install required packages: Add the Syncfusion.Pdf.WinForms NuGet package as a reference in the console application from Nuget.org.
  3. Set up the environment: In the Program.cs file, include the following namespaces.
    using System.Drawing;
    using Syncfusion.Pdf;
    using Syncfusion.Pdf.Graphics;
    using Syncfusion.Pdf.Parsing;
    using Syncfusion.Pdf.Redaction;
    using System.Collections.Generic
  4. Implement main logic: Use the provided code sample in Program.cs to replace existing text in a PDF using the .NET PDF library.
// Load the PDF document
using (PdfLoadedDocument ldoc = new PdfLoadedDocument("Input.pdf"))
{
    // Dictionary to store the found text bounds for each page
    Dictionary<int, List<RectangleF>> matchedTextbounds = new Dictionary<int, List<RectangleF>>();
    // Get the first page of the PDF
    PdfLoadedPage page = ldoc.Pages[0] as PdfLoadedPage;
    // Search for the text and get its bounding rectangles
    ldoc.FindText("Fusce", out matchedTextbounds);
    // Loop through each page with matches
    for (int i = 0; i < matchedTextbounds.Count; i++)
    {
        // Get all rectangles where the text was found on this page
        List<RectangleF> rectangles = matchedTextbounds[i];
        // Loop through each found rectangle
        for (int j = 0; j < rectangles.Count; j++)
        {
            // Create a redaction area with transparent fill over the found text
            PdfRedaction redaction = new PdfRedaction(rectangles[j], Color.Transparent);
            // Draw the replacement text on the redaction area
            redaction.Appearance.Graphics.DrawString(
                "Hello",
                new PdfStandardFont(PdfFontFamily.Helvetica, 9),
                PdfBrushes.Black,
                PointF.Empty
            );
            // Add the redaction to the page
            page.AddRedaction(redaction);
        }
    }
    // Save the PDF document
    ldoc.Save("Output.pdf");
}

A complete working sample is available for download from GitHub.

By executing the program, you will generate the following PDF document.

Conclusion

I hope you enjoyed learning about how to replace the existing text in a PDF using the .NET PDF library.

You can refer to our ASP.NET Core PDF feature tour 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 ASP.NET Core PDF example 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 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)
Access denied
Access denied