iText Format PDF Table Java Example Tutorial Part 1

Starting today, we are going to present a new series of tutorials in which we will explain how to apply different formatting options for a table that is embedded inside a PDF file. This tutorial is built on top of our initial tutorial on iText that explained how to create a basic table inside a PDF file.My plan for this tutorial would be to present all the formatting options that I can find for a table with working Java code examples as I keep explaining each one of them. To start with I have a table with two rows and three columns, 6 cells in total. The Java code based on the tutorial we provided earlier for such a table is given below;

import java.io.*;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
public class iTextFormatCell {  
     public static void main(String[] args){
        try {
            Document iText_Create_Table = new Document();
            PdfWriter.getInstance(iText_Create_Table, new FileOutputStream("iText_Format_PDF_Table_Example.pdf"));
            iText_Create_Table.open();            
            PdfPTable my_first_table = new PdfPTable(3);           
            my_first_table.addCell("Cell 1"); 
            PdfPCell table_cell; 
            table_cell=new PdfPCell(new Phrase("Cell 2"));             
            my_first_table.addCell(table_cell);            
            table_cell=new PdfPCell(new Phrase("Cell 3"));            
            my_first_table.addCell(table_cell);            
            my_first_table.addCell("Cell 4");
            my_first_table.addCell("Cell 5");
            my_first_table.addCell("Cell 6");
            my_first_table.addCell("Cell 7");
            iText_Create_Table.add(my_first_table);                       
            iText_Create_Table.close();
        }
        catch (Exception i)
        {
            System.out.println(i);
        }
    }
} 
Now, as we go ahead with this tutorial, we will use each and every method in the iText library and see how to apply the formatting options to our table. An image of the table created out of the code above is shown below;
iText Table Example
An iText Table without any Formatting Applied
We will be improvising our initial code on a step by step basis. At each step, we will apply some formatting options to our table cell and provide the Jave code to achieve that formatting effect. If we run short of cells at any point in time, I will add some more to the table. We have already covered ROWSPAN and COLSPAN for the tables and so I will not be covering them in this example. We will get started with the How To Tutorial of PDF tables created out of iText.To start with we will discuss cell level properties and then get into table level properties.
How to set the background color of a Cell in a table created using iText Java library?
To set the background color for a Cell, we will have to define a BaseColor object to start with. [com.itextpdf.text.BaseColor] The constructor for this class accepts three values one each for red, blue and green. Don't worry, we can change this later.I want to set some background color for my first three cells. Define a BaseColor object as shown below;
            int r=0;
            int g=255;
            int b=255;        
            BaseColor cell_background_color=new BaseColor(r,g,b);
Now,against the PdfPCell object that you want to color, you can invoke the setBackgroundColor method to specify a background color. The BaseColor class supports some static color values like Yellow, Green and Pink. We will use them. To generate your own colors, you can vary the red,green and blue composition used earlier. The code to set background color for a Cell is shown below;
            table_cell=new PdfPCell(new Phrase("Cell 1"));
            table_cell.setBackgroundColor(cell_background_color.YELLOW); /* Set cell background to yellow */
            my_first_table.addCell(table_cell);           
            table_cell=new PdfPCell(new Phrase("Cell 2"));
            table_cell.setBackgroundColor(cell_background_color.GREEN); /* Set background color to Green */
            my_first_table.addCell(table_cell);            
            table_cell=new PdfPCell(new Phrase("Cell 3"));           
            table_cell.setBackgroundColor(cell_background_color.PINK); /* Set Cell Color to Pink */
Now, if you apply these changes to the original code above, you get nice colors for your table cells as shown below;
Table Cell with Background Color in PDF
Table Cells with Background Color Applied
How to set the border color of a Cell using Java iText API?
In order to set the border color for a Cell in a Table using Java iText API, use the setBorderColor method. You will have to pass a BaseColor object to this method also, as explained earlier. We will now provide a code snippet that sets Cell4 border to RED and Cell6 border to YELLOW.
            table_cell=new PdfPCell(new Phrase("Cell 4"));
            table_cell.setBorderColor(cell_background_color.RED); /* Set Cell border color to RED */
            my_first_table.addCell(table_cell);          
            table_cell=new PdfPCell(new Phrase("Cell 5"));
            my_first_table.addCell(table_cell);       
            table_cell=new PdfPCell(new Phrase("Cell 6"));
            table_cell.setBorderColor(cell_background_color.YELLOW); /* Set Border color of Cell to YELLOW */
An output of the table with border color applied to the cells is shown below;
Cell Border Color - iText
Cell Border Color Example
The complete Java Code example to set the cell background color and cell border color to a PDF table using the iText library is provided below;
import java.io.*;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
public class iTextFormatCell {  
     public static void main(String[] args){
        try {
            Document iText_Create_Table = new Document();
            PdfWriter.getInstance(iText_Create_Table, new FileOutputStream("iText_Format_PDF_Table_Example.pdf"));
            iText_Create_Table.open();            
            PdfPTable my_first_table = new PdfPTable(3);  
            int r=0;
            int g=255;
            int b=255;        
            BaseColor cell_background_color=new BaseColor(r,g,b);
            PdfPCell table_cell;
            table_cell=new PdfPCell(new Phrase("Cell 1"));
            table_cell.setBackgroundColor(cell_background_color.YELLOW); /* Set cell background to yellow */            
            my_first_table.addCell(table_cell);           
            table_cell=new PdfPCell(new Phrase("Cell 2"));
            table_cell.setBackgroundColor(cell_background_color.GREEN); /* Set background color to Green */            
            my_first_table.addCell(table_cell);            
            table_cell=new PdfPCell(new Phrase("Cell 3"));           
            table_cell.setBackgroundColor(cell_background_color.PINK); /* Set Cell Color to Pink */            
            my_first_table.addCell(table_cell);            
            table_cell=new PdfPCell(new Phrase("Cell 4"));
            table_cell.setBorderColor(cell_background_color.RED); /* Set Cell border color to RED */
            my_first_table.addCell(table_cell);          
            table_cell=new PdfPCell(new Phrase("Cell 5"));
            my_first_table.addCell(table_cell);       
            table_cell=new PdfPCell(new Phrase("Cell 6"));
            table_cell.setBorderColor(cell_background_color.YELLOW); /* Set Border color of Cell to YELLOW */
            my_first_table.addCell(table_cell);                   
            iText_Create_Table.add(my_first_table);                       
            iText_Create_Table.close();
        }
        catch (Exception i)
        {
            System.out.println(i);
        }
    }
} 
We will continue this series in the upcoming posts.
Did you notice that cell 4 has only three sides turned to red and the right side is still black? I guess this is because Cell 5 is written after Cell 4. That also explains why Cell 6 has all its sides set to Yellow!
We wrote Cell 7 to the table in the original example.Why it does not show on the screen? Any guesses?

No comments:

Post a Comment