Add or Update Hyperlinks in PDF using Java

Add or Update Hyperlinks in PDF using Java

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.

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>

The following are the steps to add a hyperlink to a PDF file in Java.

The following code sample shows how to add a hyperlink in 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.

The following code sample shows how to add a hyperlink for an external PDF in Java.

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.

The following code sample shows how to add a hyperlink to navigate to a particular page in PDF.

You can also retrieve a hyperlink from a PDF file and update its properties. The following are the steps to perform this operation.

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.

See Also