In this quick tutorial, you will learn how to highlight a PDF document using Java. It provides the resources required to run the sample code, a list of steps for the application development, and a runnable sample code to highlight words in PDF using Java. You will also learn to customize text highlighting according to your application requirements.
Steps to Highlight Text in PDF using Java
- Set the environment to add Aspose.PDF for Java to highlight PDF
- Load the PDF using the Document class to highlight selected text
- Set the search text into the TextFragmentAbsorber object
- Parse through the given page and find the text fragments
- Create a highlight annotation by using the rectangle enclosing the search text
- Set the color for highlighting the text
- Add this annotation to the target page and save the resultant PDF on the disk
These steps summarize the process to search for a text and highlight PDF file using Java. In the beginning, the source PDF file is loaded and TextFragmentAbsorber class object is used to define and search the required text fragments within a particular page followed by the creation of a highlight annotation. This highlight annotation is customized and added to the collection of annotations on the target page before saving the PDF file on the disk.
Code to highlight PDF document using Java
The code above demonstrates how to highlight in PDF using Java. One of the main tasks is performed by the TextFragmentAbsorber object that defines the text to be searched and then parses the page to collect all the fragments having the desired text. The highlight annotation is defined by using the rectangle around a particular text fragment, which will define the boundary of highlighting color rendered around the text.
This topic has enlightened us on how to highlight PDF file using Java. If you are inclined to learn the process to add comments in a Pdf file, refer to the article on how to insert comment in PDF using Java.