Java Itext PDF Add Hyperlink Example

Can't really think of a webpage without a hyperlink? Hyperlink are so basic entities and are a must have feature for any kind of document HTML, Microsoft Word and so on. PDF is no exception here and the good news is that iText supports creating hyperlinks in PDF documents. In this post, we will explain how to create a hyperlink in a PDF document using iText and Java. Again, this tutorial is targeted towards beginners of iText and we will present a working Java code example, that teaches how to add hyperlinks in PDF documents. 

When talking about hyperlinks, there can be internal hyperlinks which take you to a specific section in a document and external hyperlinks that take you to a webpage. In this post, we will provide a working example for creating internal hyperlink through bookmarking inside a PDF document using iText. External hyperlinks will be covered in a later post. To create an internal reference, we will have to create a bookmark and then create a link to the bookmark. To do this in iText, we will make use of an object called Anchor

1) Create a Document object and a PDFWriter object as in the previous tutorials. Also, open the document for adding references. We have covered the explanation for this in our earlier tutorials and so, I will not be covering them in this one.
Document iText_Hyperlink_Example = new Document();
PdfWriter.getInstance(iText_Hyperlink_Example, new FileOutputStream("Create_Hyperlink_In_PDF.pdf"));
iText_Hyperlink_Example.open();


2) We now create an Anchor object (com.itextpdf.text.Anchor). This object will serve as the link back point for the hyperlink we will be creating later on. We will also set a name for this Anchor object using "setName" method, which will provide an "id" for this object. This id will serve as a mechanism for us to link the Anchor object from an external reference.
            Anchor source_point = new Anchor("Source Bookmark Point in PDF");
source_point.setName("IDSTART");


3) We create a Paragraph object that will act as an envelope for this Anchor object. We add the Anchor object to the Paragraph object by using the "add" method of Paragraph. We will also add the Paragraph object to the Document object which will inturn be picked by the PdfWriter object for writing to the PDF file.
            Paragraph Anchor_Holder = new Paragraph();
Anchor_Holder.add(source_point);
iText_Hyperlink_Example.add(Anchor_Holder);

4) We will run a for loop in Java to add some random text. This is just to fill the pages for us to place the link on a different page (to see the effect of clicking the link). So, create a for loop as shown below to fill some pages in the PDF. /* This step is purely optional */
             for(int i=1; i<100; i++){
iText_Hyperlink_Example.add(new Paragraph("Filler Paragraph Texts"));
iText_Hyperlink_Example.add(new Paragraph(" "));
}


5) We now define an anchor object that will internally reference the bookmark we created earlier in the PDF document. We can add this through a Paragraph object or directly through the document object.The internal reference java code snippet is provided below
            Anchor linking_point = new Anchor("Click me to go to the Bookmark");
linking_point.setReference("#IDSTART");
iText_Hyperlink_Example.add(linking_point);


That is it, when you open the resulting PDF document, you will be glad to note that you created your first internal hyperlink in the PDF. A screenshot of the reference created is shown below (click this and it will take you to the beginning of the document)

hyperlink
Add Hyperlinks in PDF Document using Itext - Example Output



The complete Java code to create an internal hyperlink in a PDF document using iText 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);
iText_Hyperlink_Example.close();
}
catch (Exception i1) {
i1.printStackTrace();
}
}
}

1 comment:

  1. Please share links related to accessing External hyperlinks in existing PDF.

    ReplyDelete