Annotate PDF

This blog is a continuation of our previous blog where we have highlighted some exciting features of Aspose.PDF Cloud SDK for Java on how to add Highlight or Strikeout Text, and add Line or Circle Annotations in PDF using Java REST API. In this article, we are particularly going to discuss the details about adding Text, Polyline, Squiggly, and Attachment Annotations inside PDF documents using Java programming language.

Aspose.PDF Cloud is a REST API that enables the users to create, manipulate, and render existing PDF files to other supported document formats. The Cloud API can be accessed using the following two approaches:

  • Access the API via cURL commands
  • Access the API in the Java programming language

Let’s further discuss the addition of various Annotations to PDF documents using cURL commands and Java code.

Add Annotations using the cURL command

The cURL commands are one of the easiest and coolest ways to access the REST APIs. So let’s talk about adding the different annotations using the cURL commands. Please note that each API also enables you to access the Cloud storage where files can be stored for processing, so to ensure data integrity, our APIs are only accessible to authorized persons. Therefore, you need to first visit Aspose.Cloud dashboard and if you have a GitHub or Google account, simply Sign Up. Otherwise, click on the Create a new Account button and provide the required information. Now login to the dashboard using credentials, expand the Applications section from the dashboard and scroll down towards the Client Credentials section to see Client ID and Client Secret details.

Now the next step is to generate a JSON Web Token (JWT) so that the APIs are accessible through the command prompt.

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=7042694c-5251-4aba-83c9-e81e7518724f&client_secret=db246d4742e8cd22e7266c9391992689" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Text Annotation

