How to Insert Comment in PDF using Java

This article explains how to insert comment in PDF using Java. It provides all the information required to establish the environment, a list of steps, and a runnable sample code to add notes to PDF using Java. It will guide you to add and customize a text annotation and then add it to the annotations collection in the selected PDF page before saving it on the disk.

Steps to Add Comments to PDF using Java

  1. Establish the environment to add Aspose.PDF for Java to add annotation
  2. Create or load a PDF to add annotation using the Document class object
  3. Create an object of the TextAnnotation class object and set its properties
  4. Create a border object and add it to the annotation object
  5. Add the annotation to the annotations collection of the selected page
  6. Save the resultant PDF file having annotation in it

The above steps describe how to add comments to a PDF using Java where the environment configuration is provided followed by the creation of a PDF file and then a text annotation is created and added to the PDF file. All the necessary classes are identified like TextAnnotation class is used for creating the comment and the Border class is used to create a border around the comments. In the final step, this annotation is added to the annotations collection using the getAnnotations().add() method of the Page class.

Code to Add Comment in PDF using Java

The aforementioned example demonstrates the process to insert comment in PDF using Java. The TextAnnotation class is used to set different properties whereas you may set other properties as well like set text horizontal and vertical alignment, reply type, opacity, margin, and hyperlink to name a very few. We have set the border of the annotation also however you may set its rectangle, height, color, and margins if required.

In this article, we have learned the process to put comments in PDF using Java. If you want to learn the process to add a watermark to a PDF, refer to the article on how to add watermark to PDF using Java.

 English