Find and Replace Text in PDF using Java

Find and replace text in PDF Java

In various cases, you may need to find and replace a particular piece of text in the PDF documents. However, searching and updating each occurrence manually may cost you extra time and effort. For such cases, the find and replace option makes your life easier. In this article, you will learn how to find and replace text in PDF documents using Java.

Java API to Find and Replace Text in PDF – Free Download

Aspose.PDF for Java is designed for generating and manipulating PDF files from within the Java applications. The API provides a wide range of basic as well as advanced PDF manipulation features including finding and replacing text. You can either download the API or install it using the following Maven configurations.

<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>21.1</version>    
</dependency>

Find and Replace Text in PDF using Java

In order to replace a particular text in PDF, you would first get all the text fragments matching the search string. Once you have them, simply replace each fragment with updated text one by one.

The following are the steps to find and replace text in a PDF file using Java.

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

Search and Replace Text on a Particular Page in PDF

Instead of finding and replacing text in the whole PDF, you can specify a single page on which you want to replace the text occurrences. In this case, you will accept the TextFragmentAbsorber for a particular page only by specifying the page index.

The following are the steps to search and replace text on a particular page in PDF in Java.

The following code sample shows how to find and replace text on a particular page in PDF using Java.

Find and Replace Text using Regular Expressions in PDF

You can also specify a regular expression to search the text that matches a particular pattern such as emails, SSNs, etc. The following are the steps to define and use a regular expression to search and replace text in PDF using Java.

The following code sample shows how to find and replace text in PDF using regular expression.

Get a Free API License

You can try the API without evaluation limitations for free. Get a free temporary license now.

Conclusion

In this article, you have learned how to find and replace text in PDF using Java. Furthermore, you have seen how to use a regular expression to search and replace text following a particular pattern. You can explore more about the Java PDF API using documentation.

See Also