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 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( "w3wiki" ); 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( "Mayanktyagi1709@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( "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
Contact Us