
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\" }]"

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=\\\"https://www.w3.org/1999/xhtml/\\\" xmlns:xfa=\\\"https://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

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
// Get ClientID and ClientSecret from https://dashboard.aspose.cloud/ | |
String clientId = "7042694c-5251-4aba-83c9-e81e7518724f"; | |
String clientSecret = "db246d4742e8cd22e7266c9391992689"; | |
// createPdfApi instance | |
PdfApi pdfApi = new PdfApi(clientSecret,clientId); | |
// input PDF document | |
String name = "PdfWithTable.pdf"; | |
// Load the file from local system | |
File file = new File("/Users/nayyershahbaz/Downloads/" + name); | |
// upload the file to cloud storage | |
FilesUploadResult uploadResponse = pdfApi.uploadFile(name, file, null); | |
// page number where Annotation needs to be added | |
int pageNumber = 1; | |
// create Rectangle object where Annotation is added | |
Rectangle rect = new Rectangle() | |
.LLX(100.) | |
.LLY(600.) | |
.URX(200.) | |
.URY(650.); | |
// Create ListArray of AnnotationFlags | |
List<AnnotationFlags> flags = new ArrayList<>(); | |
flags.add(AnnotationFlags.DEFAULT); | |
// Create TextAnnotation object | |
TextAnnotation textAnnotation = new TextAnnotation(); | |
textAnnotation.setName("Annotation Name"); | |
textAnnotation.setRect(rect); | |
textAnnotation.setFlags(flags); | |
textAnnotation.setHorizontalAlignment(HorizontalAlignment.CENTER); | |
// set the content to be displayed inside Annotation | |
textAnnotation.contents("Hello World..."); | |
// Set the icon for annotation | |
textAnnotation.icon(TextIcon.KEY); | |
textAnnotation.setSubject("Text Box Subj"); | |
textAnnotation.setZindex(1); | |
// the default state of Annotation Object | |
textAnnotation.setState(AnnotationState.COMPLETED); | |
// create ListArray of TextAnnotation | |
List<TextAnnotation> annotations = new ArrayList<>(); | |
// add above created TextAnnotation to List instance | |
annotations.add(textAnnotation); | |
// call the method to add annotation to PDF file | |
AsposeResponse response = pdfApi.postPageTextAnnotations(name, pageNumber, annotations, null, null); | |
assertEquals(200, (int)response.getCode()); |
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.
// Get ClientID and ClientSecret from https://dashboard.aspose.cloud/ | |
String clientId = "a41d01ef-dfd5-4e02-ad29-bd85fe41e3e4"; | |
String clientSecret = "d87269aade6a46cdc295b711e26809af"; | |
// createPdfApi instance | |
PdfApi pdfApi = new PdfApi(clientSecret,clientId); | |
// input PDF document | |
String name = "PdfWithTable.pdf"; | |
// Load the file from local system | |
File file = new File("/Users/nayyershahbaz/Downloads/" + name); | |
// upload the file to cloud storage | |
FilesUploadResult uploadResponse = pdfApi.uploadFile(name, file, null); | |
// page number for input file where annnotation will be added | |
int pageNumber = 1; | |
// rectangular region for the annotation | |
Rectangle rect = new Rectangle() | |
.LLX(100.) | |
.LLY(600.) | |
.URX(200.) | |
.URY(650.); | |
// specify the vertices for the annnotation | |
List<Point> vertices = new ArrayList(); | |
vertices.add(new Point().X(10.).Y(10.)); | |
vertices.add(new Point().X(20.).Y(10.)); | |
vertices.add(new Point().X(10.).Y(20.)); | |
vertices.add(new Point().X(10.).Y(10.)); | |
List<AnnotationFlags> flags = new ArrayList<>(); | |
flags.add(AnnotationFlags.DEFAULT); | |
// create PolyLineAnnotation object | |
PolyLineAnnotation annotation = new PolyLineAnnotation(); | |
annotation.setName("Name"); | |
annotation.setRect(rect); | |
annotation.setFlags(flags); | |
// set the Horizontal alignment of Annotation | |
annotation.setHorizontalAlignment(HorizontalAlignment.CENTER); | |
annotation.contents("Rich Text in the PDF File..."); | |
annotation.setSubject("Subj"); | |
annotation.setZindex(1); | |
annotation.setTitle("Title"); | |
// create color object | |
Color color = new Color(); | |
color.setA(255); | |
color.setR(120); | |
color.setG(140); | |
color.setB(130); | |
// set the internal color for annotation instance | |
annotation.setInteriorColor(color); | |
annotation.setVertices(vertices); | |
// specify the starting style for annotation | |
annotation.startingStyle(LineEnding.OPENARROW); | |
// set the ending style for annotation | |
annotation.endingStyle(LineEnding.SQUARE); | |
List<PolyLineAnnotation> annotations = new ArrayList<>(); | |
annotations.add(annotation); | |
// call the method to add Polyline Annotation to first page of document | |
AsposeResponse response = pdfApi.postPagePolyLineAnnotations(name, pageNumber, annotations, null, null); | |
assertEquals(200, (int)response.getCode()); |
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.
// Get ClientID and ClientSecret from https://dashboard.aspose.cloud/ | |
String clientId = "a41d01ef-dfd5-4e02-ad29-bd85fe41e3e4"; | |
String clientSecret = "d87269aade6a46cdc295b711e26809af"; | |
// createPdfApi instance | |
PdfApi pdfApi = new PdfApi(clientSecret,clientId); | |
// input PDF document | |
String name = "PdfWithTable.pdf"; | |
// Load the file from local system | |
File file = new File("/Users/nayyershahbaz/Downloads/" + name); | |
// upload the file to cloud storage | |
FilesUploadResult uploadResponse = pdfApi.uploadFile(name, file, null); | |
// page number for input file where annotation will be added | |
int pageNumber = 1; | |
// rectangular region for the annotation | |
Rectangle rect = new Rectangle() | |
.LLX(100.) | |
.LLY(600.) | |
.URX(200.) | |
.URY(650.); | |
// specify the vertices for the annotation | |
List<Point> vertices = new ArrayList(); | |
vertices.add(new Point().X(10.).Y(10.)); | |
vertices.add(new Point().X(20.).Y(10.)); | |
vertices.add(new Point().X(10.).Y(20.)); | |
vertices.add(new Point().X(10.).Y(10.)); | |
List<AnnotationFlags> flags = new ArrayList<>(); | |
flags.add(AnnotationFlags.DEFAULT); | |
// create SquigglyAnnotation object | |
SquigglyAnnotation annotation = new SquigglyAnnotation(); | |
annotation.setName("Name"); | |
annotation.setRect(rect); | |
annotation.setFlags(flags); | |
annotation.setHorizontalAlignment(HorizontalAlignment.CENTER); | |
annotation.contents("Rich Text in the PDF File..."); | |
annotation.setSubject("Subj"); | |
annotation.setZindex(1); | |
annotation.setTitle("Title"); | |
annotation.setModified("28/05/2021 00:00:00.000 AM"); | |
// create color object | |
Color color = new Color(); | |
color.setA(155); | |
color.setR(120); | |
color.setG(140); | |
color.setB(130); | |
// set the internal color for annotation instance | |
annotation.color(color); | |
// set annotation points | |
annotation.setQuadPoints(vertices); | |
List<SquigglyAnnotation> annotations = new ArrayList<>(); | |
annotations.add(annotation); | |
// call the method to add Squiggly Annotation to first page of document | |
AsposeResponse response = pdfApi.postPageSquigglyAnnotations(name, pageNumber, annotations, null, null); | |
assertEquals(200, (int)response.getCode()); |
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.
// Get ClientID and ClientSecret from https://dashboard.aspose.cloud/ | |
String clientId = "a41d01ef-dfd5-4e02-ad29-bd85fe41e3e4"; | |
String clientSecret = "d87269aade6a46cdc295b711e26809af"; | |
// createPdfApi instance | |
PdfApi pdfApi = new PdfApi(clientSecret,clientId); | |
// input PDF document | |
String name = "PdfWithTable.pdf"; | |
// Load the file from local system | |
File file = new File("/Users/nayyershahbaz/Downloads/" + name); | |
// upload the file to cloud storage | |
FilesUploadResult uploadResponse = pdfApi.uploadFile(name, file, null); | |
// page number for input file where annotation will be added | |
int pageNumber = 1; | |
// rectangular region for the annotation | |
Rectangle rect = new Rectangle() | |
.LLX(100.) | |
.LLY(100.) | |
.URX(200.) | |
.URY(200.); | |
List<AnnotationFlags> flags = new ArrayList<>(); | |
flags.add(AnnotationFlags.DEFAULT); | |
// create FileAttachmentAnnotation object | |
FileAttachmentAnnotation annotation = new FileAttachmentAnnotation(); | |
annotation.setName("Name"); | |
annotation.setRect(rect); | |
annotation.setFlags(flags); | |
annotation.setHorizontalAlignment(HorizontalAlignment.CENTER); | |
annotation.contents("Rich Text in the PDF File..."); | |
annotation.setSubject("Subj"); | |
annotation.setZindex(1); | |
annotation.setTitle("Title"); | |
annotation.setModified("28/05/2021 00:00:00.000 AM"); | |
// path of attachment file | |
annotation.setFilePath("images.jpeg"); | |
// attachment file name | |
annotation.setFileName("images.jpeg"); | |
// create List instance of FileAttachment | |
List<FileAttachmentAnnotation> annotations = new ArrayList<>(); | |
annotations.add(annotation); | |
// call the method to add FileAttachment Annotation to first page of document | |
AsposeResponse response = pdfApi.postPageFileAttachmentAnnotations(name, pageNumber, annotations, null, null); | |
assertEquals(200, (int)response.getCode()); |
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.
Related articles
We highly recommend you to visit the following articles: