PDF is a popular document format that provides cross-platform support with a range of features to create rich documents. Furthermore, it supports a variety of elements such as media, forms, annotations, artifacts, etc. Among these elements, the hyperlinks are used quite often to navigate within the PDF, from one PDF to another, to open a URL, etc. So in this article, you will learn how to add and update hyperlinks in PDF files programmatically in Java.
- Java API to Add Hyperlinks in PDF
- Add Hyperlink in a PDF
- Add Hyperlink for an External PDF
- Insert Hyperlink to a Particular Page
- Update a Hyperlink in a PDF
Java API to Add or Update Hyperlinks in PDF
Aspose.PDF for Java is an amazing API for PDF generation and manipulation. Using the API, you can seamlessly create, process, and convert PDF files of simple and complex layouts. We will use this API to add and update hyperlinks in PDF files. You can download the API’s JAR or install it using the following Maven configurations.
Repository:
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
Dependency:
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>22.4</version>
</dependency>
Add Hyperlink in a PDF in Java
The following are the steps to add a hyperlink to a PDF file in Java.
- Create a new PDF or load an existing one using Document class.
- Get the reference of the page where you want to add the hyperlink from Document.getPages() collection.
- Create an object of LinkAnnotation class and set its properties.
- Use LinkAnnotation.setAction() method to assign GoToURIAction object containing URL.
- Add hyperlink to page using Page.getAnnotations().add() method.
- Save PDF using Document.save(String) method.
The following code sample shows how to add a hyperlink in PDF in Java.
Add Hyperlink for an External PDF in Java
You can also navigate to an external PDF file from a PDF using a hyperlink. The following steps demonstrate how to add a hyperlink for an external PDF file in Java.
- Create a new PDF or load an existing one using Document class.
- Get the reference of the page where you want to add the hyperlink from Document.getPages() collection.
- Create an object of LinkAnnotation class and set its properties.
- Use LinkAnnotation.setAction() method to set action to GoToRemoteAction object containing path to external PDF.
- Add hyperlink to page using Page.getAnnotations().add() method.
- Save PDF using Document.save(String) method.
The following code sample shows how to add a hyperlink for an external PDF in Java.
Insert Hyperlink to a Particular PDF Page
In certain cases, you have to navigate from one page to another within a PDF document. The LocalHyperlink class is used to navigate to a particular page in a PDF. The following are the steps to achieve this.
- Create a new PDF or load an existing one using Document class.
- Get the reference of the page where you want to add the hyperlink from Document.getPages() collection.
- Create an instance of TextFragment class to set the text of the hyperlink.
- Create an instance of LocalHyperlink class and use LocalHyperlink.setTargetPageNumber() method to set the target page number.
- Assign LocalHyperlink object using TextFragment.setHyperlink() method.
- Add text to the page using Page.getParagraphs().add(TextFragment) method.
- Save PDF using Document.save(String) method.
The following code sample shows how to add a hyperlink to navigate to a particular page in PDF.
Update a Hyperlink in PDF using Java
You can also retrieve a hyperlink from a PDF file and update its properties. The following are the steps to perform this operation.
- Create a new PDF or load an existing one using Document class.
- Get the reference of the page where the hyperlink is located from Document.getPages() collection.
- Extract the desired LinkAnnotation object by index using Page.getAnnotations().get_item() method.
- Update the required properties of hyperlink and save PDF using Document.save(String) method.
The following code sample shows how to update a hyperlink in PDF using Java.
Get a Free License
You can get a free temporary license in order to use Aspose.PDF for Java without evaluation limitations.
Conclusion
In this article, you have learned how to add different types of hyperlinks in PDF files using Java. Furthermore, you have seen how to retrieve the hyperlinks from existing PDF files and update their properties programmatically. In addition, you can explore more about Java PDF API using the documentation. In case you would have any questions or queries, you can contact us via our forum.