Our compliance team needed to update 500 contracts after a company name change. Manually editing PDFs in Adobe Acrobat would take days. We needed automated find-and-replace.
IronPDF's text replacement feature processed all 500 contracts in under an hour. Here's the implementation.
How Do I Replace Text in a PDF?
Use ReplaceTextOnPage or ReplaceTextOnPages:
using IronPdf;
// Install via NuGet: Install-Package IronPdf
var pdf = PdfDocument.FromFile("contract.pdf");
// Replace on specific page (page 1 = index 0)
pdf.ReplaceTextOnPage(0, "Old Company Name", "New Company LLC");
pdf.SaveAs("updated-contract.pdf");
Every instance of "Old Company Name" on page 1 becomes "New Company LLC".
Can I Replace Text Across All Pages?
Yes. Use ReplaceTextOnPages with all page indices:
var pdf = PdfDocument.FromFile("document.pdf");
// Create array of all page indices
var allPages = Enumerable.Range(0, pdf.PageCount).ToArray();
pdf.ReplaceTextOnPages(allPages, "draft", "final");
pdf.SaveAs("final-document.pdf");
Or loop through pages individually if needed for logging or validation.
How Do I Do Case-Insensitive Replacement?
IronPDF's replacement is case-sensitive by default. For case-insensitive, extract text, find matches with regex, then replace exact strings:
var pdf = PdfDocument.FromFile("document.pdf");
for (int i = 0; i < pdf.PageCount; i++)
{
string pageText = pdf.Pages[i].ExtractText();
// Find all case-insensitive matches
var matches = Regex.Matches(pageText, "confidential", RegexOptions.IgnoreCase);
foreach (Match match in matches)
{
// Replace with exact match
pdf.ReplaceTextOnPage(i, match.Value, "PUBLIC");
}
}
pdf.SaveAs("sanitized.pdf");
This replaces "confidential", "Confidential", "CONFIDENTIAL", etc.
Can I Replace with Regex Patterns?
Not directly. Extract text, find pattern matches, then replace:
var pdf = PdfDocument.FromFile("invoice.pdf");
string text = pdf.ExtractAllText();
// Find all invoice numbers (format: INV-12345)
var matches = Regex.Matches(text, @"INV-\d{5}");
foreach (Match match in matches)
{
string oldNumber = match.Value;
string newNumber = "REF-" + oldNumber.Substring(4); // REF-12345
// Replace on all pages
var allPages = Enumerable.Range(0, pdf.PageCount).ToArray();
pdf.ReplaceTextOnPages(allPages, oldNumber, newNumber);
}
pdf.SaveAs("updated-invoice.pdf");
This changes INV-12345 → REF-12345 throughout the document.
What About Replacing Formatted Text?
PDF text replacement doesn't preserve formatting. Replaced text inherits the font of surrounding text, which can cause mismatches:
Original: Bold Company Name
After replace: Regular "New Company Name" (bold formatting lost)
For formatted replacements, consider regenerating the PDF from source (HTML/Word) instead of text replacement.
How Do I Batch Replace in Multiple PDFs?
Loop through files:
var files = Directory.GetFiles("contracts", "*.pdf");
foreach (var file in files)
{
var pdf = PdfDocument.FromFile(file);
var allPages = Enumerable.Range(0, pdf.PageCount).ToArray();
pdf.ReplaceTextOnPages(allPages, "Acme Corp", "Acme Corporation");
pdf.SaveAs(file.Replace(".pdf", "-updated.pdf"));
}
I updated 500+ contracts this way after a legal entity name change.
Can I Replace Multiple Terms at Once?
Yes. Chain replacements:
var pdf = PdfDocument.FromFile("document.pdf");
var allPages = Enumerable.Range(0, pdf.PageCount).ToArray();
var replacements = new Dictionary<string, string>
{
{ "Mr. Smith", "Dr. Smith" },
{ "Draft", "Final" },
{ "2024", "2025" }
};
foreach (var kvp in replacements)
{
pdf.ReplaceTextOnPages(allPages, kvp.Key, kvp.Value);
}
pdf.SaveAs("updated.pdf");
Apply multiple find-replace operations in sequence.
Does Replacement Work on Scanned PDFs?
No. Scanned PDFs are images without searchable text. Text replacement requires text layers.
For scanned PDFs, use OCR first (IronOCR), then replace text in the resulting searchable PDF.
How Do I Verify Replacement Worked?
Extract text after replacement:
var pdf = PdfDocument.FromFile("document.pdf");
pdf.ReplaceTextOnPages(new[] { 0 }, "old", "new");
string afterText = pdf.Pages[0].ExtractText();
if (afterText.Contains("new") && !afterText.Contains("old"))
{
Console.WriteLine("Replacement successful");
pdf.SaveAs("updated.pdf");
}
else
{
Console.WriteLine("Replacement failed or incomplete");
}
Validate before saving to prevent data loss.
What Are Replacement Limitations?
Font subsetting: If the replacement text contains characters not in the original font, they may not render correctly.
Layout reflow: PDFs don't reflow text. If replacement text is longer, it may overlap or truncate.
Formatting loss: Bold, italics, colors are lost in replaced text.
For production use, test replacements thoroughly on sample PDFs before batch processing.
Can I Replace Text in Form Fields?
No. ReplaceTextOnPages works on static text content, not form field values. For form fields, use the form API:
var field = pdf.Form.FindFormField("companyName");
field.Value = "New Company LLC";
See the form editing tutorial for details.
How Do I Handle Password-Protected PDFs?
Open with the password first:
var pdf = PdfDocument.FromFile("protected.pdf", "password123");
var allPages = Enumerable.Range(0, pdf.PageCount).ToArray();
pdf.ReplaceTextOnPages(allPages, "old", "new");
pdf.SaveAs("updated-protected.pdf");
The output PDF retains the original password protection.
What About Performance?
Replacing text on a 100-page PDF takes 5-10 seconds depending on text volume. For large batches:
Parallel.ForEach(files, new ParallelOptions { MaxDegreeOfParallelism = 4 }, file =>
{
var pdf = PdfDocument.FromFile(file);
// ... perform replacements ...
pdf.SaveAs(outputPath);
});
Process multiple PDFs concurrently to maximize throughput.
Written by Jacob Mellor, CTO at Iron Software. Jacob created IronPDF and leads a team of 50+ engineers building .NET document processing libraries.
Top comments (0)