How do I create a table in PDF document using iText 8?

When it comes to generating PDF documents dynamically, iText 8 is a powerful and versatile Java library that provides a wide range of functionalities. One common requirement in PDF generation is the need to include tables to present structured data. In this blog post, we will explore how to create a table in a PDF document using the iText 8 library.

The Table class in iText 8 is a layout element that represents data in a two-dimensional grid. It allows you to create tables with rows and columns to organize and display data in a structured format.

To create a table using iText 8, we would first need to create an instance of Document class where the table will be added. We then define a Table object either by passing the number columns, or an array of float for the column width as parameter.

Here is how you can create a table with iText 8:

package org.kodejava.itext;

import com.itextpdf.kernel.colors.DeviceGray;
import com.itextpdf.kernel.colors.DeviceRgb;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.element.Cell;


public class CreateTable {
    public static void main(String[] args) throws FileNotFoundException {
        String destination = "table_example.pdf";
        PdfWriter writer = new PdfWriter(destination);

        PdfDocument pdf = new PdfDocument(writer);
        try (Document document = new Document(pdf)) {

            float[] pointColumnWidths = {150F, 200F, 100F};
            Table table = new Table(pointColumnWidths);

            // Add header cells to the table
            table.addHeaderCell(new Cell().add(new Paragraph("Id"))
            table.addHeaderCell(new Cell().add(new Paragraph("Name"))
            table.addHeaderCell(new Cell().add(new Paragraph("Age"))

            // Add cells to the table
            table.addCell(new Cell().add(new Paragraph("1")));
            table.addCell(new Cell().add(new Paragraph("Alice")));
            table.addCell(new Cell().add(new Paragraph("20")));

            table.addCell(new Cell().add(new Paragraph("2")));
            table.addCell(new Cell().add(new Paragraph("Bob")));
            table.addCell(new Cell().add(new Paragraph("25")));

            // Add table to document

This will create a new document with a table including three columns and three rows. The first row is the table header, we style it with a gray background, set the font weight to bold and center aligned the text.

iText 8 Table Example

Maven Dependencies


Maven Central

How do I change an applet background color?

By default, the applet will usually have a grey background when displayed in a web browser. If you want to change it then you can call the setBackground(java.awt.Color) method and choose the color you want. Defining the background color in the init() method will change the color as soon as the applet initialized.

package org.kodejava.applet;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;

public class BackgroundColorApplet extends Applet {
    public void init() {
        // Here we change the default grey color background of an 
        // applet to yellow background.

    public void paint(Graphics g) {
        g.drawString("Applet background example", 0, 50);

Now we have the applet code ready. To enable the web browser to execute the applet create the following html page.

<!DOCTYPE html>
<html lang="en">
    <title>Applet Background Color</title>
<applet code="org.kodejava.applet.BackgroundColorApplet"
        height="150" width="350">