PeopleCode PDF Add Hyperlink Example

In one of the posts earlier, we saw how to create an internal hyperlink in PDF document using Java. On same lines in this post, we will discuss how to create an internal reference in a PDF document from PeopleCode using Java IText API. We will use a mix of PeopleCode and Java reflection / wrapper class techniques and create a PDF document with an internal reference link from PeopleSoft. The step by step procedure do create a hyperlink from PeopleCode is provided below: 

1) We begin with creating a Document object (com.lowagie.text.Document), PdfWriter object (com.lowagie.text.pdf.PdfWriter) using CreateJavaObject call. We also make sure that the PDF document is available for editing by calling Document.open() method. The PeopleCode snippet is provided below:
Local JavaObject &Obj_createpdfdocument_l = CreateJavaObject("com.lowagie.text.Document");
Local JavaObject &obj_writePDFoutput_l = GetJavaClass("com.lowagie.text.pdf.PdfWriter").getInstance(&Obj_createpdfdocument_l, CreateJavaObject("java.io.FileOutputStream", "HyperlinkFromPeopleCode.pdf", True));
&Obj_createpdfdocument_l.open();

2) Just in the same way as we did in the case of Java example, we add an Anchor object (com.lowagie.text.Anchor) and create an ID for the Anchor object for later reference using "setName" method from PeoplCode. We also create a Paragraph object (com.lowagie.text.Paragraph) and add the Anchor java object to it by using the Add method. PeopleCode below

Local JavaObject &Obj_javaanchor_l = CreateJavaObject("com.lowagie.text.Anchor", "Source Bookmark Point in PDF");
&Obj_javaanchor_l.setName("IDSTART");
Local JavaObject &obj_paragraph_l = CreateJavaObject("com.lowagie.text.Paragraph");
&obj_paragraph_l.add(&Obj_javaanchor_l);

3) So, we have successfully created an Anchor point to which we can come back from a different location in the PDF. Now, we have to add the Paragraph object to the Document. We will be greeted with "More than one overload matches" error if we directly give Document.add..back to reflection or wrapper techniques to avoid this error. We will write a small java wrapper method and call it for adding the paragraph to the document object.

public Document addParagraphtoDocument(Document PDFDocument,Paragraph para_content){
try {
PDFDocument.add(para_content);
return PDFDocument;
}
catch (Exception i)
{
System.out.println(i);
return PDFDocument;
}
}

From PeopleCode, we can invoke this method be passing the Document and Paragraph object. This method will Add the Paragraph object to the Document object and then return the Document object back to PeopleSoft .Ugly..huh

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

4) We fill some pages in the PDF after this step, using the filler approach we followed in the case of Java Hyperlink Example.

Local JavaObject &Obj_para_temp;
For &i = 1 To 100
&Obj_para_temp = CreateJavaObject("com.lowagie.text.Paragraph", "Filler Text from PeopleCode");
&Obj_createpdfdocument_l = &obj_reflection_l.addParagraphtoDocument(&Obj_createpdfdocument_l, &Obj_para_temp);
End-For;
5) Now that the pages are filled, we can define another Anchor object, and use "setReference" method to point it to the bookmark created earlier. We can add it to the Document object directly or through the Paragraph object.
Local JavaObject &Obj_linking_point = CreateJavaObject("com.lowagie.text.Anchor", "Take me to the Bookmark");
&Obj_linking_point.setReference("#IDSTART");
Local JavaObject &obj_parag_link = CreateJavaObject("com.lowagie.text.Paragraph");
&obj_parag_link.add(&Obj_linking_point);
&Obj_createpdfdocument_l = &obj_reflection_l.addParagraphtoDocument(&Obj_createpdfdocument_l, &obj_parag_link);
&Obj_createpdfdocument_l.close();
And that is all we have to do to create a hyperlink on a PDF from PeopleCode. If you open the resulting PDF, you will find a link that will take you to the bookmark. The complete PeopleCode example for creating a hyperlink in PDF is provided below
Local JavaObject &Obj_createpdfdocument_l = CreateJavaObject("com.lowagie.text.Document");
Local JavaObject &obj_writePDFoutput_l = GetJavaClass("com.lowagie.text.pdf.PdfWriter").getInstance(&Obj_createpdfdocument_l, CreateJavaObject("java.io.FileOutputStream", "HyperlinkFromPeopleCode.pdf", True));
&Obj_createpdfdocument_l.open();
Local JavaObject &Obj_javaanchor_l = CreateJavaObject("com.lowagie.text.Anchor", "Source Bookmark Point in PDF");
&Obj_javaanchor_l.setName("IDSTART");
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_createpdfdocument_l = &obj_reflection_l.addParagraphtoDocument(&Obj_createpdfdocument_l, &obj_paragraph_l);
Local JavaObject &Obj_para_temp;
For &i = 1 To 100
&Obj_para_temp = CreateJavaObject("com.lowagie.text.Paragraph", "Filler Text from PeopleCode");
&Obj_createpdfdocument_l = &obj_reflection_l.addParagraphtoDocument(&Obj_createpdfdocument_l, &Obj_para_temp);
End-For;
Local JavaObject &Obj_linking_point = CreateJavaObject("com.lowagie.text.Anchor", "Take me to the Bookmark");
&Obj_linking_point.setReference("#IDSTART");
Local JavaObject &obj_parag_link = CreateJavaObject("com.lowagie.text.Paragraph");
&obj_parag_link.add(&Obj_linking_point);
&Obj_createpdfdocument_l = &obj_reflection_l.addParagraphtoDocument(&Obj_createpdfdocument_l, &obj_parag_link);
&Obj_createpdfdocument_l.close();

No comments:

Post a Comment