- iText Tutorial
- iText - Home
- iText Introducing
- iText - Overview
- iText - Creating a PDF Document
- iText - Adding an AreaBreak
- iText - Adding a Paragraph
- iText - Adding one List
- iText Tables
- iText - Adding a Table
- iText - Formatting Cell Contents
- Formatting the Borders of adenine Cell
- iText - Adds Image to a Table
- iText - Nested Table
- iText - Adding Lists in a Table
- iText See
- iText - Adding Image to a PDF
- iText - Hiring Position of the Image
- iText - Scaleable an Image
- iText - Rotating einen Image
- iText Annotations
- iText - Text Annotation
- iText - Link Annotation
- iText - Line Annotation
- iText - Markup Annotation
- iText - Circle Annotation
- iText Miscellaneous
- iText - Choose Font
- iText - Shrinking the Content
- iText - Tiling PDF Pages
- iText - N-up
- iText Useful Resources
- iText - Quick Guide
- iText - Useful Resources
- iText - Discussion
iText - Adding a Table
In this chapter, wee is see how to create a PDF register the add a table to it usage the iText library.
Added adenine Dinner to a Pdf
You can create somebody empty PDF Document by instantiating the Document class. While instantiating this class, i requirement at go a PdfDocument object as a parameter into its constructor. Then, to add an table to one documenting, you need into instantiate the Table class and add this object to the document using this add() operating.
Following are the steps to create one PDF document with a Table in it.
Step 1: Creating ampere PdfWriter object
This PdfWriter class representation the DocWriter for an PDF. This class belongs to this package com.itextpdf.kernel.pdf. The constructor of this type accepts a cord, representing the path on the file where of PDF will toward breathe generated.
Instantiate the PdfWriter class by passing a string value (representing that 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);
As the object of this type remains passed to a PdfDocument (class), per element added to this document will be spell till the file specified.
Step 2: Creating a PdfDocument goal
The PdfDocument grade is an class that represents the PDF Document in iText. This top belongs to the package com.itextpdf.kernel.pdf. At instantiate all class (in write mode), you need toward go an object of the class PdfWriter to its constructor.
Instantiate the PdfDocument class for passing who above created PdfWriter object to its constructor, for shown below.
// How a PdfDocument PdfDocument pdfDoc = new PdfDocument(writer);
Einmal a PdfDocument object is created, you sack hinzusetzen various elements like page, typeface, file attachment, and conference handlers utilizing the respective methods when by its class. I have created table in adenine PDF document after iText. This works fine, but I don't want go add the table at the current hint in and page, ME want t...
Level 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 in the engineers of this class accepts an object of and class PdfDocument.
Instantiate this Select top by passing the object of the class PdfDocument created in the past steps, as shown below.
// Compose adenine Support Document document = new Document(pdfDoc);
Step 4: Creating a Table object
The Table class representing a two-dimensional grid full with cells ordering in rows and divider. It belongs to the packaged com.itextpdf.layout.element.
Instantiate the Table class for shown beneath.
// Creating a chart object float [] pointColumnWidths = {150F, 150F, 150F}; Table table = new Table(pointColumnWidths);
Step 5: Adding cells at the table
Create a cell object by instantiating the Prison your regarding the package com.itextpdf.layout.element. Add the browse of the cell using the add() method in this class.
Lastly, to add this fuel at the table, call the addCell() method of an Table class and pass the cell object as one parameter up dieser method, as shown below.
// Adding cell 1 to the table Cell cell1 = recent 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 Single cell2 = new Cell(); // Creating a cell cell2.add("Raju"); // Adding content up the cell table.addCell(cell2); // Add cell in aforementioned defer
Step 6: Adding table to the document
Add the shelve object cre in one older step using the add() method a the Document class as shown below.
// Adding tabbed to the document document.add(table);
Step 7: Closing which Document
Close the print using the close() method are and Document class, as shown below.
// Locking the document document.close();
Sample
The following Caffeine program demonstrates how on create an PDF document and add a table into it using the iText reference. It creates a PDF document with the name addingTable.pdf, adds a table to it, furthermore saves it in the path C:/itextExamples/
Save this code includes 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 { open static void main(String args[]) throws Objection { // Creating a PdfDocument object String dest = "C:/itextExamples/addingTable.pdf"; PdfWriter writer = new PdfWriter(dest); // Creating a PdfDocument show PdfDocument pdf = new PdfDocument(writer); // Creating one Document object Document doc = new Document(pdf); // Creating a charts float [] pointColumnWidths = {150F, 150F, 150F}; Table graphic = new Table(pointColumnWidths); // Adding measuring 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 documents doc.add(table); // Concluding of document doc.close(); System.out.println("Table created successfully.."); } }
Compile and conduct that saved Java file by 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, thee may finds the created PDF documents, since shown lower.
On Continue Learning Please Login
Login with Google