PeopleSoft Add Image PDF File Example

In this tutorial, we will discuss how to add image to PDF files from PeopleSoft applications. We will be using the iText PDF API and integrate it with PeopleSoft for this example. I would also like to recommend reading the Java tutorial to Add image to PDF provided on this website earlier as the implementation in PeopleSoft would be closely aligned to that example. Once you have installed / configured itext to be used with PeopleSoft, you can follow the step-by-step procedure provided below to add an image file through Peoplecode to a new PDF document.

Step-1
We begin by declaring a Document and PdfWriter object from PeopleCode. These are the basic objects that you will be requiring whenever you create a PDF document from PeopleCode. Example below
Local JavaObject &Obj_InsertImagePDF_l = CreateJavaObject("com.lowagie.text.Document");
Local JavaObject &obj_writePDFoutput_l = GetJavaClass("com.lowagie.text.pdf.PdfWriter").getInstance(&Obj_InsertImagePDF_l, CreateJavaObject("java.io.FileOutputStream", "ImageInsertedFromPeopleCode.pdf", True));
&Obj_InsertImagePDF_l.open();

Step-2
The image file needs to be placed in the appropriate directory (application server / process scheduler, depending on how you place your PeopleCode). We will use the following image file for this example
Sample Image to Insert to PDF from PeopleCode
Sample Image for Inserting to PDF Document from PeopleCode
Step-3:
We will now declare an image object and set the properties like alignment, border width and border type for the image we will be placing into the PDF document.
Local JavaObject &Obj_imageobject_l = GetJavaClass("com.lowagie.text.Image").getInstance("InsertToPDF.jpg");
&Obj_imageobject_l.setAlignment(GetJavaClass("com.lowagie.text.Image").RIGHT | GetJavaClass("com.lowagie.text.Image").TEXTWRAP);
&Obj_imageobject_l.setBorder(GetJavaClass("com.lowagie.text.Image").BOX);
&Obj_imageobject_l.setBorderWidth(15);
Step-4:
We are done with basic image manipulation from PeopleCode, for putting in PDF file. You can now use Document.Add to add the image object to the Document and Close the Document. This would provide a new PDF document with an image inserted into it.
&Obj_InsertImagePDF_l.add(&Obj_imageobject_l);
&Obj_InsertImagePDF_l.close();
Step-5:
The complete code example to insert an image to PDF file using PeopleCode is provided below
Local JavaObject &Obj_InsertImagePDF_l = CreateJavaObject("com.lowagie.text.Document");
Local JavaObject &obj_writePDFoutput_l = GetJavaClass("com.lowagie.text.pdf.PdfWriter").getInstance(&Obj_InsertImagePDF_l, CreateJavaObject("java.io.FileOutputStream", "ImageInsertedFromPeopleCode.pdf", True));
&Obj_InsertImagePDF_l.open();
Local JavaObject &Obj_imageobject_l = GetJavaClass("com.lowagie.text.Image").getInstance("InsertToPDF.jpg");
&Obj_imageobject_l.setAlignment(GetJavaClass("com.lowagie.text.Image").RIGHT | GetJavaClass("com.lowagie.text.Image").TEXTWRAP);
&Obj_imageobject_l.setBorder(GetJavaClass("com.lowagie.text.Image").BOX);
&Obj_imageobject_l.setBorderWidth(15);
&Obj_InsertImagePDF_l.add(&Obj_imageobject_l);
&Obj_InsertImagePDF_l.close();
Note:
You may get more than one overload matches or no overload matches error when you run this example. It could be because of the version of iText.jar you are using. Refer to the reflection techniques examples provided earlier to solve such problems.You can also write a wrapper class alternatively to solve such problems.

No comments:

Post a Comment