iText - Formatting the Borders of a Cell



In this chapter, we will see how to format the borders of a cell in a table using iText library.

Formatting the Borders of a Cell

You can create an empty PDF Document by instantiating the Document class. While instantiating this class, you need to pass a PdfDocument object as a parameter to its constructor.

Then, to add a table to the document, you need to instantiate the Table class and add this object to the document using the add() method.

You can add various types of borders like DashedBorder, SolidBorder, DottedBorder, DoubleBorder, RoundDotsBorder, etc. with various colors using the setBorder() method of the Cell class.

Following are the steps to format the borders of a cell in a table.

Step 1: Creating a PdfWriter object

The PdfWriter class represents the DocWriter for a PDF. This class belongs to the package com.itextpdf.kernel.pdf. The constructor of this class accepts a string, representing the path of the file where the PDF is to be created.

Instantiate the PdfWriter class by passing a string value (representing the path where you need to create a PDF) to its constructor, as shown below.

// Creating a PdfWriter 
String dest = "C:/itextExamples/coloredBorders.pdf"; 
PdfWriter writer = new PdfWriter(dest); 

When an object of this type is passed to a PdfDocument (class), every element added to this document will be written to the file specified.

Step 2: Creating a PdfDocument object

The PdfDocument class is the class that represents the PDFDocument in iText. This class belongs to the package com.itextpdf.kernel.pdf. To instantiate this class (in writing mode), you need to pass an object of the class PdfWriter to its constructor.

Instantiate the PdfDocument class by passing the above created PdfWriter object to its constructor, as shown below.

// Creating a PdfDocument  
PdfDocument pdfDoc = new PdfDocument(writer); 

Once a PdfDocument object is created, you can add various elements like page, font, file attachment, and event handler using the respective methods provided by its class.

Step 3: Creating the Document object

The Document class of the package com.itextpdf.layout is the root element while creating a self-sufficient PDF. One of the constructors of this class accepts an object of the class PdfDocument.

Instantiate the Document class by passing the object of the class PdfDocument created in the previous steps, as shown below.

// Creating a Document  
Document document = new Document(pdfDoc); 

Step 4: Creating a Table object

The Table class represents a two-dimensional grid filled with cells ordered in rows and columns. It belongs to the package com.itextpdf.layout.element.

Instantiate the Table class as shown below.

// Creating a table 
float [] pointColumnWidths = {200F, 200F}; 
Table table = new Table(pointColumnWidths); 

Step 5: Creating cells

Create a cell object by instantiating the Cell class of the package com.itextpdf.layout.element add the contents of the cell using the add() method of the Cell class, as shown below.

// Adding cell 1 to the table 
Cell cell1 = new Cell(); // Creating a cell 
cell1.add("Name");       // Adding content to the cell 

Step 6: Formatting the border of the cell

The iText library provides various classes representing the border such as DashedBorder, SolidBorder, DottedBorder, DoubleBorder, RoundDotsBorder, etc.

The constructors of these classes accept two parameters: a color object representing the color of the border and an integer representing the width of the border.

Choose one of this border types and instantiate the respective border by passing the color object and an integer representing the width, as shown below.

Border b1 = new DashedBorder(Color.RED, 3); 

Now, set the border of the cell using the setBorder() method of the cell class. This method accepts an object of the type Border as a parameter.

Set the border of the cell by passing the above created Border object as a parameter to the setBorder() method as shown below.

c1.setBorder(b1)

Finally, to add this cell to the table, call the addCell() method of the Table class and pass the cell object as a parameter to this method, as shown below.

table.addCell(c1);

Step 7: Adding table to the document

Add the table object created in the previous step using the add() method of the Document class, as shown below.

// Adding list to the document 
document.add(table);

Step 8: Closing the Document

Close the document using the close() method of the Document class, as shown below.

// Closing the document 
document.close();

Example

