Java PDF External Hyperlink Example

In our previous blog post, we discussed how to create an internal reference in PDF document using bookmarks in Java. We created an Anchor object, added some paragraph and finally linked back to the Anchor object from a different page in the PDF document. This can be useful when you want your PDF to be well connected. In many cases however you will also want to create an external reference / hyperlink in a PDF document. 

In this post, we will show a simple Java example that explains how to create an external hyperlink in a PDF document. We will be using the same example that we used yesterday for internal reference and just expand it to hold an external hyperlink example. Follow the procedure below to create external hyperlinks; 

1) Define an Anchor object with the text "Take me to Thinktibits" (As an example). Once this is done, you can invoke the "setReference" method of the Anchor object to create the web reference to which you want to take the user to, when the hyperlink is clicked. The Java code is provided below
/* Define a New Anchor for Creating External Hyperlink in PDF Document */
Anchor external_hyperlink=new Anchor("Take me to Thinktibits");
external_hyperlink.setReference("http://thinktibits.blogspot.com");


2) Create a Paragraph object that will act as a holder for the Anchor object. Add the Anchor object to the Paragraph object and the Paragraph object to the Document object. As simple as it is, you have created an external hyperlink in your PDF document!

Paragraph external_link_Holder = new Paragraph();
external_link_Holder.add(external_hyperlink); /* Add Anchor to Paragraph */
iText_Hyperlink_Example.add(external_link_Holder); /* Add Paragraph to Document */

If you had noted the code, we used the same "setReference" method to add an internal reference as well. So, what is the key differentiator between internal and external references? It is the presence of a leading '#' symbol. The PDF document will contain a new link that will take you to Thinktibits, a screenshot of which is shown below


image
External Hyperlink placed in PDF document using iText


When you click on this link, Adobe Reader will throw the standard security warning message, a sample provided below


image
Adobe Reader Security Warning on Clicking PDF Hyperlink



That completes our step by step guide on creating hyperlink in PDF documents. The complete Java Code is provided below

import java.io.FileOutputStream;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.*;
public class hyperlink {
public static void main(String[] args) {
try {
Document iText_Hyperlink_Example = new Document();
PdfWriter.getInstance(iText_Hyperlink_Example, new FileOutputStream("Create_Hyperlink_In_PDF.pdf"));
iText_Hyperlink_Example.open();   Anchor source_point = new Anchor("Source Bookmark Point in PDF");
source_point.setName("IDSTART");           
Paragraph Anchor_Holder = new Paragraph();
Anchor_Holder.add(source_point);
iText_Hyperlink_Example.add(Anchor_Holder);
for(int i=1; i<100; i++){
iText_Hyperlink_Example.add(new Paragraph("Filler Paragraph Texts"));
iText_Hyperlink_Example.add(new Paragraph(" "));
}
Anchor linking_point = new Anchor("Click me to go to the Bookmark");
linking_point.setReference("#IDSTART");
iText_Hyperlink_Example.add(linking_point);
/* Define a New Anchor for Creating External Hyperlink in PDF Document */
Anchor external_hyperlink=new Anchor("Take me to Thinktibits");
external_hyperlink.setReference("http://thinktibits.blogspot.com");
Paragraph external_link_Holder = new Paragraph();
external_link_Holder.add(external_hyperlink); /* Add Anchor to Paragraph */
iText_Hyperlink_Example.add(external_link_Holder); /* Add Paragraph to Document */       
iText_Hyperlink_Example.close();
}
catch (Exception i1) {
i1.printStackTrace();
}
}
}

No comments: