PeopleSoft Split PDF Document Example PeopleCode Tutorial

This post presents a tutorial that explains how to split a big PDF document into smaller parts using PeopleCode. You may be here because you got a requirement to separate a PDF into multiple parts based on size, number of pages, fixed number of documents and so on..and you are looking for a solution to solve this case using iText PDF library and PeopleSoft. As we take you through to this example, you will find that it is not only easy to merge PDF files using PeopleCode but also to do the other way  i.e. Split PDFs. You may also refer to our tutorial on combining PDF documents if you are after that. For more information on manipulating PDF documents through PeopleCode, you can refer to the related post sections below.

To Split a PDF document in PeopleSoft, you need to have iText (set in correct classpath) and an input file placed in a path that PeopleCode can pick up. You need only this much and if you are ready, you can refer to the step by step guide below.

Step-1: We begin by creating a PDF reader object (that reads the input file that needs to be sliced). We also calculate the number of pages on the input document and keep a local string variable for us to generate random file names. Refer to the code below;
/* Create PDF Reader Object in PeopleCode to read the file to be split */
Local JavaObject &Obj_readPDFtoSplit_l = CreateJavaObject("com.lowagie.text.pdf.PdfReader", "Final_Merged_Document.pdf");
/* Calculate number of pages in input file */
Local number &NumberofPages = &Obj_readPDFtoSplit_l.getNumberOfPages();
/* Variable to hold the name of split file */
Local string &splitfilename;
Step-2:Create a FOR LOOP that will loop through for the number of pages in the input file. Inside the loop, we create a Document object and PdfCopy object. We then use the "getImportedPage" as used during the merge tutorial to read the page from the input document and write it to the new PDF file. The PdfCopy object requires a file name for instantiation and we supply this input by generating it in a dynamic fashion from PeopleSoft.
/* Loop to build separate files */
For &i = 1 To &NumberofPages
   /* Create Document object for new file */
   Local JavaObject &Obj_SplitPDFDocument_l = CreateJavaObject("com.lowagie.text.Document");
   /* generate new PDF file name */
   &splitfilename = "PeopleSoft_File" | &i | ".pdf";
   /* Create Copy Object to import the Pages to New document */
   Local JavaObject &Obj_PdfCopyObject_l = CreateJavaObject("com.lowagie.text.pdf.PdfCopy", &Obj_SplitPDFDocument_l, CreateJavaObject("java.io.FileOutputStream", &splitfilename, True));
   /* Open new PDF Document */
   &Obj_SplitPDFDocument_l.open();
   /* Add page to the Document */
   &Obj_PdfCopyObject_l.addPage(&Obj_PdfCopyObject_l.getImportedPage(&Obj_readPDFtoSplit_l, &i));
   /* Close Document */
   &Obj_SplitPDFDocument_l.close();
End-For;
Only two steps and we are done. As you may see, we have written very minimal code to split a PDF document inside PeopleSoft application. The complete code is provided below;
Local JavaObject &Obj_readPDFtoSplit_l = CreateJavaObject("com.lowagie.text.pdf.PdfReader", "Final_Merged_Document.pdf");
Local number &NumberofPages = &Obj_readPDFtoSplit_l.getNumberOfPages();
Local string &splitfilename;
For &i = 1 To &NumberofPages
   Local JavaObject &Obj_SplitPDFDocument_l = CreateJavaObject("com.lowagie.text.Document");
   &splitfilename = "PeopleSoft_File" | &i | ".pdf";
   Local JavaObject &Obj_PdfCopyObject_l = CreateJavaObject("com.lowagie.text.pdf.PdfCopy", &Obj_SplitPDFDocument_l, CreateJavaObject("java.io.FileOutputStream", &splitfilename, True));
   &Obj_SplitPDFDocument_l.open();
   &Obj_PdfCopyObject_l.addPage(&Obj_PdfCopyObject_l.getImportedPage(&Obj_readPDFtoSplit_l, &i));
   &Obj_SplitPDFDocument_l.close();
End-For;

2 comments:

  1. Is iText PDF libary delivered by Peoplesoft or do we have purchase the library and install in the Java library paths on AppServer?

    ReplyDelete
  2. Hi YellowRose,
    Thank you for the article, was very usefull for me. I had some dificulties but I could move on... I had 2 problems:
    1.- Find de iText API.
    I found it in: http://www.java2s.com/Code/Jar/i/Downloaditext420comitextpdfjar.htm
    2.- Accessing through peoplecode to the java classes.
    The article is:
    Local JavaObject &Obj_readPDFtoSplit_l = CreateJavaObject("com.lowagie.text.pdf.PdfReader", "Final_Merged_Document.pdf");
    And I got the error: Java Exception: java.lang.NoClassDefFoundError: com/lowagie/text/pdf/PdfReader: finding class com.lowagie.text.pdf.PdfReader
    I solved in this way:
    Local JavaObject &Obj_readPDFtoSplit_l = CreateJavaObject("com/itextpdf/text/pdf/PdfReader", "Final_Merged_Document.pdf");

    I hope I have contributed something to your great article.

    Thanks again!
    Luigi Romero

    ReplyDelete