Manage Annotations in PDF via Java

Managing annotations in PDF document. Use Aspose.PDF for Java to modify PDF files programmatically

How to Manage Annotations Using Java Library

In order to add Text Annotation, we’ll use Aspose.PDF for Java API which is a feature-rich, powerful and easy to use conversion API for Java platform. You can download its latest version directly from Maven and install it within your Maven-based project by adding the following configurations to the pom.xml.

Repository

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java AP</name>
    <url>https://releases.aspose.com/java/repo/</url>
</repository>

Dependency

<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>

Create Annotations in PDF document via Java


You need Aspose.PDF for Java to try the code in your environment.

  1. Load PDF in an instance of Document class.
  2. Create an annotation that you want to add to the PDF.
  3. Add the annotation to the Page object’s Annotations collection.
  4. Save the PDF file.

PDF Text Annotation - Java

Example: Java


    Document document = new Document(_dataDir + "sample.pdf");
    Page page = document.getPages().get_Item(1);
    Rectangle rect = new Rectangle(200, 750, 400, 790);
    TextAnnotation textAnnotation = new TextAnnotation(page, rect);

    textAnnotation.setTitle("Aspose User");
    textAnnotation.setSubject("Sample Subject");
    textAnnotation.setState (AnnotationState.Accepted);
    textAnnotation.setContents("Sample contents for the annotation");
    textAnnotation.setOpen(true);
    textAnnotation.setIcon(TextIcon.Circle);

    Border border = new Border(textAnnotation);
    border.setWidth(5);
    border.setDash(new Dash(1, 1));
    textAnnotation.setBorder(border);
    textAnnotation.setRect(rect);

    page.getAnnotations().add(textAnnotation);
    document.save(_dataDir + "sample_textannot.pdf");