Add or Remove Annotations in PDF Files using Java

Add or Remove Annotation in PDF

Annotations in PDF files are used to elaborate the content. An annotation could be a note, popup, or a graphical object such as an arrow, line, etc. Since PDF files are not editable, annotations let you provide additional information. In this article, you will learn how to work with annotations in PDF files programmatically. Particularly, you will learn how to add or remove annotations in PDF files using Java.

Java API to Add or Remove PDF Annotations

In order to add or remove PDF annotations, we will use Aspose.PDF for Java. It is a feature-rich API that lets you generate, edit and convert PDF files using Java. In addition, the API allows you to work with a wide range of PDF annotations seamlessly. 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.3</version>
</dependency>

Add Annotation to PDF using Java

Aspose.PDF for Java supports a number of annotations including but not limited to text, line, circle, square, highlight, etc. To add each annotation type, Aspose.PDF for Java provides a separate class. For example, LineAnnotation class is used to add a line whereas HighlightAnnotation class is used to add a highlight annotation.

Steps to Add an Annotation to PDF using Java

For demonstration, let’s add a text annotation to a PDF file. The following are the steps along with API references to add a text annotation to PDF using Java.

The following code sample shows how to add a text annotation to PDF using Java.

Remove Annotations from PDF using Java

For removing the annotations from a page in PDF, Aspose.PDF for Java provides the following options:

  • Delete a particular annotation on a page
  • Delete all annotations on a page

Remove a Particular PDF Annotation

The following are the steps to remove an annotation from a PDF file using Java.

The following code sample shows how to remove a particular annotation from a page in PDF using Java.

Remove all PDF Annotations

The following are the steps to remove all the annotations on a page in PDF using Java.

The following code sample shows how to remove all annotations on a page in PDF using Java.

Get a Free API License

You can try Aspose.PDF for Java for free without evaluation limitations using a temporary license.

Conclusion

In this article, you have learned how to work with annotations in PDF files. Particularly, you have seen how to add or remove annotations in PDF files using Java. You can explore more about Aspose.PDF for Java using documentation. In case you would have any questions or queries, please let us know via our forum.

See Also