PeopleCode Create PDF Table Example

In this tutorial, we will see how to create a table inside a PDF document from PeopleSoft using iText Java library. We earlier saw a beginners tutorial on how to create PDF documents from PeopleCode. This example is an extension of that tutorial. We will be using Document and PdfWriter objects in this tutorial also and I would recommend the readers to go through the beginners tutorial to get a grasp on iText basics before attempting this example. In this example, we will describe how to create a PDF table, add some rows / columns, set ROWSPAN and COLSPAN to cells and generate a PDF document. I would assume that the right version of iText (itext-2.1.7) is loaded to your classes folder.(refer to the beginner tutorial if not). 

1) We start with creating the Document object from PeopleCode and a PdfWriter object that will create the final PDF file. The Peoplecode is provided below
Local JavaObject &Obj_CreateiTextTable_l = CreateJavaObject("com.lowagie.text.Document");
Local JavaObject &obj_writePDFoutput_l = GetJavaClass("com.lowagie.text.pdf.PdfWriter").getInstance(&Obj_CreateiTextTable_l, CreateJavaObject("java.io.FileOutputStream", "CreateTableExample.pdf", True));

2) We can now open the Document object for adding a table. At this stage we also create a PdfPTable object from PeopleCode and specify the number of columns in our table to be 3. Then, we will use the "addCell" method of the table object to add the first data at (0,0). The PdfPTable object will hold the complete table structure and will eventually be added to the Document object to create a table.
&Obj_CreateiTextTable_l.open(); /*open the document for writing */
Local JavaObject &Obj_myPDFTable_l = CreateJavaObject("com.lowagie.text.pdf.PdfPTable", 3);
&Obj_myPDFTable_l.addCell("0,0 First Cell Data");
3) Now, like the Java example for creating a table we cannot do it in a straightforward manner in PeopleCode. When you try to create a PdfPCell object by passing the "Phrase" object (com.lowagie.text.Phrase), PeopleSoft will be very quick to throw the infamous "more than one overload matches" error.To mitigate this, we have to use reflection concepts of Java or write a wrapper class.we will write a wrapper class (image.class) that will take a string as input and return a PdfPCell object back to PeopleSoft. The wrapper class method is shown below
public PdfPCell ReturnPDFCellObject(String Content){
PdfPCell table_cell=new PdfPCell(new Phrase(Content));
return table_cell;
}
With this method, we can get the right PdfPCell object and set its COLSPAN to 2 using PeopleCode, by invoking te “setColspan” method. There is also a setRowspan method to indicate the ROWSPAN but we will not cover it in this example.
Local JavaObject &obj_image_reflection_l = CreateJavaObject("image");
Local JavaObject &Obj_myPDFTableCell_l = &obj_image_reflection_l.ReturnPDFCellObject("Hi from PeopleCode"); /* More than one overload matches issue */
&Obj_myPDFTableCell_l.setColspan(2);

4) Now, when you try to add this PdfPCell object to your PdfPTable object by using the "addCell" method, we will get the same "more than one overload matches" error. We will have to expand the wrapper class to include a method that accepts the PdfPTable and PdfPCell objects, invokes the "addCell" method and returns the updated PdfPTable object back to PeopleCode. This wrapper class method is shown below
public PdfPTable addCelltoTable(PdfPTable table_details,PdfPCell Cell_object){
table_details.addCell(Cell_object);
return table_details;
}

The corresponding PeopleCode that adds the PdfPCell object to the table is shown below
&Obj_myPDFTable_l = &obj_image_reflection_l.addCelltoTable(&Obj_myPDFTable_l, &Obj_myPDFTableCell_l);

5) On the same lines, we add one more row to the PDF table, by using PeopleCode and the methods written in the wrapper class.
Local JavaObject &Obj_myPDFTableCell_2_l = &obj_image_reflection_l.ReturnPDFCellObject("A Cell COLSPAN 2"); /* More than one overload matches issue */
&Obj_myPDFTableCell_2_l.setColspan(2);
&Obj_myPDFTable_l = &obj_image_reflection_l.addCelltoTable(&Obj_myPDFTable_l, &Obj_myPDFTableCell_2_l);
&Obj_myPDFTable_l.addCell("2,3 Row 2 Column 3");

