In this chapter, we will see how to create a PDF document and add a table to it using the iText library.
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.
Following are the steps to create a PDF document with a Table in it.
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/addingTable.pdf"; PdfWriter writer = new PdfWriter(dest);
When the object of this type is passed to a PdfDocument (class), every element added to this document will be written to the file specified.
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.
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);
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 object float [] pointColumnWidths = {150F, 150F, 150F}; Table table = new Table(pointColumnWidths);
Create a cell object by instantiating the Cell class of the package com.itextpdf.layout.element. Add the contents of the cell using the add() method of this class.
Finally, to add this cell to the table, call the addCell() method of the Table class and pass the cell object as a parameter to this method, as shown below.
// Adding cell 1 to the table Cell cell1 = new Cell(); // Creating a cell cell1.add("Name"); // Adding content to the cell table.addCell(cell1); // Adding cell to the table // Adding cell 2 to the table Cell cell2 = new Cell(); // Creating a cell cell2.add("Raju"); // Adding content to the cell table.addCell(cell2); // Adding cell to the table
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);
Close the document using the close() method of the Document class, as shown below.
// Closing the document document.close();
The following Java program demonstrates how to create a PDF document and add a table to it using the iText library. It creates a PDF document with the name addingTable.pdf, adds a table to it, and saves it in the path C:/itextExamples/
Save this code in a file with the name AddingTable.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 AddingTable { public static void main(String args[]) throws Exception { // Creating a PdfDocument object String dest = "C:/itextExamples/addingTable.pdf"; PdfWriter writer = new PdfWriter(dest); // Creating a PdfDocument object PdfDocument pdf = new PdfDocument(writer); // Creating a Document object Document doc = new Document(pdf); // Creating a table float [] pointColumnWidths = {150F, 150F, 150F}; Table table = new Table(pointColumnWidths); // Adding cells to the table table.addCell(new Cell().add("Name")); table.addCell(new Cell().add("Raju")); table.addCell(new Cell().add("Id")); table.addCell(new Cell().add("1001")); table.addCell(new Cell().add("Designation")); table.addCell(new Cell().add("Programmer")); // Adding Table to document doc.add(table); // Closing the document doc.close(); System.out.println("Table created successfully.."); } }
Compile and execute the saved Java file from the Command prompt using the following commands −
javac AddingTable.java java AddingTable
Upon execution, the above program creates a PDF document, displaying the following message.
Table created successfully..
If you verify the specified path, you can find the created PDF document, as shown below.