iText - Nested Table


Advertisements

In this chapter, we will see how to add a nested table to a table in a PDF document using the iText library.

Adding Nested Tables in a Pdf

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.

To add a table to this table, you need to create another table (nested table), and pass it to the cell object using the add() method of the Cell class.

Following are the steps to insert a table into the cell of 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/addingNestedTable.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 PDF Document 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 the cell

Create a cell object by instantiating the Cell class of the package com.itextpdf.layout, as shown below.

// Adding cell to the table 
Cell contact = new Cell();    // Creating a cell 

Step 6: Creating Nested table

After creating the cell, create a nested table, and populate its cells, as shown below.

// Creating nested table for contact   
float [] pointColumnWidths2 = {150f, 150f}; 
Table nestedTable = new Table(pointColumnWidths2);    

// Populating row 1 and adding it to the nested table  
Cell nested1 = new Cell(); 
nested1.add("Phone"); 
nestedTable.addCell(nested1);       

Cell nested2 = new Cell(); 
nested2.add("9848022338"); 
nestedTable.addCell(nested2);  

// Populating row 2 and adding it to the nested table  
Cell nested3 = new Cell(); 
nested3.add("email"); 
nestedTable.addCell(nested3);

Cell nested4 = new Cell(); 
nested4.add("Raju123@gmail.com"); 
nestedTable.addCell(nested4);     

// Populating row 3 and adding it to the nested table  
Cell nested5 = new Cell(); 
nested5.add("Address"); 
nestedTable.addCell(nested5);  

Cell nested6 = new Cell(); 
nested6.add("Hyderabad"); 
nestedTable.addCell(nested6);  

Step 7: Adding Nested table to the cell

Now, add the above created nested table to the cell of the parent (container) table using the add() method of the Cell class. And, add this cell to the parent table using the addCell() method of the Table class, as shown below.

contact.add(nestedTable); 
table.addCell(contact);

Step 8: 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 9: 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 add a table to a cell of a table (nested table) in a PDF document using the iText library. It creates a PDF document with the name addingNestedTable.pdf, adds a table to it, inserts another table to one of its cells, and saves it in the path C:/itextExamples/.

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

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

import com.itextpdf.layout.Document; 
import com.itextpdf.layout.element.Cell; 
import com.itextpdf.layout.element.Table;  

public class a4AddNestedTablesPdf {  
   public static void main(String args[]) throws Exception {                        
      // Creating a PdfWriter object   
      String dest = "C:/itextExamples/addingNestedTable.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 [] pointColumnWidths1 = {150f, 150f};       
      Table table = new Table(pointColumnWidths1);                             
      
      // Populating row 1 and adding it to the table          
      Cell cell1 = new Cell();       
      cell1.add("Name");       
      table.addCell(cell1);             
      
      Cell cell2 = new Cell();       
      cell2.add("Raju");       
      table.addCell(cell2);                          
   
      // Populating row 2 and adding it to the table        
      Cell cell3 = new Cell();       
      cell3.add("Id");       
      table.addCell(cell3);             
      
      Cell cell4 = new Cell();       
      cell4.add("1001");       
      table.addCell(cell4);                    
   
      // Populating row 3 and adding it to the table        
      Cell cell5 = new Cell();       
      cell5.add("Designation");       
      table.addCell(cell5); 
      
      Cell cell6 = new Cell();       
      cell6.add("Programmer");       
      table.addCell(cell6);              
   
      // Creating nested table for contact         
      float [] pointColumnWidths2 = {150f, 150f};       
      Table nestedTable = new Table(pointColumnWidths2);                
      
      // Populating row 1 and adding it to the nested table        
      Cell nested1 = new Cell();       
      nested1.add("Phone");       
      nestedTable.addCell(nested1);                   
      
      Cell nested2 = new Cell();       
      nested2.add("9848022338");       
      nestedTable.addCell(nested2);                   
      
      // Populating row 2 and adding it to the nested table        
      Cell nested3 = new Cell();       
      nested3.add("email");       
      nestedTable.addCell(nested3);                    
      
      Cell nested4 = new Cell();       
      nested4.add("Raju123@gmail.com");       
      nestedTable.addCell(nested4);                       
      
      // Populating row 3 and adding it to the nested table        
      Cell nested5 = new Cell();       
      nested5.add("Address");       
      nestedTable.addCell(nested5);                    
      
      Cell nested6 = new Cell();       
      nested6.add("Hyderabad");       
      nestedTable.addCell(nested6);              
      
      // Adding table to the cell       
      Cell cell7 = new Cell();       
      cell7.add("Contact");       
      table.addCell(cell7);              
      
      Cell cell8 = new Cell();       
      cell8.add(nestedTable);       
      table.addCell(cell8);
      
      // Adding table to the document       
      doc.add(table);                   
      
      // Closing the document               
      doc.close();  
      System.out.println("Nested Table Added successfully..");     
   } 
}    

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

javac AddNestedTable.java 
java AddNestedTable 

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

Nested Table Added successfully..

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

Adding Nested Table
Advertisements