Create table in pdf using iText

Create table in pdf using iText

If you are using iText to generate PDF document and want to include table in it please follow below steps:

Important: You need iText…jar you could download in bottom download link or can also download directly from its web site here

Below are steps to create table in PDF:

  • Create document
  • Call PdfWriter.getInstance method and pass document object, OutputStream(File name)
  • Open document for writing
  • Create PdfPTable object and add PdfPCell to the table

Please have below sample code:

package com.javahonk.createpdf;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
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 CreateTablePDF {

    private static Font TIME_ROMAN = 
            new Font(Font.FontFamily.TIMES_ROMAN, 18,Font.BOLD);
    private static Font TIME_ROMAN_SMALL = 
            new Font(Font.FontFamily.TIMES_ROMAN, 12,Font.BOLD);

    /**
     * @param args
     */
    public static void main(String[] args) {

        try {
            Document document = new Document();
            PdfWriter.getInstance(document, 
                    new FileOutputStream("C:/PDF/CreateTable.pdf"));
            document.open();
            addMetaData(document);
            addTitlePage(document);         
            createTable(document);          
            document.close();

        } catch (FileNotFoundException | DocumentException e) {         
            e.printStackTrace();
        }

    }

    private static void addMetaData(Document document) {
        document.addTitle("Create PDF table");
        document.addSubject("Create PDF table");
        document.addAuthor("Java Honk");
        document.addCreator("Java Honk");
    }

    private static void addTitlePage(Document document)
            throws DocumentException {

        Paragraph preface = new Paragraph();
        creteEmptyLine(preface, 1);
        preface.add(new Paragraph("PDF table sample", TIME_ROMAN));

        creteEmptyLine(preface, 1);
        SimpleDateFormat simpleDateFormat = 
                new SimpleDateFormat("MM/dd/yyyy");
        preface.add(new Paragraph("Table created on "+ 
                simpleDateFormat.format(new Date()),TIME_ROMAN_SMALL));
        document.add(preface);

    }

    private static void creteEmptyLine(Paragraph paragraph, int number) {
        for (int i = 0; i < number; i++) {
            paragraph.add(new Paragraph(" "));
        }
    }

    private static void createTable(Document document) 
            throws DocumentException {      

        Paragraph paragraph = new Paragraph();
        creteEmptyLine(paragraph, 2);
        document.add(paragraph);
        PdfPTable table = new PdfPTable(3);     

        PdfPCell c1 = new PdfPCell(new Phrase("First Name"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Last Name"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Test"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);
        table.setHeaderRows(1);

        for (int i = 0; i < 5; i++) {
            table.getDefaultCell().setHorizontalAlignment(
                    Element.ALIGN_CENTER);
            table.getDefaultCell().setVerticalAlignment(
                    Element.ALIGN_MIDDLE);
            table.addCell("Java");
            table.addCell("Honk");
            table.addCell("Success");
        }       

        document.add(table);
    }

}

 

Output PDF:

Create table in pdf using iText

 

Dynamically generate PDF using servlet Download iTextJar

Leave a Reply

Your email address will not be published. Required fields are marked *