In the following example we are going to create a table with multiple header. We will create table header with columns that spans in multiple columns and rows.
Here are the steps:
- Create a
PdfWriter
object calledwriter
with the output filename. - Create a
PpdfDocument
object calledpdf
, and pass thewriter
object as parameter. - Using
try-with-resource
block, create aDocument
object with thepdf
object as constructor argument. - Instantiate a
Table
object. In this example we define it with five columns, and set the width of the table to take the full page width. - Add table header cell using
addHeaderCell()
method. We add cell that spans multiple rows or multiple columns using theCell
constructor parametersrowspan
andcolspan
. - Add some rows of data to the table.
- Finally, we add the
table
object to thedocument
.
And here is the full code snippet:
package org.kodejava.itext;
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.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.UnitValue;
import com.itextpdf.layout.properties.VerticalAlignment;
import java.io.FileNotFoundException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class TableWithMultiHeader {
public static void main(String[] args) {
TableWithMultiHeader demo = new TableWithMultiHeader();
demo.createTable();
}
private void createTable() {
try {
PdfWriter writer = new PdfWriter("multi_header_table.pdf");
PdfDocument pdf = new PdfDocument(writer);
try (Document document = new Document(pdf)) {
Table table = new Table(5);
table.setWidth(UnitValue.createPercentValue(100));
table.addHeaderCell(new Cell(2, 1)
.setVerticalAlignment(VerticalAlignment.MIDDLE)
.setTextAlignment(TextAlignment.CENTER)
.add(new Paragraph("No")));
table.addHeaderCell(new Cell(2, 1)
.setVerticalAlignment(VerticalAlignment.MIDDLE)
.setTextAlignment(TextAlignment.CENTER)
.add(new Paragraph("Name")));
table.addHeaderCell(new Cell(1, 2)
.setTextAlignment(TextAlignment.CENTER)
.add(new Paragraph("Date: " + LocalDate.now()
.format(DateTimeFormatter.ofPattern("dd-MMM-yyyy")))));
table.addHeaderCell(new Cell(2, 1)
.setVerticalAlignment(VerticalAlignment.MIDDLE)
.setTextAlignment(TextAlignment.CENTER)
.add(new Paragraph("Activity")));
table.addHeaderCell(new Cell(1, 1)
.setTextAlignment(TextAlignment.CENTER)
.add(new Paragraph("Start Time")));
table.addHeaderCell(new Cell(1, 1)
.setTextAlignment(TextAlignment.CENTER)
.add(new Paragraph("End Time")));
table.addCell(new Cell()
.setTextAlignment(TextAlignment.RIGHT)
.add(new Paragraph("1")));
table.addCell(new Cell().add(new Paragraph("Alice")));
table.addCell(new Cell()
.setTextAlignment(TextAlignment.CENTER)
.add(new Paragraph("10:00")));
table.addCell(new Cell()
.setTextAlignment(TextAlignment.CENTER)
.add(new Paragraph("11:00")));
table.addCell(new Cell().add(new Paragraph("Learn ukulele basic")));
table.addCell(new Cell()
.setTextAlignment(TextAlignment.RIGHT)
.add(new Paragraph("2")));
table.addCell(new Cell().add(new Paragraph("Bob")));
table.addCell(new Cell()
.setTextAlignment(TextAlignment.CENTER)
.add(new Paragraph("09:00")));
table.addCell(new Cell()
.setTextAlignment(TextAlignment.CENTER)
.add(new Paragraph("11:00")));
table.addCell(new Cell()
.add(new Paragraph("Learn piano basic")));
document.add(table);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Running the code will give you the result shown in the image below:
Maven Dependencies
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-core</artifactId>
<version>8.0.4</version>
<type>pom</type>
</dependency>