How to Replace Text in a PDF
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.
How to Replace Text in a PDF
- Download the IronPDF C# Library
- Render a fresh PDF or import an existing PDF document
- Use the
ReplaceTextOnAllPages
method to replace text throughout the document - Specify the page numbers for targeted text replacement
- Export the edited PDF document
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")
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
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")
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")
Output PDF
Use Custom Font
The same ReplaceTextOnPage
method also allows you to use a custom font and size. First, the font needs to be added to the PDF, after which you can pass the font name as a parameter to the method. In the following example, I will use the Pixelify Sans Font.
Replace Text on a Single Page
:path=/static-assets/pdf/content-code-examples/how-to/find-replace-text-custom-font.cs
using IronPdf;
using System.IO;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Use custom font .NET6</h1>");
string oldText = ".NET6";
string newText = ".NET7";
// Add custom font
byte[] fontByte = File.ReadAllBytes(@".\PixelifySans-VariableFont_wght.ttf");
pdf.Fonts.Add(fontByte);
// Use custom font
pdf.ReplaceTextOnPage(0, oldText, newText, "PixelifySans-Regular", 24);
pdf.SaveAs("replaceCustomText.pdf");
Imports IronPdf
Imports System.IO
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Use custom font .NET6</h1>")
Private oldText As String = ".NET6"
Private newText As String = ".NET7"
' Add custom font
Private fontByte() As Byte = File.ReadAllBytes(".\PixelifySans-VariableFont_wght.ttf")
pdf.Fonts.Add(fontByte)
' Use custom font
pdf.ReplaceTextOnPage(0, oldText, newText, "PixelifySans-Regular", 24)
pdf.SaveAs("replaceCustomText.pdf")
Result
