Java iText Cell Padding Example Tutorial - Format Options - Part 4

Cell Padding is a vital option for your HTML table and when you create a PDF table using Java iText API, it would be good to have this feature too. iText does offer a way to specify Cell padding option and in this blog post, we will provide an example tutorial that will explain how to specify cell padding options for a table created using iText. This is part 4 of our table formatting series using iText and if you are keen to check our other parts where we explain multiple formatting options for your PDF table, you can refer to the related post section at the bottom of this post. There are five options to specify cell padding option to an iText table and they are provided below;
setPadding - Padding for Cell content
setPaddingLeft - Left padding option for cell content.
setPaddingRight - Right Padding option for cell content.
setPaddingTop- Top Padding Option for Cell Content
setPaddingBottom - Bottom Padding Option for Cell Content
To understand the difference between all these options, we will now provide a simple Java program that utilizes all these options and examine its output. The Java code example is provided below;
import java.io.*;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
public class iTextCellPadding {  
     public static void main(String[] args){
        try {
            Document iText_Create_Table = new Document();
            PdfWriter.getInstance(iText_Create_Table, new FileOutputStream("iText_Cell_Padding_Example.pdf"));
            iText_Create_Table.open();            
            PdfPTable my_first_table = new PdfPTable(3);            
            PdfPCell table_cell;            
            float widthval=25;
            String CellContent="Check Cell Padding for iText PDF Table";
            table_cell=new PdfPCell(new Phrase("Cell 1" + CellContent));
            table_cell.setPadding(widthval);/* Padding Set in All Sides */
            my_first_table.addCell(table_cell);            
            table_cell=new PdfPCell(new Phrase("Cell 2" + CellContent));
            table_cell.setPaddingLeft(widthval); /* Left Padding Only */
            my_first_table.addCell(table_cell);            
            table_cell=new PdfPCell(new Phrase("Cell 3" + CellContent));
            table_cell.setPaddingRight(widthval);/* Right Padding Only */
            my_first_table.addCell(table_cell);            
            table_cell=new PdfPCell(new Phrase("Cell 4" + CellContent));
            table_cell.setPaddingTop(widthval); /* Top Padding Only */
            my_first_table.addCell(table_cell);          
            table_cell=new PdfPCell(new Phrase("Cell 5" + CellContent));
            table_cell.setPaddingBottom(widthval); /* Bottom Padding Only */
            my_first_table.addCell(table_cell);       
            table_cell=new PdfPCell(new Phrase("Cell 6" + CellContent));            
            my_first_table.addCell(table_cell); /* No Padding Set */                 
            iText_Create_Table.add(my_first_table);                       
            iText_Create_Table.close();
        }
        catch (Exception i)
        {
            System.out.println(i);
        }
    }
}
The output generated by this code will be a table with six cells. Each cell will have a different padding set based on the code example we have provided above. An image of the output is shown below;
iText Cell Padding Option
iText Cell Padding Example 
In the image above Cell1 has padding set in all directions, Cell 2 Left Padding only, Cell 3 Right Padding Only, Cell 4 Top Padding only, Cell 5 Bottom Padding only and Cell 6 No Padding. Depending on your requirement, you can select any padding option that suits to your needs.

1 comment: