How to Replace Text in a PDF

by Chaknith Bin

The feature to replace text in a PDF is extremely useful for making quick and precise edits to content, such as correcting typos, updating information, or customizing templates for different purposes. This can save a significant amount of time and effort, especially when dealing with documents that require frequent revisions or personalization.

IronPDF provides a feature for replacing text in PDFs. This feature makes IronPDF an invaluable tool for developers and professionals who need to automate or customize PDF content.

Get started with IronPDF

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer



Replace Text Example

The 'replace text' action can be applied to any PdfDocument object, whether it's newly rendered or imported. You can utilize the ReplaceTextOnAllPages method by providing both the old and new text for replacement. If the method cannot locate the specified old text, it will raise an exception with the message 'Error while replacing text: failed to find text '.NET6'.'

In the code example below, we demonstrate how to replace text on a newly rendered PDF document containing the text '.NET6'.

Code

:path=/static-assets/pdf/content-code-examples/how-to/find-replace-text-all-page.cs
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();

PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>.NET6</h1>");

string oldText = ".NET6";
string newText = ".NET7";

// Replace text on all pages
pdf.ReplaceTextOnAllPages(oldText, newText);

pdf.SaveAs("replaceText.pdf");
Imports IronPdf

Private renderer As New ChromePdfRenderer()

Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>.NET6</h1>")

Private oldText As String = ".NET6"
Private newText As String = ".NET7"

' Replace text on all pages
pdf.ReplaceTextOnAllPages(oldText, newText)

pdf.SaveAs("replaceText.pdf")
VB   C#

Replace Text on Specified Pages

For greater accuracy in replacing text within a document, IronPDF also offers options to replace text on a single page or multiple pages, depending on your requirements. You can use the ReplaceTextOnPage method to replace text on a specific page and the ReplaceTextOnPages method to replace text on multiple specified pages of the document.

Tips
All page indexes follow zero-based indexing.

Replace Text on a Single Page

:path=/static-assets/pdf/content-code-examples/how-to/find-replace-text-on-single-page.cs
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();

PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>.NET6</h1>");

string oldText = ".NET6";
string newText = ".NET7";

// Replace text on page 1
pdf.ReplaceTextOnPage(0, oldText, newText);

pdf.SaveAs("replaceTextOnSinglePage.pdf");
Imports IronPdf

Private renderer As New ChromePdfRenderer()

Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>.NET6</h1>")

Private oldText As String = ".NET6"
Private newText As String = ".NET7"

' Replace text on page 1
pdf.ReplaceTextOnPage(0, oldText, newText)

pdf.SaveAs("replaceTextOnSinglePage.pdf")
VB   C#

Replace Text on Multiple Pages

:path=/static-assets/pdf/content-code-examples/how-to/find-replace-text-on-multiple-pages.cs
using IronPdf;

string html = @"<p> .NET6 </p>
<p> This is 1st Page </p>
<div style = 'page-break-after: always;'></div>
<p> This is 2nd Page</p>
<div style = 'page-break-after: always;'></div>
<p> .NET6 </p>
<p> This is 3rd Page</p>";

ChromePdfRenderer renderer = new ChromePdfRenderer();

PdfDocument pdf = renderer.RenderHtmlAsPdf(html);

string oldText = ".NET6";
string newText = ".NET7";

int[] pages = { 0, 2 };

// Replace text on page 1 & 3
pdf.ReplaceTextOnPages(pages, oldText, newText);

pdf.SaveAs("replaceTextOnMultiplePages.pdf");
Imports IronPdf

Private html As String = "<p> .NET6 </p>
<p> This is 1st Page </p>
<div style = 'page-break-after: always;'></div>
<p> This is 2nd Page</p>
<div style = 'page-break-after: always;'></div>
<p> .NET6 </p>
<p> This is 3rd Page</p>"

Private renderer As New ChromePdfRenderer()

Private pdf As PdfDocument = renderer.RenderHtmlAsPdf(html)

Private oldText As String = ".NET6"
Private newText As String = ".NET7"

Private pages() As Integer = { 0, 2 }

' Replace text on page 1 & 3
pdf.ReplaceTextOnPages(pages, oldText, newText)

pdf.SaveAs("replaceTextOnMultiplePages.pdf")
VB   C#

Output PDF

Chaknith related to Output PDF

Chaknith Bin

Software Engineer

Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.