Create PDF Links in Java
Contents
[
Hide
]
Aspose.PDF for Java uses LinkAnnotation together with an action object to define link behavior.
Create a launch-action link
Use this example when a link annotation should launch an external file or target.
- Open the source PDF Document and select the target page.
- Create a LinkAnnotation and configure its border and color.
- Assign a LaunchAction and save the document.
public static void createLinkAnnotationLaunchAction(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
Page page = document.getPages().get_Item(1);
LinkAnnotation link = new LinkAnnotation(page, new Rectangle(10, 580, 120, 600, true));
Border border = new Border(link);
border.setWidth(5);
border.setDash(new Dash(1, 1));
link.setBorder(border);
link.setColor(Color.getGreen());
link.setAction(new LaunchAction(document, inputFile.toString()));
page.getAnnotations().add(link);
document.save(outputFile.toString());
}
}
Create a remote go-to link
Use this example when the link should open a page in another PDF document.
- Open the source PDF Document.
- Create a LinkAnnotation on the target page.
- Assign a GoToRemoteAction and save the output file.
public static void createLinkAnnotationGoToRemoteAction(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
Page page = document.getPages().get_Item(1);
LinkAnnotation link = new LinkAnnotation(page, new Rectangle(10, 580, 120, 600, true));
link.setColor(Color.getGreen());
link.setAction(new GoToRemoteAction(inputFile.toString(), 1));
page.getAnnotations().add(link);
document.save(outputFile.toString());
}
}
Create an internal go-to link
Use this example when the link should navigate to another page inside the same PDF document.
- Open the source PDF Document.
- Create a LinkAnnotation and configure its appearance.
- Assign a GoToAction to the destination page and save the document.
public static void createLinkAnnotationGoToAction(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
Page page = document.getPages().get_Item(1);
LinkAnnotation link = new LinkAnnotation(page, new Rectangle(10, 580, 120, 600, true));
Border border = new Border(link);
border.setWidth(5);
border.setDash(new Dash(1, 1));
link.setBorder(border);
link.setColor(Color.getGreen());
if (document.getPages().size() >= 4) {
link.setAction(new GoToAction(document.getPages().get_Item(4)));
} else {
link.setAction(new GoToAction(document.getPages().get_Item(document.getPages().size())));
}
page.getAnnotations().add(link);
document.save(outputFile.toString());
}
}
Create a URI link
Use this example when the link should open a web resource through a URI action.
- Open the source PDF Document.
- Create a LinkAnnotation on the page.
- Assign a GoToURIAction and save the output file.
public static void createLinkAnnotationGoToUriAction(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
Page page = document.getPages().get_Item(1);
LinkAnnotation link = new LinkAnnotation(page, new Rectangle(10, 580, 120, 600, true));
link.setColor(Color.getGreen());
link.setAction(new GoToURIAction("https://docs.aspose.com/pdf/python"));
page.getAnnotations().add(link);
document.save(outputFile.toString());
}
}