The following Java program demonstrates how to format the border of a cell in a table using the iText library. It creates a PDF document with the name coloredBorders.pdf, adds a table to it, formats the contents of its cells, and saves it in the path C:/itextExamples/

Save this code in a file with the name FormatedBorders.java.

import com.itextpdf.kernel.color.Color; 
import com.itextpdf.kernel.pdf.PdfDocument; 
import com.itextpdf.kernel.pdf.PdfWriter; 

import com.itextpdf.layout.Document; 
import com.itextpdf.layout.border.Border; 
import com.itextpdf.layout.border.DashedBorder; 
import com.itextpdf.layout.border.DottedBorder; 
import com.itextpdf.layout.border.DoubleBorder; 
import com.itextpdf.layout.border.RoundDotsBorder; 
import com.itextpdf.layout.border.SolidBorder; 

import com.itextpdf.layout.element.Cell; 
import com.itextpdf.layout.element.Table; 
import com.itextpdf.layout.property.TextAlignment;  

public class FormatedBorders {      
   public static void main(String args[]) throws Exception {
      // Creating a PdfWriter object   
      String dest = "C:/itextExamples/coloredBorders.pdf";   
      
      PdfWriter writer = new 
      PdfWriter(dest);                 

      // Creating a PdfDocument object       
      PdfDocument pdfDoc = new PdfDocument(writer);                      
   
      // Creating a Document object      
      Document doc = new Document(pdfDoc);                            
   
      // Creating a table       
      float [] pointColumnWidths = {200F, 200F};       
      Table table = new Table(pointColumnWidths); 
   
      // Adding row 1 to the table
      Cell c1 = new Cell();
      
      // Adding the contents of the cell
      c1.add("Name");
   
      // Setting the back ground color of the cell
      c1.setBackgroundColor(Color.DARK_GRAY);    
   
      // Instantiating the Border class 
      Border b1 = new DashedBorder(Color.RED, 3);
   
      // Setting the border of the cell
      c1.setBorder(b1);
      
      // Setting the text alignment       
      c1.setTextAlignment(TextAlignment.CENTER);
   
      // Adding the cell to the table       
      table.addCell(c1);                          
      Cell c2 = new Cell();       
      c2.add("Raju");       
      c1.setBorder(new SolidBorder(Color.RED, 3));       
      c2.setTextAlignment(TextAlignment.CENTER);       
      table.addCell(c2);
   
      // Adding row 2 to the table                
      Cell c3 = new Cell();      
      c3.add("Id"); 
      c3.setBorder(new DottedBorder(Color.DARK_GRAY, 3));       
      c3.setTextAlignment(TextAlignment.CENTER);      
      table.addCell(c3);                          
      
      Cell c4 = new Cell();       
      c4.add("001");       
      c4.setBorder(new DoubleBorder(Color.DARK_GRAY, 3));       
      c4.setTextAlignment(TextAlignment.CENTER);       
      table.addCell(c4);                          
      
      // Adding row 3 to the table       
      Cell c5 = new Cell();       
      c5.add("Designation");       
      c5.setBorder(new RoundDotsBorder(Color.RED, 3));       
      c5.setTextAlignment(TextAlignment.CENTER);       
      table.addCell(c5);                 
      
      Cell c6 = new Cell();       
      c6.add("Programmer");       
      c6.setBorder(new RoundDotsBorder(Color.RED, 3)); 
      c6.setTextAlignment(TextAlignment.CENTER);      
      table.addCell(c6);                              
      
      // Adding Table to document        
      doc.add(table);                  
      
      // Closing the document       
      doc.close();  
      
      System.out.println("Borders added successfully..");     
   } 
} 

Compile and execute the saved Java file from the Command prompt using the following commands −

javac FormatedBorders.java 
java FormatedBorders

Upon execution, the above program creates a PDF document, displaying the following message.

Borders added successfully

If you verify the specified path, you can find the created PDF document, as shown below.

Coloured Borders
Advertisements