Update PDF Links in Java
Contents
[
Hide
]
Existing links can be edited by finding the link annotation on a page and updating either its appearance or its action.
Update linked text color
Use this example when the text area covered by a link annotation should be recolored.
- Open the source PDF Document.
- Find link annotations and build a text search rectangle from each annotation area.
- Recolor the matched text fragments and save the document.
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());
}
}
Update link border color
Use this example when the visible color of existing link annotations should be changed.
- Open the source PDF Document.
- Iterate through the page annotations and filter for LinkAnnotation objects.
- Update the link annotation color and save the document.
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());
}
}
Update a web link destination
Use this example when an existing web link should point to a new URI.
- Open the source PDF Document.
- Find link annotations whose action is a GoToURIAction.
- Replace the URI and save the updated document.
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());
}
}