Change Table Cell Color iText Example PeopleCode Tutorial

We are going to provide a new set of PeopleCode example tutorials in which we will explain how to apply different formatting options for a table that is embedded inside a PDF file, created through PeopleCode. They will be built on top of our initial tutorial on PeopleSoft iText that explained how to create a basic table inside a PDF file.To start with I have a table with two rows and three columns, 6 cells in total. I would like to have a standard PeopleCode example to create a PDF table and then keep improvising it, as we discuss each of the formatting options available for a table cell while creating the table. Refer to the code below. [ for an explanation refer to the initial iText tutorial explained earlier ]
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();
Local JavaObject &Obj_myPDFTable_l = CreateJavaObject("com.lowagie.text.pdf.PdfPTable", 3);
Local JavaObject &obj_image_reflection_l = CreateJavaObject("image");
Local JavaObject &Obj_myPDFTableCell_l = &obj_image_reflection_l.ReturnPDFCellObject("Cell 1");
&Obj_myPDFTable_l = &obj_image_reflection_l.addCelltoTable(&Obj_myPDFTable_l, &Obj_myPDFTableCell_l);
&Obj_myPDFTableCell_l = &obj_image_reflection_l.ReturnPDFCellObject("Cell 2");
&Obj_myPDFTable_l = &obj_image_reflection_l.addCelltoTable(&Obj_myPDFTable_l, &Obj_myPDFTableCell_l);
&Obj_myPDFTableCell_l = &obj_image_reflection_l.ReturnPDFCellObject("Cell 3");
&Obj_myPDFTable_l = &obj_image_reflection_l.addCelltoTable(&Obj_myPDFTable_l, &Obj_myPDFTableCell_l);
&Obj_myPDFTableCell_l = &obj_image_reflection_l.ReturnPDFCellObject("Cell 4");
&Obj_myPDFTable_l = &obj_image_reflection_l.addCelltoTable(&Obj_myPDFTable_l, &Obj_myPDFTableCell_l);
&Obj_myPDFTableCell_l = &obj_image_reflection_l.ReturnPDFCellObject("Cell 5");
&Obj_myPDFTable_l = &obj_image_reflection_l.addCelltoTable(&Obj_myPDFTable_l, &Obj_myPDFTableCell_l);
&Obj_myPDFTableCell_l = &obj_image_reflection_l.ReturnPDFCellObject("Cell 6");
&Obj_myPDFTable_l = &obj_image_reflection_l.addCelltoTable(&Obj_myPDFTable_l, &Obj_myPDFTableCell_l);
&Obj_CreateiTextTable_l = &obj_image_reflection_l.addTabletoDocument(&Obj_myPDFTable_l, &Obj_CreateiTextTable_l);
&Obj_CreateiTextTable_l.close();
The table created out of this PeopleCode is provided below;
Create PDF Table from PeopleCode
Table created from PeopleCode
We can discuss each of the formatting options available to us against this table with suitable examples now.
While creating a PDF table from PeopleCode Using iText API, how to apply a color to the background of a specific cell?
To answer this question,we will have to create a JavaObject for [java.awt.Color and Not com.lowagie.text.BaseColor (we used BaseColor object in our Java Tutorial)] and then apply this object against the PdfPCell objects to set the background color. Refer to the code below to declare a Color object in PeopleCode;
Local integer &red = 0;
Local integer &green = 255;
Local integer &blue = 255;
Local JavaObject &Objbasecolor = CreateJavaObject("java.awt.Color", &red, &green, &blue);
To apply a background color to the Cell, we can simply use the setBackgroundColor method and pass this Color object to it. An example is provided below;
&Obj_myPDFTableCell_l.setBackgroundColor(&Objbasecolor.YELLOW); /* Set cell background to YELLOW */
&Obj_myPDFTable_l = &obj_image_reflection_l.addCelltoTable(&Obj_myPDFTable_l, &Obj_myPDFTableCell_l);
&Obj_myPDFTableCell_l = &obj_image_reflection_l.ReturnPDFCellObject("Cell 2");
&Obj_myPDFTableCell_l.setBackgroundColor(&Objbasecolor.RED); /* Change Cell background color to RED */
&Obj_myPDFTable_l = &obj_image_reflection_l.addCelltoTable(&Obj_myPDFTable_l, &Obj_myPDFTableCell_l);
&Obj_myPDFTableCell_l = &obj_image_reflection_l.ReturnPDFCellObject("Cell 3");
&Obj_myPDFTableCell_l.setBackgroundColor(&Objbasecolor.PINK); /* Apply Cell background color as Pink */
The PDF table produced by this PeopleCode example is provided below;[ all borders will be present, though the image does not show in this clipped version ]
Cells with colored Background
Cells with Colored Background in iText
When creating a PDF table through iText in PeopleSoft, how to set the color the border for a particular Cell in a table using PeopleCode?

