Searching and Replacing Text in PDFs with PDFEdit


Requirements: Secure PDF or PDF SDK

Introduction

Text in a PDF is not always final once the file has been generated. For example, a contract template with placeholder fields often needs those placeholders filled in with real values before it is sent to a client, and a recurring value like a company name repeated across a batch of invoices may need to be updated after the fact without regenerating the underlying document. The PDFEdit component in Secure PDF and PDF SDK provides a dedicated API for this task at both the page and document levels.

This guide covers how to use PDFEdit to search for and replace text in a PDF.

Overview

Text replacement in PDFEdit is handled by two methods:

  • ReplacePageText searches a single page for matching text and replaces it.
  • ReplaceDocumentText searches every page in the document and replaces matches throughout.

Both methods take the text to search for and the replacement text, and both return the number of matches that were replaced.

Replacing Text on a Single Page

Use ReplacePageText when the text you want to replace is known to occur on a specific page, such as a page you have already identified through prior processing:

pdfedit.InputFile = "invoice.pdf"; pdfedit.OutputFile = "invoice_filled.pdf";
pdfedit.Open(); int occurrences = pdfedit.ReplacePageText(0, "SELLER_NAME", "/n software"); pdfedit.Close();

This example replaces every occurrence of SELLER_NAME on the first page (index 0) with /n software, and returns the number of replacements made.

Replacing Text Throughout a Document

When the text you are targeting may appear anywhere in the document, or you simply want to avoid tracking which page it is on, use ReplaceDocumentText instead. It applies the same search-and-replace logic across every page:

pdfedit.InputFile = "contract_template.pdf"; pdfedit.OutputFile = "contract_final.pdf";
pdfedit.Open(); int occurrences = pdfedit.ReplaceDocumentText("CLIENT_NAME", "Acme Corporation"); pdfedit.Close();

This is well suited to templating workflows, where a document is authored once with placeholder text and then populated repeatedly with different values for each recipient or record.

Refining Replacements with the ReplaceText Event

Both of the above methods fire the ReplaceText event as they work through the document, giving you the opportunity to inspect each match before it is applied, skip specific occurrences, or adjust the replacement text on the fly. This is useful when a straightforward one-to-one substitution is not enough, for example when the same placeholder needs different replacement values depending on where it appears:

pdfedit.InputFile = "invoice.pdf"; pdfedit.OutputFile = "invoice_filled.pdf";
pdfedit.OnReplaceText += (s, e) => { if (e.Substitute == "/n software") { e.Substitute = "my company"; } };
pdfedit.Open(); int occurrences = pdfedit.ReplacePageText(0, "SELLER_NAME", "/n software"); pdfedit.Close();

In this example, the event handler intercepts the replacement and substitutes a different value than the one originally passed to ReplacePageText, without needing a separate call for each variation.

We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.