PDF Annotation

The content inside a PDF page is difficult to edit, but the PDF specification defines a complete set of objects that can be added to PDF pages without changing the page content. These objects are called annotations, and their purpose ranges from marking up page content to implementing interactive features such as forms.

Most PDF viewers allow the creation and editing of various annotation types, e.g. text highlights, notes, lines, or shapes, and regardless of the annotation types that are created, PDF viewers conforming to the PDF specification should also support rendering for all annotation types. However, when dealing with a large set of documents, the manual process of adding annotations gets cumbersome and therefore, a programming API is a viable solution. We are further going to discuss the usage of Aspose.PDF Cloud SDK for Java to add Annotations to PDF documents.

Installation of SDK

Aspose.PDF Cloud SDK for Java is a programming API that enables Java programmers to create, manipulate as well as transform existing PDF documents to other supported document formats. 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>http://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.

Furthermore, please note that in order to ensure user privacy and data integrity, all our APIs are only accessible to authorized persons. Therefore, you need to first visit Aspose.Cloud dashboard and if you have 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 and expand the Applications section from the dashboard and scroll down towards the Client Credentials section to see Client ID and Client Secret details.

Strikeout text using Java

Sometimes we have a requirement to mark certain content inside the document as obsolete without removing it from the document. The Strikethrough (a.k.a strikeout) is represented by words with a horizontal line through their center. It implies that the text is wrong or obsolete and was recently deleted or marked as such. We can set the settings of the strikethrough annotations using the strikethroughSettings property. The strikethroughSettings property is used to set the color, opacity, author, subject, modifiedDate, and isLocked properties of the strikethrough annotations.

Add Highlight Annotation

Highlighting the content inside PDF is essential because it allows us to draw attention to important information within the document. Highlighting is effective because it first asks the reader to pick out the important parts, and then gives an effective way to review that information later. Our Cloud API provides the capabilities to highlight content inside PDF documents. Given below are the steps to add highlight annotations in a PDF file.

  • First of all, create an instance of PdfApi while providing ClientSecret and ClientId details
  • We create a File instance to load a PDF file from the local system and then call uploadFile(…) method of PdfApi class to upload the document to cloud storage
  • Now create an instance of the Rectangle class to specify the rectangular region where annotation can be added
  • Then create an ArrayList of type Point to hold the information regarding points to draw the HighlightAnnotation
  • Create an object of HighlightAnnotation class and set Name, Rectangular region, Subject, Title, QuadPoints, Annotation modify date
  • The next step is to create a List of HighlightAnnotation type and add HighlightAnnotation object created above to this list
  • Finally, call postPageHighlightAnnotations(…) method of PdfApi class and pass source PDF, page number, and Annotations list as arguments
Highlight Annotation preview

Image 1:- Highlight Annotation in PDF file.

You may download the sample files used in above example from following links

How to add Line Annotation

Similar to adding strikeout and highlight annotations, the API also enables you to add Line Annotations within PDF documents. We have a class named LineAnnotation to accomplish this requirement. Given below are further details on how to fulfill this requirement.

  • First of all, create an instance of PdfApi while providing ClientSecret and ClientId details
  • Optionally, we create a File instance to load a PDF file from the local system and then call uploadFile(…) method of PdfApi class to upload the document to cloud storage. In case the file is already available on Cloud storage, we can use it
  • The rest of the object initialization properties are the same as mentioned above for HighlithAnnotation except we need to create an object of LineAnnotation class and set starting and ending point of a line using setStarting(…) and setEnding(…) methods

Adding Circle Annotation in PDF

Circle annotations are also commonly used inside PDF documents and the API very well supports this annotation type. In order to add circle annotations, please try using CircleAnnotation class. The specified below code can be used to fulfill this requirement.

Circle Annotation in PDF

Image 2:- Circle Annotation in PDF file.

The input PDF document and resultant PDF, as shown above, can be downloaded from the following links:

Quick Tip

Use our Free online PDF Annotation App to remove all annotations from your PDF in one click.

Conclusion

In this blog, we have discussed various Annotations types currently supported by Aspose.PDF Cloud SDK for Java. Apart from Annotations, the API supports a plethora of other exciting features related to PDF format. For more information, we recommend you further explore the Developer Guide.

Please be informed that we always strive to serve the customers in the best possible way and acknowledge their feedback and suggestions. So, in case you encounter any issue while using the API or you have any related query, please feel free to contact us via our Free Support forum.