This quick guide describes how to find and replace text in PDF using C# with the help of detailed steps and a runnable code. It assists to configure the environment and then provides a step-by-step process to replace text in a PDF using C#. Once the file is updated, you can save it back on the disk in the original format i.e. PDF or as a DOCX, Excel, HTML, etc. to name a few.
Steps to Find and Replace Text in PDF using C#
- Configure the project to use Aspose.PDF for .NET using the NuGet package manager
- Create or load a PDF file containing sample text using the Document class object
- Using the TextFragmentAbsorber class object, set the text to be searched
- For all the pages in the input PDF file, accept the text absorber
- Get the collection of fragments where text is extracted from the loaded PDF file
- Parse through all the fragments and set new text
- Save the updated PDF file
These steps describe how in a PDF search and replace text using C#. A new file with some sample text is created however you may load an existing PDF file whose text is to be replaced. There is a variety of options available to search text in the PDF like ignoring shadow text, limiting the search to page bound, etc.
Code to Replace Text in a PDF using C#
This code uses TextFragmentAbsorber and TextFragment for text to find and replace in PDF using C#. You can not only replace the text but also change its font family, size, foreground color, and background color in the resultant PDF file. Options are also available to replace text in the entire PDF at once or replace text based on the regular expression.
In this topic we have learned to find and replace text in PDF, however, if you want to learn to split PDF files by pages, refer to the article on how to split PDF file by pages in C#.