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 Work with PDF Annotations
- Add Annotations to PDF using Java
- Remove Annotations from PDF using Java
- Get a Free API License
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.
- First, load the PDF document using Document class.
- Create an object of TextAnnotation class to add text annotation.
- Set properties of the annotation such as title, subject, etc.
- Set border of the annotation using Border class.
- Add annotation to the document using Document.getPages().get_Item(int).getAnnotations().add(Annotation) method.
- Finally, save the updated PDF using Document.save(string) method.
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.
- First, load the PDF document using Document class.
- Remove desired annotation by index using Document.getPages().get_Item(int).getAnnotations().delete(int) method.
- Finally, save the updated PDF using Document.save(string) method.
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.
- First, load the PDF document using Document class.
- Remove all annotations using Document.getPages().get_Item(int).getAnnotations().delete() method.
- Finally, save the updated PDF using Document.save(string) method.
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.