Atualizar links de PDF em Java
Contents
[
Hide
]
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.
- Abra o PDF fonte Document.
- Encontre anotações de link e construa um retângulo de busca de texto a partir de cada área de anotação.
- 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());
}
}
Atualizar cor da borda do link
Use este exemplo quando a cor visível das anotações de link existentes deve ser alterada.
- Abra o PDF fonte Document.
- Itere pelas anotações da página e filtre por objetos LinkAnnotation.
- 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());
}
}
Atualizar o destino de um link da web
Use este exemplo quando um link da web existente deve apontar para um novo URI.
- Abra o PDF fonte Document.
- Encontre anotações de link cuja ação é um GoToURIAction.
- 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());
}
}