6) We have added two rows to the table now and the table is ready for getting stamped to the PDF document.To do that, we have to invoke Document.Add to add the table to the document. If we attempt to do this directly in PeopleCode, we will get the same "more than one overload matches" error , as Document object contains more than one Add method. To add the PdfPTable to the Document object, we will write a wrapper method that will accept the Document Object and PdfPTable object and return back to PeopleCode, the Document object for any further modifications. The wrapper method is shown below
public Document addTabletoDocument(PdfPTable table_details,Document PDFDocument) {
try {
PDFDocument.add(table_details);
return PDFDocument;
}
catch (Exception i)
{
System.out.println(i);
return PDFDocument;
}
}

We will invoke this wrapper class method from PeopleCode which will add the table to the document. At this point, the PDFWriter object will write the table contents for us into the actual file. The Document object can be closed following that. The PeopleCode is provided below
&Obj_CreateiTextTable_l = &obj_image_reflection_l.addTabletoDocument(&Obj_myPDFTable_l, &Obj_CreateiTextTable_l);
&Obj_CreateiTextTable_l.close();

Complete PeopleCode and Wrapper Class code for this “Create Table in PDF using PeopleCode” tutorial is provided below:

Local JavaObject &Obj_CreateiTextTable_l = CreateJavaObject("com.lowagie.text.Document");
Local JavaObject &obj_writePDFoutput_l = GetJavaClass("com.lowagie.text.pdf.PdfWriter").getInstance(&Obj_CreateiTextTable_l, CreateJavaObject("java.io.FileOutputStream", "CreateTableExample.pdf", True));
&Obj_CreateiTextTable_l.open(); /*open the document for writing */
Local JavaObject &Obj_myPDFTable_l = CreateJavaObject("com.lowagie.text.pdf.PdfPTable", 3);
&Obj_myPDFTable_l.addCell("0,0 First Cell Data");
Local JavaObject &obj_image_reflection_l = CreateJavaObject("image");
Local JavaObject &Obj_myPDFTableCell_l = &obj_image_reflection_l.ReturnPDFCellObject("Hi from PeopleCode"); /* More than one overload matches issue */
&Obj_myPDFTableCell_l.setColspan(2);
&Obj_myPDFTable_l = &obj_image_reflection_l.addCelltoTable(&Obj_myPDFTable_l, &Obj_myPDFTableCell_l);
Local JavaObject &Obj_myPDFTableCell_2_l = &obj_image_reflection_l.ReturnPDFCellObject("A Cell COLSPAN 2"); /* More than one overload matches issue */
&Obj_myPDFTableCell_2_l.setColspan(2);
&Obj_myPDFTable_l = &obj_image_reflection_l.addCelltoTable(&Obj_myPDFTable_l, &Obj_myPDFTableCell_2_l);
&Obj_myPDFTable_l.addCell("2,3 Row 2 Column 3");
&Obj_CreateiTextTable_l = &obj_image_reflection_l.addTabletoDocument(&Obj_myPDFTable_l, &Obj_CreateiTextTable_l);
&Obj_CreateiTextTable_l.close();


import java.io.*;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.Document;
public class image {
public static void main(String[] args){
System.out.println("Hi");
}
public PdfPCell ReturnPDFCellObject(String Content){
PdfPCell table_cell=new PdfPCell(new Phrase(Content));
return table_cell;
}
public PdfPTable addCelltoTable(PdfPTable table_details,PdfPCell Cell_object){
table_details.addCell(Cell_object);
return table_details;
}
public Document addTabletoDocument(PdfPTable table_details,Document PDFDocument) {
try {
PDFDocument.add(table_details);
return PDFDocument;
}
catch (Exception i)
{
System.out.println(i);
return PDFDocument;
}
}
}

The PDF document created by this code will have a good looking table, screenshot of which is provided below

image
PDF Table Created From PeopleCode using IText Java Library

1 comment:

  1. can any one tell me how to extract tet from a pdf file, tried using pdfTextExtractor it always give no overload methods

    ReplyDelete