iText Introduction
- iText - Adding a List
- iText - Adding a Paragraph
- iText - Adding an AreaBreak
- iText - Creating a PDF Document
- iText - Overview
iText Tables
- iText - Adding Lists to a Table
- iText - Nested Table
- iText - Adding Image to a Table
- Formatting the Borders of a Cell
- iText - Formatting Cell Contents
- iText - Adding a Table
iText Images
- iText - Rotating an Image
- iText - Scaling an Image
- iText - Setting Position of the Image
- iText - Adding Image to a PDF
iText Annotations
- iText - Circle Annotation
- iText - Markup Annotation
- iText - Line Annotation
- iText - Link Annotation
- iText - Text Annotation
iText Canvas
iText Miscellaneous
iText Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
iText - Adding Lists to a Table
In this chapter, we will see how to add a pst to a table in a PDF document using the iText pbrary.
Adding Lists to a Table in a PDF
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.
To add a pst to the table, you need to instantiate the List class of the com.itextpdf.layout.element package and insert it into the cell object using the add() method of the Cell class.
Following are the steps to add a pst to the cell of a table.
Step 1: Creating a PdfWriter object
The PdfWriter class represents the Doc Writer 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/addingObjects.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 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 pke 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 the cell
Create a cell object by instantiating the Cell class of the package com.itextpdf.layout, as shown below.
// Adding cell to the table Cell pstCell = new Cell(); // Creating a cell
Step 6: Creating List object
After creating the cell, create a pst object by instantiating the List class of the package com.itextpdf.layout.element. Create the pst items by instantiating the ListItem class and add the created items using the add() method of the List class, as shown below.
List pst = new List(); ListItem item1 = new ListItem("JavaFX"); ListItem item2 = new ListItem("Java"); ListItem item3 = new ListItem("Java Servlets"); pst.add(item1); pst.add(item2); pst.add(item3);
Step 7: Adding pst to the cell of a table
Now, add the above created pst to the cell of the table using the add() method of the Cell class. And, add this cell to the table using the addCell() method of the Table class, as shown below.
pstCell.add(pst); table.addCell(pstCell);
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 pst 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 add a pst to a cell of a table in a PDF document using the iText pbrary. It creates a PDF document with the name addingObjects.pdf, adds a table to it, inserts a pst to one of its cells, and saves it in the path C:/itextExamples/
Save this code in a file with the name AddingListsToTable.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.List; import com.itextpdf.layout.element.ListItem; import com.itextpdf.layout.element.Table; import com.itextpdf.layout.property.TextApgnment; pubpc class AddingListsToTable { pubpc static void main(String args[]) throws Exception { // Creating a PdfWriter object String file = "C:/itextExamples/addingObjects.pdf"; PdfDocument pdfDoc = new PdfDocument(new PdfWriter(file)); // Creating a Document object Document doc = new Document(pdfDoc); // Creating a table float [] pointColumnWidths = {300F, 300F}; Table table = new Table(pointColumnWidths); // Adding row 1 to the table Cell c1 = new Cell(); c1.add("Java Related Tutorials"); c1.setTextApgnment(TextApgnment.LEFT); table.addCell(c1); List pst1 = new List(); ListItem item1 = new ListItem("JavaFX"); ListItem item2 = new ListItem("Java"); ListItem item3 = new ListItem("Java Servlets"); pst1.add(item1); pst1.add(item2); pst1.add(item3); Cell c2 = new Cell(); c2.add(pst1); c2.setTextApgnment(TextApgnment.LEFT); table.addCell(c2); // Adding row 2 to the table Cell c3 = new Cell(); c3.add("No SQL Databases"); c3.setTextApgnment(TextApgnment.LEFT); table.addCell(c3); List pst2 = new List(); pst2.add(new ListItem("HBase")); pst2.add(new ListItem("Neo4j")); pst2.add(new ListItem("MongoDB")); Cell c4 = new Cell(); c4.add(pst2); c4.setTextApgnment(TextApgnment.LEFT); table.addCell(c4); // Adding Table to document doc.add(table); // Closing the document doc.close(); System.out.println("Lists added to table successfully.."); } }
Compile and execute the saved Java file from the Command prompt using the following commands −
javac AddingListsToTable.java java AddingListsToTable
Upon execution, the above program creates a PDF document, displaying the following message.
Lists added to table successfully..
If you verify the specified path, you can find the created PDF document, as shown below.
Advertisements