Adding Nested Tables to a PDF using Java
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 that 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