C#: Replace Text in a PDF Document

2024-03-20 09:49:00 Written by  support iceblue
Rate this item
(6 votes)

People often need to replace text in a PDF document for a variety of reasons. It could be to correct errors or typos, update outdated information, customize the content for a specific audience or purpose, or comply with legal or regulatory requirements. By replacing text in a PDF, individuals can ensure accuracy, maintain document integrity, and enhance the overall quality and relevance of the information presented.

In this article, you will learn how to replace text in a PDF document in C# by using the Spire.PDF for .NET library.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Replace Text in a Specific PDF Page in C#

Spire.PDF for .NET offers the PdfTextReplacer.ReplaceAllText() method, allowing users to replace all occurrences of target text in a page with new text. The following are the steps to replace text in a specific page using C#.

  • Create a PdfDocument object.
  • Load a PDF file for a specified path.
  • Get a specific page from the document.
  • Create a PdfTextReplaceOptions object, and specify the replace options using ReplaceType property of the object.
  • Create a PdfTextReplacer object, and apply the replace options using Options property of it.
  • Replace all occurrences of the target text in the page with new text using PdfTextReplacer.ReplaceAllText() method.
  • Save the document to a different PDF file.
  • C#
using Spire.Pdf;
using Spire.Pdf.Texts;

namespace ReplaceTextInPage
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            // Load a PDF file
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf");

            // Create a PdfTextReplaceOptions object
            PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();

            // Specify the options for text replacement
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.IgnoreCase;
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.WholeWord;
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.AutofitWidth;

            // Get a specific page
            PdfPageBase page = doc.Pages[0];

            // Create a PdfTextReplacer object based on the page
            PdfTextReplacer textReplacer = new PdfTextReplacer(page);

            // Set the replace options
            textReplacer.Options = textReplaceOptions;

            // Replace all occurrences of target text with new text
            textReplacer.ReplaceAllText(".NET Framework", "New Content");

            // Save the document to a different PDF file
            doc.SaveToFile("ReplaceTextInPage.pdf");

            // Dispose resources
            doc.Dispose();
        }
    }
}

C#: Replace Text in a PDF Document

Replace Text in an Entire PDF Document in C#

In order to replace all occurrences of target text in the entire document with new text, you need to iterate through pages in the document and replace text on each page using the PdfTextReplacer.ReplaceAllText() method.

The following are the steps to replace text in an entire PDF document using C#.

  • Create a PdfDocument object.
  • Load a PDF file for a specified path.
  • Create a PdfTextReplaceOptions object, and specify the replace options using ReplaceType property of the object.
  • Iterate through the pages in the document.
    • Create a PdfTextReplacer object based on a specified page, and apply the replace options using Options property of it.
    • Replace all occurrences of the target text in the page with new text using PdfTextReplacer.ReplaceAllText() method.
  • Save the document to a different PDF file.
  • C#
using Spire.Pdf;
using Spire.Pdf.Texts;

namespace ReplaceInEntireDocument
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            // Load a PDF file
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf");

            // Create a PdfTextReplaceOptions object
            PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();

            // Specify the options for text replacement
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.IgnoreCase;
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.WholeWord;
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.AutofitWidth;

            for (int i = 0; i < doc.Pages.Count; i++) {

                // Get a specific page
                PdfPageBase page = doc.Pages[i];

                // Create a PdfTextReplacer object based on the page
                PdfTextReplacer textReplacer = new PdfTextReplacer(page);

                // Set the replace options
                textReplacer.Options = textReplaceOptions;

                // Replace all occurrences of target text with new text
                textReplacer.ReplaceAllText(".NET Framework", "New Content");
            }
            

            // Save the document to a different PDF file
            doc.SaveToFile("ReplaceTextInDocument.pdf");

            // Dispose resources
            doc.Dispose();
        }
    }
}

Replace the First Occurrence of the Target Text in C#

Instead of replacing all text on a page, you can only replace the first occurrence of the target text by utilizing the ReplaceText() method of the PdfTextReplacer class.

The following are the steps to replace the first occurrence of the target text using C#.

  • Create a PdfDocument object.
  • Load a PDF file for a specified path.
  • Get a specific page from the document.
  • Create a PdfTextReplaceOptions object, and specify the replace options using ReplaceType property of the object.
  • Create a PdfTextReplacer object, and apply the replace options using Options property of it.
  • Replace the first occurrence of the target text in the page with new text using PdfTextReplacer.ReplaceText() method.
  • Save the document to a different PDF file.
  • C#
using Spire.Pdf;
using Spire.Pdf.Texts;

namespace ReplaceFirstOccurance
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            // Load a PDF file
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf");

            // Create a PdfTextReplaceOptions object
            PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();

            // Specify the options for text replacement
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.IgnoreCase;
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.WholeWord;
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.AutofitWidth;

            // Get a specific page
            PdfPageBase page = doc.Pages[1];

            // Create a PdfTextReplacer object based on the page
            PdfTextReplacer textReplacer = new PdfTextReplacer(page);

            // Set the replace options
            textReplacer.Options = textReplaceOptions;

            // Replace the first occurrence of target text with new text
            textReplacer.ReplaceText(".NET Framework", "New Content");

            // Save the document to a different PDF file
            doc.SaveToFile("ReplaceFirstOccurance.pdf");

            // Dispose resources
            doc.Dispose();
        }
    }
}

C#: Replace Text in a PDF Document

Replace Text Based on a Regular Expression in C#

Regular expressions are powerful and versatile patterns used for matching and manipulating text. With Spire.PDF, you utilize regular expressions to search for specific text patterns in a PDF and replace them with new strings.

The steps to replace text in PDF based on a regular expression are as follows.

  • Create a PdfDocument object.
  • Load a PDF file for a specified path.
  • Get a specific page from the document.
  • Create a PdfTextReplaceOptions object.
  • Specify the replace type as Regex using PdfTextReplaceOptions.ReplaceType property.
  • Create a PdfTextReplacer object, and apply the replace options using Options property of it.
  • Find and replace the text that matches a specified regular expression using PdfTextReplacer.ReplaceAllText() method.
  • Save the document to a different PDF file.
  • C#
using Spire.Pdf;
using Spire.Pdf.Texts;

namespace ReplaceUsingRegularExpression
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            // Load a PDF file
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf");

            // Create a PdfTextReplaceOptions object
            PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();

            // Set the replace type as Regex
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.Regex;

            // Get a specific page
            PdfPageBase page = doc.Pages[1];

            // Create a PdfTextReplacer object based on the page
            PdfTextReplacer textReplacer = new PdfTextReplacer(page);

            // Set the replace options
            textReplacer.Options = textReplaceOptions;

            // Specify the regular expression
            string regularExpression = @"\bC\w*?R\b";

            // Replace all occurrences that match the regular expression with new text
            textReplacer.ReplaceAllText(regularExpression, "NEW");

            // Save the document to a different PDF file
            doc.SaveToFile("ReplaceWithRegularExpression.pdf");

            // Dispose resources
            doc.Dispose();
        }
    }
}

C#: Replace Text in a PDF Document

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Wednesday, 20 March 2024 08:51