Do you need a high-speed solution to find and replace text in a bunch of PDF files using C#? No need to worry because this article provides a powerful mechanism to find and replace text in PDF files within a few simple steps. Let’s find out how it works.

C# find and replace text in PDF

The find and replace option makes it possible to replace a particular piece of text in a document in one go. This way, you do not have to locate and update each occurrence of the text in the whole document manually. So let’s see how to find and replace text in a PDF in C#. You can also use the same guidelines and the provided code samples to replace text in a batch of PDF files.

C# Library to Find and Replace Text in PDF

To find and replace text in PDF files, we will use Aspose.PDF for .NET. It is a C# class library that provides basic as well as advanced PDF manipulation features for .NET applications. The library also lets you find and replace text in PDF files in different ways with high accuracy and speed.

You can either download the library’s DLL or install it using NuGet.

PM> Install-Package Aspose.PDF

Find and Replace Text in PDF using C#

The following are steps to find and replace text in a PDF document.

C# Code to Replace Text in a PDF

The following code sample shows how to find and replace text in PDF using C#.

// Open document
Document pdfDocument = new Document("Document.pdf");
// Create TextAbsorber object to find all instances of the input search phrase
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("text");
// Accept the absorber for all the pages
pdfDocument.Pages.Accept(textFragmentAbsorber);
// Get the extracted text fragments
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
// Loop through the fragments
foreach (TextFragment textFragment in textFragmentCollection)
{
// Update text and other properties
textFragment.Text = "TEXT";
textFragment.TextState.Font = FontRepository.FindFont("Verdana");
textFragment.TextState.FontSize = 22;
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Blue);
textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Green);
}
// Save resulting PDF document.
pdfDocument.Save("updated-document.pdf");

C# Replace Text in a Particular PDF Page

The following are steps to find and replace text on a particular page in a PDF document.

The following code sample shows how to find and replace text on a particular page of the PDF using C#.

// Open document
Document pdfDocument = new Document("Document.pdf");
// Create TextAbsorber object to find all instances of the input search phrase
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("text");
// Accept the absorber for desired
pdfDocument.Pages[1].Accept(textFragmentAbsorber);
// Get the extracted text fragments
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
// Loop through the fragments
foreach (TextFragment textFragment in textFragmentCollection)
{
// Update text and other properties
textFragment.Text = "TEXT";
textFragment.TextState.Font = FontRepository.FindFont("Verdana");
textFragment.TextState.FontSize = 22;
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Blue);
textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Green);
}
// Save resulting PDF document.
pdfDocument.Save("updated-document.pdf");

Find and Replace Text in PDF Page Region

You can also find and replace text in a particular region of the page in a PDF document. The following steps show how to define a particular region and then replace text within it.

The following code sample shows how to find and replace text in a particular page region in a PDF using C#.

// load PDF file
Document pdf = new Document("Document.pdf");
// instantiate TextFragment Absorber object
TextFragmentAbsorber TextFragmentAbsorberAddress = new TextFragmentAbsorber();
// search text within page bound
TextFragmentAbsorberAddress.TextSearchOptions.LimitToPageBounds = true;
// specify the page region for TextSearch Options
TextFragmentAbsorberAddress.TextSearchOptions.Rectangle = new Rectangle(100, 100, 200, 200);
// search text from first page of PDF file
pdf.Pages[1].Accept(TextFragmentAbsorberAddress);
// iterate through individual TextFragment
foreach (TextFragment tf in TextFragmentAbsorberAddress.TextFragments)
{
// update text to blank characters
tf.Text = "";
}
// save updated PDF file after text replace
pdf.Save("output.pdf");

C# Replace Text in PDF with Regex

You can also use regular expressions to find and replace the text occurrences matching a particular pattern. For this, you only need to provide a regular expression instead of the plain search phrase and use TextSearchOptions. The following are the steps to do so.

The following code sample shows how to find and replace text in a PDF using regular expressions in C#.

// Open document
Document pdfDocument = new Document("Document.pdf");
// Create TextAbsorber object to find all the phrases matching the regular expression
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("\\d{4}-\\d{4}"); // Like 1999-2000
// Set text search option to specify regular expression usage
TextSearchOptions textSearchOptions = new TextSearchOptions(true);
textFragmentAbsorber.TextSearchOptions = textSearchOptions;
// Accept the absorber for a single page
pdfDocument.Pages[1].Accept(textFragmentAbsorber);
// Get the extracted text fragments
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
// Loop through the fragments
foreach (TextFragment textFragment in textFragmentCollection)
{
// Update text and other properties
textFragment.Text = "New Phrase";
// Set to an instance of an object.
textFragment.TextState.Font = FontRepository.FindFont("Verdana");
textFragment.TextState.FontSize = 22;
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Blue);
textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Green);
}
// Save PDF
pdfDocument.Save("output.pdf");

Free C# Library to Replace Text in PDF

You can get a free temporary license and replace text in PDF files without any limitations.

Explore C# PDF Library

You can explore more advanced features of the C# PDF library using the documentation.

Conclusion

PDF automation is widely adopted these days to manipulate PDF documents from within the web or desktop applications. This article covered a useful PDF automation feature of finding and replacing text in PDF in C#. The step-by-step guide and code samples have shown how to find and replace text in a whole PDF, a particular page in PDF, or a page region.

See Also