Interactive Annotations using Java
Contents
[
Hide
]
Add a link annotation
- Load the source PDF document and search the first page for the target text.
- Use the matched text rectangle to create a
LinkAnnotationand assign the destination URI. - Add the annotation to the page and save the updated PDF.
public static void linkAdd(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("file");
document.getPages().get_Item(1).accept(textFragmentAbsorber);
TextFragment phoneNumberFragment = textFragmentAbsorber.getTextFragments().get_Item(1);
LinkAnnotation linkAnnotation = new LinkAnnotation(
document.getPages().get_Item(1), phoneNumberFragment.getRectangle());
linkAnnotation.setAction(new GoToURIAction("www.aspose.com"));
document.getPages().get_Item(1).getAnnotations().add(linkAnnotation);
document.save(outputFile.toString());
}
}