PeopleCode Create Hyperlink Example

In our previous post, we saw how to create a bookmark in a PDF document using PeopleCode and refer to that bookmark as an internal hyperlink within the PDF document. In this post, we will see how to create an external hyperlink in a PDF document using PeopleCode. This will be exactly on the same lines as of the Internal hyperlink example, and hence make sure that you read that post first before continuing on this one.The step by step guide is provided below; 

1) Create the Document Object, PdfWriter Object and open the Document object for adding a hyperlink.
Local JavaObject &Obj_createhyperlinkedpdf_l = CreateJavaObject("com.lowagie.text.Document");
Local JavaObject &obj_writePDFoutput_l = GetJavaClass("com.lowagie.text.pdf.PdfWriter").getInstance(&Obj_createhyperlinkedpdf_l, CreateJavaObject("java.io.FileOutputStream", "ExternalHyperlinkFromPeopleCode.pdf", True));
&Obj_createhyperlinkedpdf_l.open();

2) We will create a link to Google search engine from PeopleCode. Create an Anchor object with the text "Take me to Google" and use "setReference" method to set the http reference to the external URL. Create a Paragraph object and add the Anchor object to the Paragraph object.

Local JavaObject &Obj_javaanchor_l = CreateJavaObject("com.lowagie.text.Anchor", "Take me to Google");
&Obj_javaanchor_l.setReference("http://www.google.com");
Local JavaObject &obj_paragraph_l = CreateJavaObject("com.lowagie.text.Paragraph");
&obj_paragraph_l.add(&Obj_javaanchor_l);

3) Following the wrapper techniques as explained in the previous post, add the Paragraph Object to the Document Object and close the Document object after adding.

Local JavaObject &obj_reflection_l = CreateJavaObject("image");
&Obj_createhyperlinkedpdf_l = &obj_reflection_l.addParagraphtoDocument(&Obj_createhyperlinkedpdf_l, &obj_paragraph_l);
&Obj_createhyperlinkedpdf_l.close();

That completes the tutorial to create a hyperlink that references an external URL in PeopleCode using iText Java API. Complete PeopleCode example is provided below

Local JavaObject &Obj_createhyperlinkedpdf_l = CreateJavaObject("com.lowagie.text.Document");
Local JavaObject &obj_writePDFoutput_l = GetJavaClass("com.lowagie.text.pdf.PdfWriter").getInstance(&Obj_createhyperlinkedpdf_l, CreateJavaObject("java.io.FileOutputStream", "ExternalHyperlinkFromPeopleCode.pdf", True));
&Obj_createhyperlinkedpdf_l.open();
Local JavaObject &Obj_javaanchor_l = CreateJavaObject("com.lowagie.text.Anchor", "Take me to Google");
&Obj_javaanchor_l.setReference("http://www.google.com");
Local JavaObject &obj_paragraph_l = CreateJavaObject("com.lowagie.text.Paragraph");
&obj_paragraph_l.add(&Obj_javaanchor_l);
Local JavaObject &obj_reflection_l = CreateJavaObject("image");
&Obj_createhyperlinkedpdf_l = &obj_reflection_l.addParagraphtoDocument(&Obj_createhyperlinkedpdf_l, &obj_paragraph_l);
&Obj_createhyperlinkedpdf_l.close();

If you open the resulting PDF file, you will find a single line in the PDF pointing to Google. Click on it and do your search!..Got a question, post us and we will get you the answer..

No comments:

Post a Comment