PeopleCode WaterMark PDF Documents

We saw how to get started with generating PDF documents in PeopleSoft earlier. In this post, we will discuss how to add watermark to a PDF document through PeopleCode. We will use an existing PDF document, "Add_Watermark.pdf" and then use iText from PeopleCode to add a watermark to it. The procedure for adding a watermark through PeopleCode is on the same lines as of Java Watermarking example. So, make sure that you go through the Java example before you read this post.

1) Place the document to be watermarked and the watermark image in a location that PeopleCode can access.

2) Create an object for PdfReader,through CreateJavaObject. The reader object is required to know the number of pages in the PDF document, which will require watermarking. The PeopleCode snippet to get the number of pages is provided below

Local JavaObject &Obj_read_existing_pdf_l = CreateJavaObject("com.lowagie.text.pdf.PdfReader", "Add_Watermark.pdf");
Local number &Nm_numberofpages_l = &Obj_read_existing_pdf_l.getNumberOfPages();
3) Create a Javaobject for PdfStamper class and pass the PDF file name that will contain the original input file with watermark. We will have to pass the PdfReader object as an input to this CreateJavaObject call. The PeopleCode to do this is provided below

Local JavaObject &Obj_watermarked_pdf_l = CreateJavaObject("com.lowagie.text.pdf.PdfStamper", &Obj_read_existing_pdf_l, CreateJavaObject("java.io.FileOutputStream", "Watermarked.pdf", True));

4) Create an image object of type com.lowagie.text.Image which will act as a container for the watermark image. We will have to set the xposition and yposition of this image object using "setAbsolutePosition" call as done earlier. From PeopleCode, we will have to declare two float variables and pass the x and y coordinate values as float inputs to this method. The PeopleCode to do this is provided below

Local JavaObject &Obj_imageobject_l = GetJavaClass("com.lowagie.text.Image").getInstance("watermark.jpg");
Local float &fl_xposition_l = 200;
Local float &fl_yposition_l = 400;
&Obj_imageobject_l.setAbsolutePosition(&fl_xposition_l, &fl_yposition_l);

5) We now declare a Java Object which will act as a container for com.lowagie.text.pdf.PdfContentByte class. We will run a for loop for the number of pages in the document, use "getUnderContent" method to get a PdfContentByte object and use "addImage" method to place the watermark for every page in the PDF document. The PeopleCode that will add this watermarking is given below

Local JavaObject &obj_pdfcontentbyte_l;
Local number &Nm_loopcounter_l = 0;
For &Nm_loopcounter_l = 1 To &Nm_numberofpages_l
   &obj_pdfcontentbyte_l = &Obj_watermarked_pdf_l.getUnderContent(&Nm_loopcounter_l);
   &obj_pdfcontentbyte_l.addImage(&Obj_imageobject_l);
End-For;

6) Finally, we will close the pdfStamper object which will write EOF information to the PDF file. Once this is done, you will be able to find the watermarked PDF document inside your appserv folder.

&Obj_watermarked_pdf_l.close();
The complete PeopleCode example to add a watermark image to every page in a PDF document is provided below;

Local JavaObject &Obj_read_existing_pdf_l = CreateJavaObject("com.lowagie.text.pdf.PdfReader", "Add_Watermark.pdf");
Local number &Nm_numberofpages_l = &Obj_read_existing_pdf_l.getNumberOfPages();
Local JavaObject &Obj_watermarked_pdf_l = CreateJavaObject("com.lowagie.text.pdf.PdfStamper", &Obj_read_existing_pdf_l, CreateJavaObject("java.io.FileOutputStream", "Watermarked.pdf", True));
Local JavaObject &Obj_imageobject_l = GetJavaClass("com.lowagie.text.Image").getInstance("watermark.jpg");
Local float &fl_xposition_l = 200;
Local float &fl_yposition_l = 400;
&Obj_imageobject_l.setAbsolutePosition(&fl_xposition_l, &fl_yposition_l);
Local JavaObject &obj_pdfcontentbyte_l;
Local number &Nm_loopcounter_l = 0;
For &Nm_loopcounter_l = 1 To &Nm_numberofpages_l
   &obj_pdfcontentbyte_l = &Obj_watermarked_pdf_l.getUnderContent(&Nm_loopcounter_l);
   &obj_pdfcontentbyte_l.addImage(&Obj_imageobject_l);
End-For;
&Obj_watermarked_pdf_l.close();
The watermark image used for this PeopleSoft example is the same as used in our Java example. You may wish to grab the image from the Java PDF example so that it can be used for this PeopleSoft example as well.

No comments:

Post a Comment