iText PDFtable example
In the previous example, we demonstrated the use of PDFStamper class in the IText library. In this example, we will demonstrate how we can use the PDFTable
to improve the design of the PDF Document and to customize the Document layout with a tabular structure.
Let’s start by setting up the project. We create a simple Maven project in the Eclipse. Replace the content of pom.xml
with the below given contents:
1. Project Set-Up
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>PDFTableExample</groupId> <artifactId>PDFTableExample</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.6</version> </dependency> <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15on</artifactId> <version>1.52</version> </dependency> </dependencies> </project>
This will load the required Itext
libraries and its corresponding dependencies into the project. Now that the project is setup, lets start with the demonstration and actual code writing.
Here’s the class that will create a new PdfTable
in the PDF Document.
CreatePDFTable.java
package com.jcg.examples; import java.io.FileNotFoundException; import java.io.FileOutputStream; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Phrase; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; public class CreatePDFTable { public static void main(String[] args) { Document document = new Document(); try { @SuppressWarnings("unused") PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("CreateTable.pdf")); document.open(); PdfPTable pdfPTable =new PdfPTable(2); PdfPCell pdfCell1 = new PdfPCell(new Phrase("Cell-1")); PdfPCell pdfCell2 = new PdfPCell(new Phrase("Cell-12")); pdfPTable.addCell(pdfCell1); pdfPTable.addCell(pdfCell2); PdfPCell pdfCell3 = new PdfPCell(new Phrase("Cell-21")); pdfCell3.setColspan(2); pdfCell3.setBackgroundColor(BaseColor.DARK_GRAY); pdfCell3.setBorderColor(BaseColor.RED); pdfCell3.setRotation(90); pdfPTable.addCell(pdfCell3); pdfPTable.setWidthPercentage(70); document.add(pdfPTable); document.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } document.open(); } }
We start by creating an instance of com.itextpdf.text.Document
. We pass this instance to the PDFWriter
along-with the path and name of the Document to be created. Next, we create an instance of com.itextpdf.text.pdf.PdfPTable
. This table may have multiple cells, with each cell having its own properties, which can be managed at an elemental level.
We have used PdfPTable#setWidthPercentage
to set the table width relative to the document. Cells are created by creating instances of com.itextpdf.text.pdf.PdfPCell
. We can set the cell border color using the PdfPCell#setBorderColor
. Similarly we can set the background color using the PdfPCell#setBackgroundColor
.
The width of the cells can be adjusted using the setWidths
method. We can increase the size of a colspan
using the setColspan
method.
The text in the cell can also be rotated if needed by using setRotation
method. Similarly, we can set the cell-padding, cell-aligment, cell-indentation etc.
Once the cell instance is ready, it can be attached to the parent table. This table is then added to the document. When the document is closed the table is flushed to PDF.
We can create a nested table by creating a cell in a table and adding a new table in the cell.
Here’s how the PDFPTable
looks in a document when the above class is run:
2. Download the Source Code
In this example we demonstrated how we can use the Itext PDFPTable class to improve the design and precision of the PDF document.
You can download the source code of this example here: PDFTableExample.zip