iText - Adding Lists to a Table


Advertisements

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

Adding Lists to a Table 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 list to the table, you need to instantiate the List class of the com.itextpdf.layout.element package and insert it into the cell object using the add() method of the Cell class.

Following are the steps to add a list to the cell of a table.

Step 1: Creating a PdfWriter object

The PdfWriter class represents the Doc Writer 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/addingObjects.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 listCell = new Cell(); // Creating a cell 

Step 6: Creating List object

After creating the cell, create a list object by instantiating the List class of the package com.itextpdf.layout.element. Create the list items by instantiating the ListItem class and add the created items using the add() method of the List class, as shown below.

List list = new List(); 
ListItem item1 = new ListItem("JavaFX"); 
ListItem item2 = new ListItem("Java"); 
ListItem item3 = new ListItem("Java Servlets");        
list.add(item1); 
list.add(item2); 
list.add(item3); 

Step 7: Adding list to the cell of a table

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

listCell.add(list); 
table.addCell(listCell);

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 list to a cell of a table in a PDF document using the iText library. It creates a PDF document with the name addingObjects.pdf, adds a table to it, inserts a list to one of its cells, and saves it in the path C:/itextExamples/

Save this code in a file with the name AddingListsToTable.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.List; 
import com.itextpdf.layout.element.ListItem; 
import com.itextpdf.layout.element.Table; 
import com.itextpdf.layout.property.TextAlignment;  

public class AddingListsToTable {      
   public static void main(String args[]) throws Exception {              
      // Creating a PdfWriter object
      String file = "C:/itextExamples/addingObjects.pdf";       
      PdfDocument pdfDoc = new PdfDocument(new PdfWriter(file));                   
      
      // Creating a Document object       
      Document doc = new Document(pdfDoc);               
      
      // Creating a table       
      float [] pointColumnWidths = {300F, 300F};       
      Table table = new Table(pointColumnWidths);                            
      
      // Adding row 1 to the table                
      Cell c1 = new Cell();       
      c1.add("Java Related Tutorials");       
      c1.setTextAlignment(TextAlignment.LEFT);       
      table.addCell(c1);                      
      
      List list1 = new List();       
      ListItem item1 = new ListItem("JavaFX");
      ListItem item2 = new ListItem("Java");       
      ListItem item3 = new ListItem("Java Servlets");              
      list1.add(item1);       
      list1.add(item2);       
      list1.add(item3);                 
      
      Cell c2 = new Cell();       
      c2.add(list1);       
      c2.setTextAlignment(TextAlignment.LEFT);       
      table.addCell(c2);                 
      
      // Adding row 2 to the table                
      Cell c3 = new Cell();       
      c3.add("No SQL Databases");       
      c3.setTextAlignment(TextAlignment.LEFT);       
      table.addCell(c3);                     
      
      List list2 = new List();       
      list2.add(new ListItem("HBase"));       
      list2.add(new ListItem("Neo4j"));       
      list2.add(new ListItem("MongoDB"));                 
      
      Cell c4 = new Cell();       
      c4.add(list2); 
      c4.setTextAlignment(TextAlignment.LEFT);       
      table.addCell(c4);                       
      
      // Adding Table to document        
      doc.add(table);                  
      
      // Closing the document       
      doc.close();  
      System.out.println("Lists added to table successfully..");     
   } 
}  

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

javac AddingListsToTable.java 
java AddingListsToTable

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

Lists added to table successfully..

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

Adding Objects
Advertisements