Atualizar links de PDF em Java

Links existentes podem ser editados encontrando a anotação de link em uma página e atualizando sua aparência ou sua ação.

Atualizar cor do texto vinculado

Use este exemplo quando a área de texto coberta por uma anotação de link precisar ser recolorizada.

  1. Abra o PDF fonte Document.
  2. Encontre anotações de link e construa um retângulo de busca de texto a partir de cada área de anotação.
  3. Recolore os fragmentos de texto correspondentes e salve o documento.
public static void linkAnnotationUpdateTextColor(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Link) {
                TextFragmentAbsorber absorber = new TextFragmentAbsorber();
                Rectangle rect = annotation.getRect();
                rect.setLLX(rect.getLLX() - 2);
                rect.setLLY(rect.getLLY() - 2);
                rect.setURX(rect.getURX() + 2);
                rect.setURY(rect.getURY() + 2);
                absorber.setTextSearchOptions(new TextSearchOptions(rect));
                absorber.visit(document.getPages().get_Item(1));
                for (TextFragment textFragment : absorber.getTextFragments()) {
                    textFragment.getTextState().setForegroundColor(Color.getRed());
                }
            }
        }

        document.save(outputFile.toString());
    }
}

Use este exemplo quando a cor visível das anotações de link existentes deve ser alterada.

  1. Abra o PDF fonte Document.
  2. Itere pelas anotações da página e filtre por objetos LinkAnnotation.
  3. Atualize a cor da anotação de link e salve o documento.
public static void linkAnnotationUpdateBorder(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Link && annotation instanceof LinkAnnotation) {
                LinkAnnotation linkAnnotation = (LinkAnnotation) annotation;
                linkAnnotation.setColor(Color.getRed());
            }
        }

        document.save(outputFile.toString());
    }
}

Use este exemplo quando um link da web existente deve apontar para um novo URI.

  1. Abra o PDF fonte Document.
  2. Encontre anotações de link cuja ação é um GoToURIAction.
  3. Substitua o URI e salve o documento atualizado.
public static void linkAnnotationUpdateWebDestination(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Link && annotation instanceof LinkAnnotation) {
                LinkAnnotation linkAnnotation = (LinkAnnotation) annotation;
                if (linkAnnotation.getAction() instanceof GoToURIAction) {
                    GoToURIAction action = (GoToURIAction) linkAnnotation.getAction();
                    action.setURI("https://www.aspose.com");
                }
            }
        }
        document.save(outputFile.toString());
    }
}