In this chapter, we will see how to create a PDF document and add a table and format the contents of a cell in the table using the iText library. itext formatting cell contents.
Formatting the Cells in a Table
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. You can format the contents of a cell in a table using the methods of the Cell class.
Following are the steps to format the contents of a cell in a table.
Step 1: Creating a PdfWriter object
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/addingBackground.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 PDFDocument 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 cells
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 the Cell class, as shown below.
// Adding cell 1 to the table Cell cell1 = new Cell(); // Creating a cell cell1.add("Name"); // Adding content to the cell // Adding cell 2 to the table Cell cell2 = new Cell(); // Creating a cell cell2.add("Raju"); // Adding content to the cell
Step 6: Adding Background to the cell
Once you created the cell and added contents to it, you can format the cell. For example, you can set its background, align the text inside the cell, change the text color, etc., using different methods of the cell class such as setBackgroundColor(), setBorder(), setTextAlignment().
You can set the background color, border, and text alignment to the cell created in the previous step, as shown below.
c1.setBackgroundColor(Color.DARK_GRAY); // Setting background color to cell1 c1.setBorder(Border.NO_BORDER); // Setting border to cell1 c1.setTextAlignment(TextAlignment.CENTER); // Setting text alignment to cell1
Step 7: Adding cell to the table
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.
table.addCell(c1);
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 format the contents of a cell in a table using the iText library. It creates a PDF document with the name addingBackground.pdf, adds a table to it, formats the contents of its cells, and saves it in the path C:/itextExamples/
Save this code in a file with the name BackgroundToTable.java.
import com.itextpdf.kernel.color.Color; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.layout.Document; import com.itextpdf.layout.border.Border; import com.itextpdf.layout.element.Cell; import com.itextpdf.layout.element.Table; import com.itextpdf.layout.property.TextAlignment; public class BackgroundToTable { public static void main(String args[]) throws Exception { // Creating a PdfWriter object String dest = "C:/itextExamples/addingBackground.pdf"; PdfWriter writer = new PdfWriter(dest); // Creating a PdfDocument object PdfDocument pdfDoc = new PdfDocument(writer); // Creating a Document object Document doc = new Document(pdfDoc); // Creating a table float [] pointColumnWidths = {200F, 200F}; Table table = new Table(pointColumnWidths); // Populating row 1 and adding it to the table Cell c1 = new Cell(); // Creating cell 1 c1.add("Name"); // Adding name to cell 1 c1.setBackgroundColor(Color.DARK_GRAY); // Setting background color c1.setBorder(Border.NO_BORDER); // Setting border c1.setTextAlignment(TextAlignment.CENTER); // Setting text alignment table.addCell(c1); // Adding cell 1 to the table Cell c2 = new Cell(); c2.add("Raju"); c2.setBackgroundColor(Color.GRAY); c2.setBorder(Border.NO_BORDER); c2.setTextAlignment(TextAlignment.CENTER); table.addCell(c2); // Populating row 2 and adding it to the table Cell c3 = new Cell(); c3.add("Id"); c3.setBackgroundColor(Color.WHITE); c3.setBorder(Border.NO_BORDER); c3.setTextAlignment(TextAlignment.CENTER); table.addCell(c3); Cell c4 = new Cell(); c4.add("001"); c4.setBackgroundColor(Color.WHITE); c4.setBorder(Border.NO_BORDER); c4.setTextAlignment(TextAlignment.CENTER); table.addCell(c4); // Populating row 3 and adding it to the table Cell c5 = new Cell(); c5.add("Designation"); c5.setBackgroundColor(Color.DARK_GRAY); c5.setBorder(Border.NO_BORDER); c5.setTextAlignment(TextAlignment.CENTER); table.addCell(c5); Cell c6 = new Cell(); c6.add("Programmer"); c6.setBackgroundColor(Color.GRAY); c6.setBorder(Border.NO_BORDER); c6.setTextAlignment(TextAlignment.CENTER); table.addCell(c6); // Adding Table to document doc.add(table); // Closing the document doc.close(); System.out.println("Background added successfully.."); } }
Compile and execute the saved Java file from the Command prompt using the following commands −
javac BackgroundToTable.java java BackgroundToTable
Upon execution, the above program creates a PDF document, displaying the following message.
Background added successfully..
If you verify the specified path, you can find the created PDF document, as shown below.
Next Topic : Click Here
Pingback: iText - Adding a Table with example | Adglob Infosystem Pvt Ltd