A Text Annotation is an annotation attached to a specific location in a PDF document. When closed, the annotation is displayed as an icon; when opened, it should display a pop-up window containing the note text in the font and size chosen by the reader. To add the Text Annotation, we need to use PostPageTextAnnotation API. Use the following cURL command to add Text Annotation with the Note icon.

 curl -X POST "https://api.aspose.cloud/v3.0/pdf/PdfWithBookmarks.pdf/pages/1/annotations/text" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>" \
-H  "Content-Type: application/json" \
-d "[{  \"Color\": {  \"A\": 0, \"R\": 0xDA,  \"G\": 0xA5, \"B\": 0x20    },  \"Contents\": \"Hello World \",  \"Modified\": \"05/26/2021 03:10:00.000 PM\",    \"Id\": \"1\",    \"Flags\": [      \"Default\"    ],    \"Name\": \"string\",    \"Rect\": {      \"LLX\": 100,      \"LLY\": 800,      \"URX\": 100,      \"URY\": 100    },    \"PageIndex\": 1,    \"ZIndex\": 1,    \"HorizontalAlignment\": \"Center\",    \"VerticalAlignment\": \"Center\",    \"CreationDate\": \"string\",    \"Subject\": \"Subject of Annotation\",    \"Title\": \"Annotation Title\",    \"RichText\": \"string\",    \"State\": \"Undefined\",    \"Open\": true,    \"Icon\": \"Note\"  }]"
Text Annotation in PDF

Image 1:- Text Annotation with Note icon in PDF file.

The sample PDF files used in the above example can be downloaded from the following links

Polyline Annotation

The Polyline annotations are used to hand-draw polylines on a page. They can contain any number of sides defined by the polyline vertices. Polyline annotations with transparent fill color are only selectable around their visible lines. The Polyline annotations can also be added using PostPagePolyLineAnnotations API.

curl -X POST "https://api.aspose.cloud/v3.0/pdf/PdfWithTable.pdf/pages/1/annotations/polyline" \
-H  "accept: application/json" \
-H  "authorization: Bearer " \
-H  "Content-Type: application/json" \
-d "[  {       \"Color\": {      \"A\": 0,      \"R\": 122,      \"G\": 132,      \"B\": 255    },    \"Contents\": \"Hello World...\",    \"Modified\": \"05/26/2021 03:10:00.000 PM\",    \"Id\": \"1\",    \"Flags\": [      \"Default\"    ],    \"Name\": \"Polyline\",    \"Rect\": {      \"LLX\": 100,      \"LLY\": 200,      \"URX\": 150,      \"URY\": 250    },    \"PageIndex\": 1,    \"ZIndex\": 1,    \"HorizontalAlignment\": \"Center\",    \"VerticalAlignment\": \"Center\",    \"CreationDate\": \"05/26/2021 03:10:00.000 PM\",    \"Subject\": \"Subject of Annotation\",    \"Title\": \"Title of Annotation\",    \"RichText\": \"<?xml version=\\\"1.0\\\"?><body xmlns=\\\"http://www.w3.org/1999/xhtml\\\" xmlns:xfa=\\\"http://www.xfa.org/schema/xfa-data/1.0/\\\" xfa:APIVersion=\\\"Acrobat:7.0.0\\\" xfa:spec=\\\"2.0.2\\\" ><span style=\\\"text-decoration:;font-size:10.0pt\\\">Contents</span></body>\",    \"InteriorColor\": {      \"A\": 255,      \"R\": 120,      \"G\": 110,      \"B\": 255    },    \"StartingStyle\": \"Circle\",    \"EndingStyle\": \"OpenArrow\",    \"Intent\": \"PolyLineDimension\",    \"Vertices\": [      {            \"X\": 164.611,            \"Y\": 499.629          },          {            \"X\": 192.858,            \"Y\": 509.857          },          {            \"X\": 226.461,            \"Y\": 493.785          }    ]  }]"

Squiggly Annotation

annotpdf - npm

The squiggly or jagged underlined Annotations help to highlight the information within the document. They can be added using PostPageSquigglyAnnotations API. Given below command helps you to add Squiggly annotation to the PDF document.

curl -X POST "https://api.aspose.cloud/v3.0/pdf/PdfWithTable.pdf/pages/1/annotations/squiggly" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>" \
-H  "Content-Type: application/json" \
-d "[  {    \"Color\": {      \"A\": 255,      \"R\": 120,      \"G\": 123,      \"B\": 150    },    \"Contents\": \"A squiggly Annotation\",    \"Modified\": \"05/26/2021 03:10:00.000 PM\",    \"Id\": \"1\",    \"Flags\": [      \"Default\"    ],    \"Name\": \"First Annotation\",    \"Rect\": {      \"LLX\": 100,      \"LLY\": 300,      \"URX\": 120,      \"URY\": 330    },    \"PageIndex\": 1,    \"ZIndex\": 1,    \"HorizontalAlignment\": \"Center\",    \"VerticalAlignment\": \"Center\",    \"CreationDate\": \"05/26/2021 03:10:00.000 PM\",    \"Subject\": \"Subject \",    \"Title\": \"Title of Squiggly\",     \"Starting\": {      \"X\": 162.663,      \"Y\": 654.5    },    \"StartingStyle\": \"Circle\",    \"Ending\": {      \"X\": 230.845,      \"Y\": 654.5    },    \"EndingStyle\": \"OpenArrow\",    \"InteriorColor\": {      \"A\": 255,      \"R\": 220,      \"G\": 220,      \"B\": 220    },    \"LeaderLine\": 10,    \"LeaderLineExtension\": 5,    \"LeaderLineOffset\": 2.5,    \"ShowCaption\": true,    \"CaptionOffset\": {      \"X\": 7,      \"Y\": 8    },    \"CaptionPosition\": \"Top\",    \"Intent\": \"LineArrow\",\"RichText\": \"string\",    \"QuadPoints\": [      {        \"X\": 100,        \"Y\": 200      }    ]  }]"

Attachment Annotation

Various files can be added as Attachment annotations to the PDF document and to accomplish this requirement, the PostPageFileAttachmentAnnotations API can be used. Execute the following cURL command to attach an existing file to the PDF document. In our example, the file named PdfWithTable.pdf (already available on cloud storage) is used as an attachment.

curl -X POST "https://api.aspose.cloud/v3.0/pdf/PdfWithBookmarks.pdf/pages/1/annotations/fileattachment" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>" \
-H  "Content-Type: application/json" \
 -d "[  {       \"Color\": {      \"A\": 255,      \"R\": 120,      \"G\": 120,      \"B\": 120    },    \"Contents\": \"Content\",    \"Modified\": \"05/26/2021 03:10:00.000 PM\",    \"Id\": \"1\",    \"Flags\": [      \"Default\"    ],    \"Name\": \"FileAttachment\",    \"Rect\": {      \"LLX\": 100,      \"LLY\": 200,      \"URX\": 120,      \"URY\": 2200    },    \"PageIndex\": 1,    \"ZIndex\": 0,    \"HorizontalAlignment\": \"Center\",    \"VerticalAlignment\": \"Top\",    \"CreationDate\": \"05/26/2021 03:10:00.000 PM\",    \"Subject\": \"Subject\",    \"Title\": \"Title\",    \"RichText\": \"string\",    \"Icon\": \"PushPin\",    \"Opacity\": 0,    \"FileDescription\": \"string\",    \"FileName\": \"PdfWithTable.pdf\",    \"FilePath\": \"PdfWithTable.pdf\"  }]"

Add Annotations using Java

Other than cURL commands, another approach to using our Cloud APIs is to access them through programming SDKs. We have developed the programming SDKs for popular programming languages. So, to work with Annotations in PDF using Java language, please try using Aspose.PDF Cloud SDK for Java.

The first step is to install the SKD on the system. The Cloud SDK is available for download over Maven and GitHub. Now add the following details in your pom.xml file to download and use Aspose.Pdf.jar in your Maven build project.

<repositories>
 <repository>
        <id>aspose-cloud</id>
        <name>artifact.aspose-cloud-releases</name>
        <url>https://artifact.aspose.cloud/repo</url>
    </repository>   
</repositories>
<dependencies>
 <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-pdf-cloud</artifactId>
        <version>21.1.0</version>
        <scope>compile</scope>
    </dependency>
 </dependencies>

For more information, please visit How to Install Aspose.Cloud SDKs.

Text Annotations

Given below are the steps to add Text Annotations to the PDF documents.

  • The first step is to create a PdfApi object where we provide ClientID and ClientSecret as arguments
  • Then we create the Rectangle object where the annotation is added
    • LLX - X-coordinate of the lower-left corner.
    • LLY - Y - coordinate of the lower-left corner.
    • URY - X - coordinate of the upper-right corner.
    • URY - Y - coordinate of the upper-right corner.
  • Create TextAnnotation object where we define alignment using setHorizontalAlignment(..) method. Set the subject using setSubject(…) method, the default state using the setState(…) method, etc
  • Then create the object of ArrayList<> of type TextAnnotation and add the above created TextAnnotation object to it
  • Finally, call the postPageTextAnnotations(…) where we pass input PDF file name, PageNumber and the Annotations ArrayList created above as arguments

Polyline Annotations

The Cloud SDK also provides the same capabilities to add the Polyline Annotations to PDF document. Given below are the steps and code snippet to fulfill this requirement.

  • Firstly, we need to create a PdfApi instance and upload the source file to Cloud storage.
  • Create ArrayList of type Point which defines the points at which the polylines will be drawn.
  • The next step is to create a PolyLineAnnotation object where we define the rectangular region and pass Points ListArray to the setVertices(…) method.
  • To set the interior color, use the method setInteriorColor(…) and pass Color instance as an argument.
  • Now call the startingStyle(…) and endingStyle(…) methods to define starting and ending style of annotation. These methods take value from LineEnding enumeration as arguments.
  • Finally, call the postPagePolyLineAnnotations(…) method of PdfApi class to render Annotation inside the PDF document.

Squiggly Annotations

A separate class named SquigglyAnnotation is used to add Squiggly annotation to the PDF document. The given below code snippet can be used to add Squiggly annotations to the PDF file available on Cloud storage.

File Attachment Annotations

To add the file attachment annotation, please try using the following code snippet. The code description is the same as shared in the above sections, except you need to use the FileAttachmentAnnotation object to accomplish this requirement.

Conclusion

In this article, we have discussed the steps and related details for adding Text, Polyline, Squiggly, and FileAttachment Annotations to the PDF document. Since our Cloud SDKs are open source, you can download the complete source code from GitHub. The repository also contains other useful examples on how to use the Java Cloud SDK to create as well as manipulate existing PDF files. Should you have any related queries, please feel free to contact us via Free customer support forums.

We highly recommend you to visit the following articles: