Highlight PDF using Annotations in Java

It is often needed to highlight important areas of your documents on purpose. As a developer, you can automate highlighting within your applications. In this article, you will learn how to highlight text and any area in PDF files using Java. Additionally, there will be several properties of highlighting that can be adjusted according to the requirement.

The following topics are covered below:

Highlight Text in PDF - Programmatically

Java API to Highlight in PDF

GroupDocs.Annotation for Java is the API that allows easy manipulation and automation of annotations in documents within your Java-based applications. We will use this API to highlight text in the PDF file.

Download or Configure

You may download the JAR file from the downloads section, or just get the latest repository and dependency configurations for the pox.xml of your maven-based Java applications.

<repository>
	<id>GroupDocsJavaAPI</id>
	<name>GroupDocs Java API</name>
	<url>http://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
        <groupId>com.groupdocs</groupId>
        <artifactId>groupdocs-annotation</artifactId>
        <version>21.7</version> 
</dependency>

Highlight in PDF using Java

The following are the steps to highlight text or any area in PDF using Java.

  • Load the PDF document using Annotator class.
  • Define list of Points to select the area of highlight.
  • Create the HighlightAnnotation object.
  • Define the highlight properties like color, opacity, and page number.
  • Add the defined highlighting to the loaded PDF document using add method.
  • Save the annotated PDF using save method.

Note: You may change the highlight color, opacity, and other properties.

The following Java code shows how to highlight the text in PDF programmatically.

Here is the output of the above code.

Highlight Text in PDF - Programmatically

Get a Free API License

You can get a free temporary license in order to use the API without the evaluation limitations.

Conclusion

To conclude, we have discussed how to programmatically add highlight annotation in PDF files using Java. Additionally, we can change the highlight color, opacity, and other properties with ease. Many different types of annotations are available through the API. These annotations can be added in a similar way using the same API. To learn about the API, visit the documentation. For queries, contact us via the forum.

See Also