Actualizar enlaces PDF en Java
Contents
[
Hide
]
Los enlaces existentes pueden editarse encontrando la anotación de enlace en una página y actualizando ya sea su apariencia o su acción.
Actualizar el color del texto enlazado
Utilice este ejemplo cuando el área de texto cubierta por una anotación de enlace deba recolorarse.
- Abra el PDF de origen
Document. - Encuentre anotaciones de enlace y cree un rectángulo de búsqueda de texto a partir de cada área de anotación.
- Recoloree los fragmentos de texto coincidentes y guarde el 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());
}
}
Actualizar color del borde del enlace
Utilice este ejemplo cuando se deba cambiar el color visible de las anotaciones de enlace existentes.
- Abra el PDF de origen
Document. - Itere a través de las anotaciones de la página y filtre por
LinkAnnotationobjetos. - Actualice el color de la anotación de enlace y guarde el 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());
}
}
Actualizar el destino de un enlace web
Utilice este ejemplo cuando un enlace web existente debe apuntar a un nuevo URI.
- Abra el PDF de origen
Document. - Busque anotaciones de enlace cuya acción es una
GoToURIAction. - Reemplace el URI y guarde el documento actualizado.
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());
}
}