Change Cell Width Color Example - Java iText Format PDF Table- Part 2

We are discussing how to apply various formatting options to a table created in a PDF document using iText Java API and this part two of this series. In the first part we discussed how to set the background color for a cell [PdfPCell, Method: setBackgroundColor] and how to set the border color for a cell [ PdfPCell, Method:setBorderColor].We will continue this series in this blog post and discuss couple of more formatting options for a Cell inside a PDF Table. Note that we will be using the base example provided in Part 1 of this series and it is recommended to go through that Part before getting on with this tutorial.
Can you show us how to set the left border of a cell alone to a different color in iText with an example?
In order to set the left border color of a cell to a different color (Take Red) as an example, we can use the setBorderColorLeft method. This method accepts a BaseColor object as shown in Part 1, and we will set the Cell 1 left border alone to RED color using this method. The Java code snippet to set left border color is shown below;

            table_cell.setBorderColorLeft(cell_background_color.RED);/* Set Left Border Color of a Cell to RED */            
On similar lines if you want to set the right border color of a cell, you can use setBorderColorRight method. To set the top border color use setBorderColorTop method and to set bottom border color of a cell use setBorderColorBottom method.
Can you show us how to change the width of a table cell in a PDF table created using iText?
To change the width of a Cell, iText provides the user with a lot of options. Each of these options are listed below; [ All these methods of PdfPCell accepts a float input ]
setBorderWidth -> Used to set the complete width of a cell border.
setBorderWidthLeft -> Used to set the left border width of a cell.
setBorderWidthRight -> Used to set the right border width of a cell.
setBorderWidthTop -> Used to set the top border width of a cell.
setBorderWidthBottom -> Used to set the bottom border width of a cell.

A complete Java code example that explains how to set and change the width of a cell inside a table for a PDF document 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);            
            PdfPCell table_cell;
            float widthval=2;
            table_cell=new PdfPCell(new Phrase("Cell 1"));
            table_cell.setBorderWidth(widthval); /* Sets complete border width */
            my_first_table.addCell(table_cell);            
            table_cell=new PdfPCell(new Phrase("Cell 2"));
            table_cell.setBorderWidthTop(widthval); /* sets width of top border */
            my_first_table.addCell(table_cell);            
            table_cell=new PdfPCell(new Phrase("Cell 3"));
            table_cell.setBorderWidthRight(widthval); /* Sets width of right border */
            my_first_table.addCell(table_cell);            
            table_cell=new PdfPCell(new Phrase("Cell 4"));            
            my_first_table.addCell(table_cell);          
            table_cell=new PdfPCell(new Phrase("Cell 5"));
            table_cell.setBorderWidthLeft(widthval); /* Sets width of Left border */
            my_first_table.addCell(table_cell);       
            table_cell=new PdfPCell(new Phrase("Cell 6"));
            table_cell.setBorderWidthBottom(widthval); /* Sets width of bottom border */
            my_first_table.addCell(table_cell);                  
            iText_Create_Table.add(my_first_table);                       
            iText_Create_Table.close();
        }
        catch (Exception i)
        {
            System.out.println(i);
        }
    }
} 
The output of this code is shown below;
Change width of a Cell inside a Table in iText
Change width of a Cell inside a Table in iText
More examples and formatting options to follow..

No comments:

Post a Comment