How to Replace Text in a PDF Using C#
Replace text in PDFs using C# with IronPDF’s simple API - use ReplaceTextOnAllPages() for document-wide changes or ReplaceTextOnPage() for specific pages. This enables automated corrections, template customization, and dynamic content updates in .NET applications.
Quickstart: Replace Text in PDF with IronPDF
Begin replacing text in your PDFs effortlessly using IronPDF. With just a few lines of code, you can update or customize documents quickly. This example demonstrates how to replace text across all pages of a PDF. Simply load your PDF, specify the text to find and replace, and save the updated document. Perfect for correcting typos or updating information in templates, IronPDF makes text replacement in C# seamless. Dive into this guide to transform your PDF handling efficiency in .NET environments.
Get started making PDFs with NuGet now:
Install IronPDF with NuGet Package Manager
Copy and run this code snippet.
IronPdf.PdfDocument.FromFile("example.pdf") .ReplaceTextOnAllPages("old text", "new text") .SaveAs("updated.pdf");Deploy to test on your live environment
Minimal Workflow (5 steps)
- Download the IronPDF C# Library
- Render a fresh PDF or import an existing PDF document
- Use the
ReplaceTextOnAllPagesmethod to replace text throughout the document - Specify the page numbers for targeted text replacement
- Export the edited PDF document
How Do I Replace Text in a PDF?
The 'replace text' action can be applied to any PdfDocument object, whether newly rendered or imported. Use the ReplaceTextOnAllPages method by providing both the old and new text for replacement. If the method cannot locate the specified old text, it raises 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'. This functionality is particularly useful when working with HTML to PDF conversion or when you need to edit existing PDFs.
What Does the Basic Code Look Like?
:path=/static-assets/pdf/content-code-examples/how-to/find-replace-text-all-page.csusing 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");When implementing text replacement in your applications, you might also want to explore adding headers and footers or applying custom watermarks to your PDFs for enhanced document branding and identification.
How Do I Replace Text with Newlines?
The replace text action supports newline characters, allowing you to replace old text with a new string that includes built-in newlines for better formatting and visual clarity. This feature is essential when you need to maintain proper document structure or when working with multi-line text content. To achieve this, add newline characters (\n) to the new string. Using the example above, replace newText with .NET7\nnewline instead of just .NET7. This approach works seamlessly with various rendering options available in IronPDF.
How Do I Replace Text on Specified Pages?
For greater accuracy in replacing text within a document, IronPDF offers options to replace text on a single page or multiple pages. Use the ReplaceTextOnPage method to replace text on a specific page and the ReplaceTextOnPages method to replace text on multiple specified pages. This granular control is particularly useful when working with complex PDF forms or documents with varying content across pages.
How Do I Replace Text on a Single Page?
:path=/static-assets/pdf/content-code-examples/how-to/find-replace-text-on-single-page.csusing 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");This single-page replacement feature integrates well with other page-specific operations like adding page numbers or when you need to split PDFs for targeted content modification.
How Do I Replace Text on Multiple Pages?
:path=/static-assets/pdf/content-code-examples/how-to/find-replace-text-on-multiple-pages.csusing 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");When working with multi-page documents, consider implementing PDF compression to optimize file sizes after text replacement operations, especially when dealing with large documents or batch processing scenarios.
How Can I Use a Custom Font When Replacing Text?
The ReplaceTextOnPage method also allows you to use a custom font and size. First, add the font to the PDF, then pass the font name as a parameter to the method. In the following example, I use the Pixelify Sans Font. Custom font implementation is particularly important when maintaining brand consistency or when working with international languages and UTF-8 support.
What Steps Are Required for Custom Font Implementation?
:path=/static-assets/pdf/content-code-examples/how-to/find-replace-text-custom-font.csusing 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");
var pdfFont = pdf.Fonts.Add(fontByte);
// Use custom font
pdf.ReplaceTextOnPage(0, oldText, newText, pdfFont, 24);
pdf.SaveAs("replaceCustomText.pdf");
Advanced Text Replacement Scenarios
Beyond basic text replacement, IronPDF offers powerful capabilities for complex scenarios. When working with templates or dynamic documents, combine text replacement with other features like stamping text and images or adding digital signatures. For applications requiring batch processing, implement text replacement alongside async and multithreading capabilities to optimize performance when processing multiple PDFs simultaneously. This is particularly useful in enterprise environments where document throughput is critical. When replacing sensitive information, explore IronPDF’s redaction features for permanent removal of confidential data, ensuring compliance with data protection regulations.
Frequently Asked Questions
How do I replace text across all pages of a PDF using C#?
With IronPDF, you can easily replace text across all pages using the ReplaceTextOnAllPages() method. Simply load your PDF document, call this method with the old text and new text parameters, and save the updated document. This feature is perfect for batch corrections and template updates.
Can I replace text on specific pages only instead of the entire document?
Yes, IronPDF provides the ReplaceTextOnPage() method that allows you to target specific pages for text replacement. This gives you precise control over which pages are modified, making it ideal for selective updates in multi-page documents.
What happens if the text I want to replace isn't found in the PDF?
When IronPDF cannot locate the specified text to replace, it raises an exception with a clear error message indicating that the text was not found. This helps you handle cases where the search text might not exist in the document.
Is it possible to replace text that contains newline characters?
Yes, IronPDF's replace text functionality fully supports newline characters. You can replace old text with new strings that include built-in newlines, maintaining proper document formatting and structure for multi-line content.
Can I use text replacement on PDFs created from HTML?
Absolutely! IronPDF's text replacement works seamlessly on PDFs generated from HTML conversion. Whether you've created a PDF from HTML or imported an existing document, the ReplaceTextOnAllPages() method functions identically.
What are the main steps to implement text replacement in a PDF?
The process involves five simple steps with IronPDF: First, download and install the library. Second, render or import your PDF document. Third, use ReplaceTextOnAllPages() for document-wide changes. Fourth, optionally specify page numbers for targeted replacement. Finally, save the edited PDF using SaveAs().






