Text Find and Replace
This code example demonstrates how to replace specific text in any PDF document. This feature can be applied to both newly rendered and existing PDFs.
To replace text in a PDF document, you can utilize the replaceText
method. It requires three arguments: the old text to find, the new text to replace it with, and the page index where this replacement should take place. In this example, it substitutes ".NET6" with ".NET7" on the specified page.
using IronPdf;
using System;
class PDFTextReplacer
{
static void Main()
{
// Load an existing PDF document
var pdfDocument = PdfDocument.FromFile("example.pdf");
// The old text to be replaced
string oldText = ".NET6";
// The new text that will replace the old text
string newText = ".NET7";
// Page index where the text replacement should occur (0-based index)
int pageIndex = 0;
// Replace text on the specified page
pdfDocument.ReplaceText(pageIndex, oldText, newText);
// Save the modified PDF document to a new file
pdfDocument.SaveAs("modified_example.pdf");
// Inform the user that the process is complete
Console.WriteLine("Text replacement complete. Modified PDF saved as 'modified_example.pdf'.");
}
}
using IronPdf;
using System;
class PDFTextReplacer
{
static void Main()
{
// Load an existing PDF document
var pdfDocument = PdfDocument.FromFile("example.pdf");
// The old text to be replaced
string oldText = ".NET6";
// The new text that will replace the old text
string newText = ".NET7";
// Page index where the text replacement should occur (0-based index)
int pageIndex = 0;
// Replace text on the specified page
pdfDocument.ReplaceText(pageIndex, oldText, newText);
// Save the modified PDF document to a new file
pdfDocument.SaveAs("modified_example.pdf");
// Inform the user that the process is complete
Console.WriteLine("Text replacement complete. Modified PDF saved as 'modified_example.pdf'.");
}
}
Imports IronPdf
Imports System
Friend Class PDFTextReplacer
Shared Sub Main()
' Load an existing PDF document
Dim pdfDocument = PdfDocument.FromFile("example.pdf")
' The old text to be replaced
Dim oldText As String = ".NET6"
' The new text that will replace the old text
Dim newText As String = ".NET7"
' Page index where the text replacement should occur (0-based index)
Dim pageIndex As Integer = 0
' Replace text on the specified page
pdfDocument.ReplaceText(pageIndex, oldText, newText)
' Save the modified PDF document to a new file
pdfDocument.SaveAs("modified_example.pdf")
' Inform the user that the process is complete
Console.WriteLine("Text replacement complete. Modified PDF saved as 'modified_example.pdf'.")
End Sub
End Class
Once the text replacement is completed, you can save the modified PDF document using the SaveAs
method. For more information on how to manipulate and manage PDF files, visit the IronPDF Features Page.
Additionally, you can explore other powerful tools offered by Iron Software, such as IronOCR for OCR capabilities, IronBarcode for barcode generation and reading, or IronSoftware.com for a full list of products.