
Often you need to locate and replace specific strings within a PDF document. Manually searching and editing each occurrence is inefficient and error‑prone. Using a programmatic find‑and‑replace solution saves time and ensures consistency. This article demonstrates how to perform text search and replacement in PDFs with Java using Aspose.PDF.
- Java Library to Find and Replace Text in PDF
- Find and Replace Text in PDF using Java
- Replace Text on a Particular Page in PDF
- Replace Text using Regular Expression
Java Library to Find and Replace Text in PDF
To perform text search and replacement in PDFs, we use Aspose.PDF for Java3. This library enables creation, editing, and manipulation of PDF files directly from Java applications, including powerful find‑and‑replace capabilities.
You can either download the library or install it using the following Maven configuration.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>22.12</version>
</dependency>
Find and Replace Text in PDF using Java
Replacing a specific string in a PDF involves three main steps: locate the text fragments, modify each fragment, and save the updated document. The process with Aspose.PDF is straightforward:
- Load the PDF file with the Document5 class.
- Create a TextFragmentAbsorber6 instance, initializing it with the target string.
- Apply the absorber to the document pages using Document.getPages().accept(TextFragmentAbsorber)7.
- Retrieve all matching fragments via TextFragmentAbsorber.getTextFragments()8, which returns a TextFragmentCollection9.
- Iterate through each TextFragment10 in the collection and replace the content using TextFragment.setText(String)12.
- Save the modified PDF with Document.save(String)13.
The code snippet below demonstrates a complete find‑and‑replace implementation.
Search and Replace Text on a Particular Page in PDF
If you need to replace text on a single page rather than the entire document, specify the page index when accepting the absorber. This limits the search scope and improves performance.
- Load the PDF using Document15.
- Instantiate TextFragmentAbsorber16 with the text you want to replace.
- Accept the absorber for the desired page via Document.getPages().get_Item(Int pageIndex).accept(TextFragmentAbsorber)17.
- Collect the matching fragments with TextFragmentAbsorber.getTextFragments()18 into a TextFragmentCollection19.
- Loop through each TextFragment20 and update its text using TextFragment.setText(String)22.
- Save the updated file with Document.save(String)23.
The following example shows how to replace text on a specific page.
Java Find and Replace Text in PDF using Regex
Aspose.PDF also supports regular‑expression based searches, allowing you to replace patterns such as email addresses, phone numbers, or custom identifiers.
- Load the PDF with Document24.
- Create a TextFragmentAbsorber25 and set the regular expression pattern as the search string.
- Instantiate TextSearchOptions26 and enable regex by setting its setRegEx(true) property.
- Apply the options using TextFragmentAbsorber.setTextSearchOptions(TextSearchOptions)27.
- Accept the absorber for all pages via Document.getPages().accept(TextFragmentAbsorber)28.
- Retrieve matched fragments with TextFragmentAbsorber.getTextFragments()29 into a TextFragmentCollection30.
- Iterate over each TextFragment31 and replace the matched text using TextFragment.setText(String)33.
- Save the modified PDF using Document.save(String)34.
The code sample below illustrates regex‑based find‑and‑replace.
Free Java Library to Replace Text in PDF
You can get a free temporary license and use Aspose.PDF without limitations for development and testing.
Explore Java PDF Library
Discover additional features and detailed usage examples in the official documentation.
Conclusion
In this article you learned how to programmatically find and replace text in PDF files using Java and Aspose.PDF. You also saw how to limit replacements to a specific page and how to apply regular expressions for pattern‑based text updates.