In this short how-to tutorial, you will learn how to create hyperlink in PDF using Java. It provides all the details to establish the environment, a list of steps to be followed, and a runnable sample code to insert link in PDF using Java. You will get all the details to customize the hyperlink along with the location and other parameters.
Steps to Embed Link in PDF using Java
- Set the IDE to use Aspose.PDF for Java to add hyperlinks
- Open the target PDF file using the Document class object and get access to the desired page
- Create a link annotation and add it to the target page
- Create a free text annotation at the sample place and add it to the target page as well
- Save the resultant PDF file having a hyperlink in it on the disk
These steps elaborate the process of how to add link in PDF using Java. The process is quite simple where we have to add two types of annotations to the default collection of the target page of a PDF. The first annotation is a link annotation where an action is defined to open a URL and in the second annotation, we define the text that is to be displayed at the same location where the link annotation is added to make the hyperlink effective and visible in the form of text.
Code to Add Hyperlink in PDF using Java
The above code demonstrates the process to insert hyperlink in PDF using Java. It uses LinkAnnotation class object to create link annotation where you can set a number of properties like active rectangular area for the link, border, width, action, appearance state, margin, and name to list a very few. Similarly for text annotation, the FreeTextAnnotation class object is used where we need to set the rectangular area similar to the link annotation active area, font, size, color, contents, alignment, callout, flags, intent, justification, and opacity, etc.
In this article, we have learned the process to create hyperlink in PDF using Java. If you want to learn the process to add a watermark in a PDF, refer to the article on how to add watermark to PDF using Java.