Adding Nested Tables to a PDF using Java
Last Updated :
03 Apr, 2023
We can add nested tables to a PDF by installing the document class. While instantiating this class, you would like to pass a PdfDocument object as a parameter, to its constructor. Then, to feature a table to the document, you would like 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.
Below are the steps to add the nested tables to a PDF using java:
1. Create a PDF writer object
The PdfWriter class here 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 trail of the file where the PDF is to be created.
Create the PdfWriter class by passing a string value (representing the trail where you would like to make a PDF) to its constructor.
2. Create 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 create this class (in writing mode), you would like to pass an object of the category PdfWriter to its constructor.
Create the PdfDocument class by passing the above created PdfWriter object to its constructor.
3. Create the Document object
The Document class of the package com.itextpdf.layout is that the root element while creating a self-sufficient PDF. one among the constructors of this class accepts an object of the category PdfDocument.
Create the Document class by passing the thing of the category PdfDocument created within the previous steps.
4. Create a Table object
The Table class represents a two-dimensional grid crammed with cells, ordered in rows and columns. It belongs to the package com.itextpdf.layout.element.
5. Create the cell
Create a cell object by creating the Cell class of the package com.itextpdf.layout.
6. Create Nested Table
After creating the cell, create a nested table, and populate its cells.
7. Add Nested table to the cell
Add the nested table created in the previous step to the cell of the container table using the add() method of the Cell class. Add this cell to the containertable using the addCell() method of the Table class
8. Add the table to the document
Add the table object created in the previous step using the add() method of the Document class
9. Closing the Document
Close the document using the close() method of the Document class
Now, let us see some examples of how we can apply these steps
Java
// Java Program to add Nested Tables to a PDF
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 nestedTablesPdf {
public static void main(String args[]) throws Exception
{
// Creating a PdfWriter object
String destination
= "C:/itextExamples/addingNestedTable.pdf";
PdfWriter writer = new PdfWriter(destination);
// Creating a PdfDocument object
PdfDocument pdfDoc = new PdfDocument(writer);
// Creating a Document object
Document doc = new Document(pdfDoc);
// Creating a table
float[] pointColumnWidths1 = { 130f, 130f };
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("Mayank Tyagi");
table.addCell(cell2);
// Populating row 2 and adding it to the table
Cell cell3 = new Cell();
cell3.add("Designation");
table.addCell(cell3);
Cell cell4 = new Cell();
cell4.add("Tech. Content Writer");
table.addCell(cell4);
// Populating row 3 and adding it to the table
Cell cell5 = new Cell();
cell5.add("Company");
table.addCell(cell5);
Cell cell6 = new Cell();
cell6.add("GeeksforGeeks");
table.addCell(cell6);
// Creating nested table for contact
float[] pointColumnWidths2 = { 130f, 130f };
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("1122334455");
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("[email protected]");
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("Delhi");
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("Table successfully added");
}
}
Output

Similar Reads
Adding Images to a Table in PDF using Java
PDFBox is an open-source library which is written in Java. It helps in the development and conversion of PDF Documents. PDFBox library comes in form of a JAR file. It can create new PDF documents, manipulate existing documents, bookmark the PDF and also extract content from PDF documents. We can use
7 min read
Adding List in a PDF using Java
In this article, we will learn how to create a PDF and add a list to that PDF using java. For adding a list in a PDF, we will use the iText library. These are the steps that should be followed to add a list in a PDF using java. 1. Creating a PdfWriter object The PdfWriter class represents the DocWri
3 min read
Adding Paragraphs as Text to a PDF using Java
iText is a Java library developed, to access and manipulate PDF files, that is to extract and modify the PDF content. Java allows us to incorporate various fully developed packages and modules in order to work with PDF files. We will see how to create a PDF document and add a paragraph to it using t
4 min read
Adding Pages to a PDF Document using Java
PDDocument class of 'org.apache.pdfbox.pdmodel' package which extends 'java.lang.Object'. is used. Declaration: public class PDDocument extends Object implements Pageable, Closeable Pre-requisite: Constructors PDDocument(): This constructor used to construct a new PDF document with zero pages.PDDocu
4 min read
Create a Table in a PDF Using Java
The creation of a table in a PDF using Java is done by installing the document class. While instantiating this class, pass a PdfDocument object as a parameter to its constructor. Then, to feature a table to the document, instantiate the Table class, and add this object to the document using the add(
1 min read
Adding Water Marks to the Images in a PDF using Java
In this article, we will learn how to add watermarks to the images in a PDF document using Java. Adding watermarks to the images in a PDF, we will use the iText library. These are the steps that should be followed to add watermarks to the images in a PDF using java. 1. Creating a PdfWriter object Th
4 min read
Formatting the Text in a PDF using Java
We can add nested tables to a PDF by installing the document class. Following are the steps to format the text in a PDF using java. 1. Create a PDF writer object The PdfWriter class here represents the DocWriter for a PDF. This class belongs to the package com.itextpdf.kernel.pdf. The constructor of
3 min read
Drawing a Line in a PDF Document using Java
In this article, we will learn how to Draw a line in a PDF document using Java. For drawing a line in a PDF, we will use the iText library. These are the steps that should be followed to Draw a line in a PDF using java. 1. Creating a PdfWriter object The PdfWriter class represents the DocWriter for
3 min read
Splitting a PDF into many using Java
Program to split the PDF document into multiple PDFs. Below is the implementation for the same using JAVA. The prerequisite of this topic is that you have already installed apache library Approach: Load the PDF from the computer.Load the PDF using the class called PDDocument.Use load() function of
2 min read
How to Create, Edit & Alter Tables Using Java?
In Java, JDBC can be used to create, edit, and alter the tables. JDBC can be defined as Java Database Connectivity. It is an API that allows Java programs to interact with the databases. JDBC can implement Java programs to execute SQL queries and manipulate databases through a standard interface. In
5 min read