For setting the border color of a table cell in iText through PeopleCode, we can use the setBorderColor method of PdfPCell object. This method accepts a Color object similar to the background color example. To set the border color of Cell 4 to green and cell 6 to blue, use the PeopleCode example provided below;
&Obj_myPDFTableCell_l = &obj_image_reflection_l.ReturnPDFCellObject("Cell 4");
&Obj_myPDFTableCell_l.setBorderColor(&Objbasecolor.GREEN); /* Change Border Color of Cell to Green */
&Obj_myPDFTable_l = &obj_image_reflection_l.addCelltoTable(&Obj_myPDFTable_l, &Obj_myPDFTableCell_l);
&Obj_myPDFTableCell_l = &obj_image_reflection_l.ReturnPDFCellObject("Cell 5");
&Obj_myPDFTable_l = &obj_image_reflection_l.addCelltoTable(&Obj_myPDFTable_l, &Obj_myPDFTableCell_l);
&Obj_myPDFTableCell_l = &obj_image_reflection_l.ReturnPDFCellObject("Cell 6");
&Obj_myPDFTableCell_l.setBorderColor(&Objbasecolor.BLUE); /* Set Border Color as BLUE */
This produces a PDF Table with colored borders as shown below;
PDF Cell with Colored Borders
PDF Table with Colored Borders in PeopleCode
The complete PeopleCode example to color a cell with a background color and apply color to cell border in iText PDF Table, 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();
Local integer &red = 0;
Local integer &green = 255;
Local integer &blue = 255;
/* Define COLOR object to pass to iText methods for setting color */
Local JavaObject &Objbasecolor = CreateJavaObject("java.awt.Color", &red, &green, &blue);
/* Create PDFTable object */
Local JavaObject &Obj_myPDFTable_l = CreateJavaObject("com.lowagie.text.pdf.PdfPTable", 3);
/* PeopleCode Reflection techniques needs to be used to add cell to Table. Explained earlier */
Local JavaObject &obj_image_reflection_l = CreateJavaObject("image");
Local JavaObject &Obj_myPDFTableCell_l = &obj_image_reflection_l.ReturnPDFCellObject("Cell 1");
&Obj_myPDFTableCell_l.setBackgroundColor(&Objbasecolor.YELLOW); /* Set cell background to YELLOW */
&Obj_myPDFTable_l = &obj_image_reflection_l.addCelltoTable(&Obj_myPDFTable_l, &Obj_myPDFTableCell_l);
&Obj_myPDFTableCell_l = &obj_image_reflection_l.ReturnPDFCellObject("Cell 2");
&Obj_myPDFTableCell_l.setBackgroundColor(&Objbasecolor.RED); /* Change Cell background color to RED */
&Obj_myPDFTable_l = &obj_image_reflection_l.addCelltoTable(&Obj_myPDFTable_l, &Obj_myPDFTableCell_l);
&Obj_myPDFTableCell_l = &obj_image_reflection_l.ReturnPDFCellObject("Cell 3");
&Obj_myPDFTableCell_l.setBackgroundColor(&Objbasecolor.PINK); /* Apply Cell background color as Pink */
&Obj_myPDFTable_l = &obj_image_reflection_l.addCelltoTable(&Obj_myPDFTable_l, &Obj_myPDFTableCell_l);
&Obj_myPDFTableCell_l = &obj_image_reflection_l.ReturnPDFCellObject("Cell 4");
&Obj_myPDFTableCell_l.setBorderColor(&Objbasecolor.GREEN); /* Change Border Color of Cell to Green */
&Obj_myPDFTable_l = &obj_image_reflection_l.addCelltoTable(&Obj_myPDFTable_l, &Obj_myPDFTableCell_l);
&Obj_myPDFTableCell_l = &obj_image_reflection_l.ReturnPDFCellObject("Cell 5");
&Obj_myPDFTable_l = &obj_image_reflection_l.addCelltoTable(&Obj_myPDFTable_l, &Obj_myPDFTableCell_l);
&Obj_myPDFTableCell_l = &obj_image_reflection_l.ReturnPDFCellObject("Cell 6");
&Obj_myPDFTableCell_l.setBorderColor(&Objbasecolor.BLUE); /* Set Border Color as BLUE */
&Obj_myPDFTable_l = &obj_image_reflection_l.addCelltoTable(&Obj_myPDFTable_l, &Obj_myPDFTableCell_l);
&Obj_CreateiTextTable_l = &obj_image_reflection_l.addTabletoDocument(&Obj_myPDFTable_l, &Obj_CreateiTextTable_l);
&Obj_CreateiTextTable_l.close();

No comments:

Post a Comment