Mettre à jour les liens PDF en Java
Contents
[
Hide
]
Les liens existants peuvent être modifiés en recherchant l’annotation du lien sur une page et en mettant à jour son apparence ou son action.
Mettre à jour la couleur du texte lié
Utilisez cet exemple lorsque la zone de texte couverte par une annotation de lien doit être recolorée.
- Ouvrez le PDF source Document.
- Recherchez des annotations de lien et créez un rectangle de recherche de texte à partir de chaque zone d’annotation.
- Recolorez les fragments de texte correspondants et enregistrez le 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());
}
}
Mettre à jour la couleur de la bordure du lien
Utilisez cet exemple lorsque la couleur visible des annotations de liens existantes doit être modifiée.
- Ouvrez le PDF source Document.
- Parcourez les annotations de la page et filtrez les objets LinkAnnotation.
- Mettez à jour la couleur de l’annotation du lien et enregistrez le 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());
}
}
Mettre à jour une destination de lien Web
Utilisez cet exemple lorsqu’un lien Web existant doit pointer vers un nouvel URI.
- Ouvrez le PDF source Document.
- Recherchez les annotations de lien dont l’action est une GoToURIAction.
- Remplacez l’URI et enregistrez le document mis à jour.
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());
}
}