Change Cell Width Border Color iText Example PeopleCode Tutorial

This tutorial explains how to dynamically change the border width of a cell inside a Table using iText Java API and PeopleCode. In the first tutorial of this series, we discussed an approach to change the background and border color of a Cell using PeopleCode. You can think this tutorial as an extension to the first one as this dwells more on cell level properties of a PdfPTable object. To run the PeopleCode examples presented in this post, you need to have the iText library in your classpath. I'm using version 2.1.7 of iText for demonstration purposes.
How to change the Cell border width for a table cell created using iText in Peoplecode?
The iText library offers a lot of methods to change the border width of cells created in a table.We will cover each of these methods with suitable example. It is to be noted that it is possible to either set the complete border width of a cell or specify individual values for Left, right, top and bottom borders. There are different methods available for this purpose and they are documented below;
setBorderWidth -> Set the complete width of a cell border.
setBorderWidthLeft -> Set the left border width of a cell.
setBorderWidthRight -> Set the right border width of a cell.
setBorderWidthTop -> Set the top border width of a cell.
setBorderWidthBottom -> Set the bottom border width of a cell.
We will now extend the PDF Table example to set each of these widths for our table and examine the output PDF table in this case. Refer to the PeopleCode snippet below;
Local float &fl = 2; /* Weight of the border width that needs to be used */
Local JavaObject &Obj_myPDFTableCell_l = &obj_image_reflection_l.ReturnPDFCellObject("Cell 1");
&Obj_myPDFTableCell_l.setBorderWidth(&fl); /* Set full width for 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_myPDFTableCell_l.setBorderWidthTop(&fl); /* Set Top Border Width alone */
&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.setBorderWidthLeft(&fl); /* Set Left Border Width for the Cell Only */
&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_myPDFTableCell_l.setBorderWidthBottom(&fl); /* Set Bottom Border Width for the Cell */
&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.setBorderWidthRight(&fl); /* Right Border Width alone */
When you run this example (refer to full PeopleCode example at the bottom of this post), you will get a PDF table that has its cell width adjusted on the borders as shown in the screenshot below;
Cell Border Width of PDF Table Adjusted Using PeopleCode
Cell Border Width of PDF Table Adjusted Using PeopleCode
Ok,that is interesting. Is it also possible to specify a unique border color to a specific cell border in each of these cases?
Yes, iText also offers some unique methods to change the cell border color, either completely or partially. To do this, you have to invoke the right methods as provided below;

setBorderColor -> Set the complete Color of a cell border.
setBorderColorLeft -> Set the left border Color of a cell.
setBorderColorRight -> Set the right border Color of a cell.
setBorderColorTop -> Set the top border Color of a cell.
setBorderColorBottom -> Set the bottom border Color of a cell.
We can extend the example we provided above to set a unique border color in each of the cases. The input you pass to these methods from PeopleCode should be of type java.awt.Color. An example PDF table created from PeopleCode using these methods is provided below;
Change Cell Border Color Using PeopleCode and iText
Change Cell Border Color Using iText
The complete PeopleCode example to adjust the width of a Cell and change the border color of Cell partially or completely 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;
Local JavaObject &Objbasecolor = CreateJavaObject("java.awt.Color", &red, &green, &blue);
Local JavaObject &Obj_myPDFTable_l = CreateJavaObject("com.lowagie.text.pdf.PdfPTable", 3);
Local JavaObject &obj_image_reflection_l = CreateJavaObject("image");
Local float &fl = 2; /* Weight of the border width that needs to be used */
Local JavaObject &Obj_myPDFTableCell_l = &obj_image_reflection_l.ReturnPDFCellObject("Cell 1");
&Obj_myPDFTableCell_l.setBorderWidth(&fl); /* Set full width for Cell 1 */
&Obj_myPDFTableCell_l.setBorderColor(&Objbasecolor.RED); /* Set full border 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 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_myPDFTableCell_l.setBorderWidthTop(&fl); /* Set Top Border Width alone */
&Obj_myPDFTableCell_l.setBorderColorTop(&Objbasecolor.YELLOW); /* Set Top Border Color 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 4");
&Obj_myPDFTableCell_l.setBorderWidthLeft(&fl); /* Set Left Border Width for the Cell Only */
&Obj_myPDFTableCell_l.setBorderColorLeft(&Objbasecolor.GREEN); /* Set Left Border Color 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_myPDFTableCell_l.setBorderWidthBottom(&fl); /* Set Bottom Border Width for the Cell */
&Obj_myPDFTableCell_l.setBorderColorBottom(&Objbasecolor.PINK); /* Set Bottom Border color to Pink */
&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.setBorderWidthRight(&fl); /* Right Border Width alone */
&Obj_myPDFTableCell_l.setBorderColorRight(&Objbasecolor.BLUE); /* Set Right Border color to 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();
We will discuss some more formatting options for a Cell in a PDF table in the upcoming posts;

No comments:

Post a Comment