IRONSOFTWAREHOME

How to Replace Text in a PDF Using C#

Curtis Chau
Curtis Chau
Updated: August 2, 2026

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# smooth. Dive into this guide to transform your PDF handling efficiency in .NET environments.

  1. 1Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. 2Copy and run this code snippet.

    IronPdf.PdfDocument.FromFile("example.pdf")
        .ReplaceTextOnAllPages("old text", "new text")
        .SaveAs("updated.pdf");
    C#
  3. 3Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer

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?

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");

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 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.

Tips: All page indexes follow zero-based indexing.

What Do I Replace Text on a Single Page?

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");

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?

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");

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.


My favorite library of this kind is IronPDF. It allows for fast and efficient manipulation of PDF files. It also has many valuable features, like exporting to PDF/A format and digitally signing PDF documents.

Milan Jovanovic

Microsoft MVP

View case study

IronOCR means we can save $40,000 annually from manual processing, while enhancing productivity and freeing up resources for high-impact tasks. I would highly recommend it.

Brent Matzelle

Chief Technology Officer, OPYN

View case study

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?

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");
var pdfFont = pdf.Fonts.Add(fontByte);

// Use custom font
pdf.ReplaceTextOnPage(0, oldText, newText, pdfFont, 24);

pdf.SaveAs("replaceCustomText.pdf");
Comparison showing custom font usage migrating from .NET 6 to .NET 7 with downward arrow

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

What is IronPDF and how can it help with PDF text replacement?

IronPDF is a C# library that enables easy manipulation of PDF documents, including text replacement. With its methods like `ReplaceTextOnAllPages()` and `ReplaceTextOnPage()`, you can automate text updates across entire documents or specific pages efficiently.

How do I replace text on all pages of a PDF using C#?

To replace text on all pages of a PDF in C#, you utilize the `ReplaceTextOnAllPages(oldText, newText)` method of IronPDF. This method allows you to specify the text to be replaced and the new text, applying changes across the entire document.

Can I replace text on specific pages of a PDF with IronPDF?

Yes, IronPDF allows targeted text replacement using the `ReplaceTextOnPage()` method for single-page updates and `ReplaceTextOnPages()` for multiple pages. These methods offer precise control over the document modifications.

Is it possible to replace text with newlines for better formatting?

Yes, you can include newline characters in the new string to format texts with newlines using IronPDF. This is effective for multiline text replacement, enhancing document readability and structure.

How do I use a custom font when replacing text in a PDF?

To replace text with a custom font in IronPDF, first add the desired font to the PDF using the `Fonts.Add()` method, then specify this font when performing text replacement. This is crucial for maintaining brand consistency.

Can IronPDF handle dynamic content updates in PDF templates?

Yes, IronPDF effectively handles dynamic content updates through its text replacement features, which are particularly useful for PDF templates, enabling automated corrections and customizations.

What are some advanced use cases for text replacement in PDFs using IronPDF?

Advanced use cases for text replacement with IronPDF include combining it with stamping features, adding signatures, implementing async processing for batch operations, and utilizing redaction for sensitive information.

How do I handle exceptions when replacing text using IronPDF?

When using `ReplaceTextOnAllPages()`, if the old text cannot be found, IronPDF raises an exception with a detailed message. Handling these exceptions ensures reliable text replacement operations.

Is it possible to integrate text replacement with other PDF operations?

Yes, IronPDF allows integration of text replacement with other PDF operations like adding headers, footers, stamping, and more, providing comprehensive document management capabilities.

How does IronPDF ensure performance during large document processing?

IronPDF supports async and multithreading capabilities, optimizing performance and enabling high throughput in enterprise environments when processing large or multiple PDF documents.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...
Read More

Ready to Get Started?

Nuget Downloads 21,253,952Version:2026.10just released

Get your FREE

30-day Trial Key instantly.

bullet_checkedNo credit card or account creation required
bullet_testTest in production
without watermarks
bullet_calendar30 days fully
functional product
bullet_support24/5 technical
support during trial
Get your free 30-day Trial Key instantly.
No credit card or account creation required
C# NuGet Library for PDF
Install with NuGet

Version: 2026.10

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search "IronPdf"
  3. Select the package and install
C# PDF DLL
Download DLL

Version: 2026.10

or download Windows Installer here.

  1. Download and unzip IronPDF to a location such as ~/Libs within your Solution directory
  2. In Visual Studio Solution Explorer, right click References. Select Browse, "IronPdf.dll"

Licenses from $999

Key in blue circle

Get your free 30-day Trial Key instantly

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get a Quote, No Obligation
Your details will always be kept confidential.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Trusted by Millions of Engineers Worldwide
Get your free 30-day Trial Key instantly.
No credit card or account creation required
C# NuGet Library for PDF
Install with NuGet

Version: 2026.10

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search "IronPdf"
  3. Select the package and install
C# PDF DLL
Download DLL

Version: 2026.10

or download Windows Installer here.

  1. Download and unzip IronPDF to a location such as ~/Libs within your Solution directory
  2. In Visual Studio Solution Explorer, right click References. Select Browse, "IronPdf.dll"

Licenses from $999