In this example tutorial, we will provide the instructions on how to merge PDF documents in PeopleSoft applications using PeopleCode. We had earlier seen an example for joining PDF files through Java.You may end up creating PDF documents through PeopleCode, and position yourself in a case where you have to copy the contents of two or multiple PDF files into one single document. This guide assumes that you already have the input documents in place and the iText API on your classpath. If you don't, then you may wish to see our code snippets in the related post section below, that will explain how to do this as a first step.
Merging Multiple PDF documents in PeopleCode is very simple if you know the basics of creating javaobject and invoking them from PeopleCode. Without keeping you waiting any further, we will begin this step by step guide now.
Step-1: Create a Document and PdfCopy object to start with. Refer to the code below (with comments). Also, open the final PDF file for adding the contents from existing files.
Merging Multiple PDF documents in PeopleCode is very simple if you know the basics of creating javaobject and invoking them from PeopleCode. Without keeping you waiting any further, we will begin this step by step guide now.
Step-1: Create a Document and PdfCopy object to start with. Refer to the code below (with comments). Also, open the final PDF file for adding the contents from existing files.
/* Create a Document Object in PeopleCode for Merging */
Local JavaObject &Obj_MergePDFDocuments_l = CreateJavaObject("com.lowagie.text.Document");
/* Create a PdfCopy Object for copying the contents of multiple PDF files */
Local JavaObject &Obj_PdfCopyObject_l = CreateJavaObject("com.lowagie.text.pdf.PdfCopy", &Obj_MergePDFDocuments_l, CreateJavaObject("java.io.FileOutputStream", "Final_Merged_Document.pdf", True));
/* Open the Document Object for Adding Contents from Another PDF file */
&Obj_MergePDFDocuments_l.open();
Step -2: At this stage, you have the fundamental objects that are required for creating the consolidated result document. We will now create a PdfReader object that will be used to read existing PDF documents. We will also create an Array in PeopleCode that will hold the list of files that needs to be fused together./* Create a PDF Reader Object to Read PDF documents that needs to be combined / mixed */
Local JavaObject &Obj_ReaderObject_l;
/* Create an Array in PeopleSoft that will hold the files to be combined. You can dynamically push contents into the array depending on your requirement */
Local array of string &ArFileHolder = CreateArrayRept("", 0); /* creates an empty array of string */
&ArFileHolder.Push("File1.pdf");
&ArFileHolder.Push("File2.pdf");
/* The Number variable below is used to get the number of pages in input file */
Local number &NumberofPages;
Step-3: For every input file read by PeopleSoft, import the document using the reader object created earlier. Then, stamp every page from the imported document into the final document by using getImportedPage method to retrieve a single page and by using addPage method of the Copy object to add the imported page to the final document. To know the number of pages in a PDF document, use the getNumberofPages method. Finally close the PDF file opened in step 1. Your merged PDF file is ready for viewing./* Loop incoming input files for merging */
For &i = 1 To &ArFileHolder.Len
&Obj_ReaderObject_l = CreateJavaObject("com.lowagie.text.pdf.PdfReader", &ArFileHolder [&i]);
/* Get number of pages in the input PDF document */
&NumberofPages = &Obj_ReaderObject_l.getNumberOfPages();
rem MessageBox(0, "", 0, 0, "" | &NumberofPages | &ArFileHolder [&i]);
For &j = 1 To &NumberofPages
/* Import page from source document and move it to merged document */
&Obj_PdfCopyObject_l.addPage(&Obj_PdfCopyObject_l.getImportedPage(&Obj_ReaderObject_l, &j))
End-For;
End-For;
/* Close the Document Object */
&Obj_MergePDFDocuments_l.close();
The complete PeopleCode example is provided below. File1.pdf and File2.pdf are the elements in the array, and these two files serve as the input file for combining. "Final_Merged_Document.pdf" is the merged document created through Peoplecode.
Local JavaObject &Obj_MergePDFDocuments_l = CreateJavaObject("com.lowagie.text.Document");
Local JavaObject &Obj_PdfCopyObject_l = CreateJavaObject("com.lowagie.text.pdf.PdfCopy", &Obj_MergePDFDocuments_l, CreateJavaObject("java.io.FileOutputStream", "Final_Merged_Document.pdf", True));
&Obj_MergePDFDocuments_l.open();
Local JavaObject &Obj_ReaderObject_l;
Local array of string &ArFileHolder = CreateArrayRept("", 0); /* creates an empty array of string */
&ArFileHolder.Push("File1.pdf");
&ArFileHolder.Push("File2.pdf");
Local number &NumberofPages;
For &i = 1 To &ArFileHolder.Len
&Obj_ReaderObject_l = CreateJavaObject("com.lowagie.text.pdf.PdfReader", &ArFileHolder [&i]);
&NumberofPages = &Obj_ReaderObject_l.getNumberOfPages();
For &j = 1 To &NumberofPages
&Obj_PdfCopyObject_l.addPage(&Obj_PdfCopyObject_l.getImportedPage(&Obj_ReaderObject_l, &j))
End-For;
End-For;
&Obj_MergePDFDocuments_l.close();
When you run the example, you can get the following errors: [If you get any other errors, share it with us below.]
Java Exception: java.io.IOException: File1.pdf not found as file or resource.: during construction of com.lowagie.text.pdf.PdfReader. (2,768) The constructor for the given class threw the noted Java exception.
Check your input file path.PeopleSoft is not able to read the input files
Check your input file path.PeopleSoft is not able to read the input files
Java Exception: ExceptionConverter: java.io.IOException: The document has no pages.: during call of com.lowagie.text.Document.close. The noted Java error was thrown during a call of the given method.
No pages were merged into the final document. Check your input PDF documents for available pages. Check if you have declared the array with enough elements.
No pages were merged into the final document. Check your input PDF documents for available pages. Check if you have declared the array with enough elements.
Java Exception: java.lang.IllegalArgumentException: Invalid page number: during call of com.lowagie.text.pdf.PdfCopy.getImportedPage.
Check your for loop variables.They are trying to import a PDF page that does not exist.
Check your for loop variables.They are trying to import a PDF page that does not exist.
Hi,
ReplyDeleteExcellent topic, however, I seem to have difficulties.
First Problem was the following:
1) I had to extract the class files from the JAR files and place them under the following location:
$PS_HOME/appserv/classes/com/itextpdf/text/;$PS_HOME/appserv/classes/com/itextpdf/text/pdf/;$PS_HOME/appserv/classes/com/itextpdf/text/xml/
This was because peoplesoft did not seem to recognise the JAR file, after bouncing the application server and following the Peoplebooks doc.
2) Then I had to amend the class path in the PSAPPSRV.CFG file to the above 2 paths.
After getting over the above hurdle, I am now presented with the following error message:
Java Exception: Java.lang.ClassFormatError: (com/itextpdf/text/pdf/PdfWriter) unknown constant pool entry tag at offset=1526: finding class com.itextpdf.txt.pdf.PdfCopy
The classes seem to be found ok by Peoplesoft as the initial error I was getting was that Document class file could not be found.
Do I need to re-compile the source on my AIX Unix box’s before running the class files.
By the way I am using the itextpdf-5.1.0
Any suggestions would be great.
Thanks Francis
@Francis,
ReplyDeleteWhat version of PeopleTools you are using?
By default, you should be having $PS_HOME/appserv/classes in your classpath. Did you try placing the JAR file in this path, rather than extracting it?
In my case, PeopleSoft was able to pick the JAR files directly from the classpath and I did not extract its contents. Can you set the logfence property in your configuration file to 5, and dump the application server log contents?
Hi,
ReplyDeleteCan you please help me with the java file (jar file) and the file path corresponding to the Java Object com.lowagie.text.pdf.PdfReader.
Email me on nandak.ps@gmail.com
@Nandak
ReplyDeleteRefer earlier posts on peoplecode / itext ..it has all the details that you ask for.
You said the following:
ReplyDeleteThis guide assumes that you already have the input documents in place and the iText API on your classpath. If you don't, then you may wish to see our code snippets in the related post section below, that will explain how to do this as a first step.
But, I couldn't find anything called "related posts".
Can you please point me in the right direction?
thank you!