Anotaciones interactivas usando Java

Agregar una anotación de enlace

  1. Cargue el documento PDF de origen y busque en la primera página el texto objetivo.
  2. Utilice el rectángulo de texto coincidente para crear un LinkAnnotation y asignar la URI de destino.
  3. Añada la anotación a la página y guarde el PDF actualizado.
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());
    }
}