java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to create a table in PDF document?

This example shows how to create a table in a PDF document. Using the iText library we can use the PdfPTable and the PdfPCell to create table and table cell in our pdf document. The example use data in an array format as the content to be formatted in the table.

package com.javacoderanch.example.lowagie;

import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPCell;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.File;

public class PDFTableDemo {
    public static void main(String[] args) {
        String[] headers = new String[] {"NO", "USERNAME", "FIRST NAME", "LAST NAME"};
        String[][] data = new String[][] {
                {"1", "JDOW", "JOHN", "DOW"},
                {"2", "STIGER", "SCOTT", "TIGER"},
                {"3", "FBAR", "FOO", "BAR"}
        };

        //
        // Create a new document.
        //
        Document document = new Document(PageSize.LETTER.rotate());

        try {
            //
            // Get an instance of PdfWriter and create a Table.pdf file as an output.
            //
            PdfWriter.getInstance(document, new FileOutputStream(new File("Table.pdf")));
            document.open();

            //
            // Create an instance of PdfPTable. After that we transform the header and
            // data array into a PdfPCell object. When each table row is complete we
            // have to call the table.completeRow() method.
            //
            // For better presentation we also set the cell font name, size and weight.
            // And we also define the background fill for the cell.
            //
            PdfPTable table = new PdfPTable(headers.length);
            for (int i = 0; i < headers.length; i++) {
                String header = headers[i];
                PdfPCell cell = new PdfPCell();
                cell.setGrayFill(0.9f);
                cell.setPhrase(new Phrase(header.toUpperCase(), new Font(Font.HELVETICA, 10, Font.BOLD)));
                table.addCell(cell);
            }
            table.completeRow();

            for (int i = 0; i < data.length; i++) {
                for (int j = 0; j < data[i].length; j++) {
                    String datum = data[i][j];
                    PdfPCell cell = new PdfPCell();
                    cell.setPhrase(new Phrase(datum.toUpperCase(), new Font(Font.HELVETICA, 10, Font.NORMAL)));
                    table.addCell(cell);
                }
                table.completeRow();
            }

            document.addTitle("Table Demo");
            document.add(table);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
    }
}