How to Add Annotations to a PDF

How to Add Annotations to a PDF

You add text annotations in a PDF file to summarize the content written in the documents and note the critical points and other details. However, you may add text annotations anywhere in the document, and once created, it appears as a popup and as an icon when it is closed. In this blog post, you will learn how to add annotations to a PDF file using this Java API for PDF. In addition, you will go through the methods that help you add text annotations in a file programmatically.

The following points will be covered in this guide:

Java API for PDF - installation

The installation procedure of this library is just one step away. You can 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>
    <classifier>jdk17</classifier>
</dependency>

How to add annotations to a PDF

Now, you can leverage of the methods of this PDF library to implement adding text annotation to a PDF file programmatically.

Please follow the code snippet and the steps mentioned below:

  1. Instantiate an instance of the Document class and load the source PDF file.
  2. Get a page by index by invoking the get_Item method.
  3. Initialize an object of the Rectangle class that represents a rectangle.
  4. Create a new text annotation on the specified page by creating an object of the TextAnnotation class.
  5. setTitle method sets a text that shall be displayed in the title bar of the annotation.
  6. setSubject method sets text representing the description of the object.
  7. setContents method sets annotation text.
  8. Instantiate the constructor of Border object with the textAnnotation object.
  9. Set the width of the border and dash pattern by calling the setWidth and setDash methods.
  10. Invoke the add method that adds an annotation to the collection.
  11. save method will save the annotation file.

Copy & paste the following code into your main file:

You can see the output in the image below:

text-annotation

Read text annotations

Likewise, we can read the text annotations from an existing PDF file by following the steps mentioned below:

  1. Create an instance of the Document class and load the source PDF file.
  2. Call the get_Item method to get a page by index.
  3. Filter annotations using AnnotationSelector.
  4. Invoke the accept method to accept visitor to process annotation.
  5. Call the getSelected method to get the list of selected Annotation objects.
  6. Print the results.

Copy & paste the following code into your main file:

Delete text annotations

In addition, you may follow the steps and the code snippet to delete the text annotations programmatically:

  1. Create an object of the Document class and load the source PDF file.
  2. Filter annotations using AnnotationSelector.
  3. Accept visitor to process annotation by calling the accept method.
  4. Call the getSelected method to get the list of selected Annotation objects.
  5. Loop through all the text annotations and delete annotations by calling the delete method.
  6. Invoke the save method to save the file.

Copy & paste the following code into your main file:

Get a Free License

You can avail a free temporary license to try the API without evaluation limitations.

Summing up

This brings us to the end of this blog post. You have gone through how to add annotations in a PDF file programmatically. In addition, you also have learned how to read and delete text annotations from existing PDF documents using this Java API for PDF. Moreover, you may visit the documentation for further exploration of the API methods.

Moreover, we suggest you follow our Getting Started guide.

Finally, conholdate.com is writing new blog posts. Therefore, please stay in touch for regular updates.

Ask a question

You can let us know about your questions or queries on our forum.

FAQs

Can I annotate a PDF for free?

Please visit this link to know the code snippet and API methods exposed by Java API for PDF.

See Also