Add link annotation

On this page

Link annotation adds a hyperlink to document as shown in the picture below:

You can use the following methods to set the properties of the LinkAnnotation class:

  • setUrl() defines link to a web resource
  • setPoints() defines the annotation positions as an array of points.

To add a link annotation, follow these steps:

  1. Instantiate the Annotator class. Specify the input document path or stream.
  2. Instantiate the LinkAnnotation class. Specify the appropriate properties (position, page number, etc).
  3. Call the add() method. Specify the LinkAnnotation class.
  4. Call the save() method. Specify the output document path or stream.

The following code snippet shows how to add LinkAnnotation to the document:

// This example demonstrates adding link annotation.

// Create an instance of Annotator class
Annotator annotator = new Annotator("inputPath");
try {
    // Create an instance of Reply class and add comments
    Reply reply1 = new Reply();
    reply1.setComment("First comment");
    reply1.setRepliedOn(Calendar.getInstance().getTime());

    Reply reply2 = new Reply();
    reply2.setComment("Second comment");
    reply2.setRepliedOn(Calendar.getInstance().getTime());

    java.util.List<Reply> replies = Arrays.asList(reply1, reply2);
    
    List<Point> points = Arrays.asList(new Point(80, 730), new Point(240, 730), new Point(80, 650), new Point(240, 650));                
    
    // Create an instance of LinkAnnotation class and set options
    LinkAnnotation link = new LinkAnnotation();
    link.setCreatedOn(Calendar.getInstance().getTime());
    link.setMessage("This is link annotation");
    link.setOpacity(0.7);
    link.setPageNumber(0);
    
    link.setPoints(points);               
    
    link.setReplies(replies);
    link.setUrl("https://www.google.com");
    
    // Add annotation and save to file
    annotator.add(link);
    annotator.save("outputPath");
} finally {
    if (annotator != null) {
        annotator.dispose();
    }
}

On this page