Java – iText PDF Table (Structure & Style)

java-itext-pdfUsing iText for Java we can create PDF documents. Here we will see how to build a table in the PDF document and how to style it.

 

 

Objective:

  • Add table to PDF document using iText?

Environment & Tools:

  • Eclipse (or other IDE)
  • Maven (optional)

Libraries:

  • iText 5.4.2
<dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.4.2</version>
    </dependency>

Final Output:

The PDF document will contain the following table:

( 1 ) Table Structure

  • Create 6-column-table
  • Set the width of the table to 100%
  • Set the width of each cell “relatively”
  • Create table header “Title with blue background”
  • Create table cells label “with gray background” value “with white background”
  • TableBuilder.java
package com.hmkcode;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

public class TableBuilder {
     // create table
    public static PdfPTable createTable() throws DocumentException {

        // create 6 column table
        PdfPTable table = new PdfPTable(6);

        // set the width of the table to 100% of page
        table.setWidthPercentage(100);

        // set relative columns width
        table.setWidths(new float[]{0.6f, 1.4f, 0.8f,0.8f,1.8f,2.6f});

        // ----------------Table Header "Title"----------------
        // font
        Font font = new Font(FontFamily.HELVETICA, 14, Font.BOLD, BaseColor.WHITE);
        // create header cell
        PdfPCell cell = new PdfPCell(new Phrase("HMKCODE.com - iText PDFTable Example",font));
        // set Column span "1 cell = 6 cells width"
        cell.setColspan(6);
        // set style
        Style.headerCellStyle(cell);
        // add to table
        table.addCell(cell);

        //-----------------Table Cells Label/Value------------------

        // 1st Row
        table.addCell(createLabelCell("Label 1"));
        table.addCell(createValueCell("Value 1"));
        table.addCell(createLabelCell("Label 2"));
        table.addCell(createValueCell("Value 2"));
        table.addCell(createLabelCell("Label 3"));
        table.addCell(createValueCell("Value 3"));

        // 2nd Row
        table.addCell(createLabelCell("Label 4"));
        table.addCell(createValueCell("Value 4"));
        table.addCell(createLabelCell("Label 5"));
        table.addCell(createValueCell("Value 5"));
        table.addCell(createLabelCell("Label 6"));
        table.addCell(createValueCell("Value 6"));

        return table;
    }

    // create cells
    private static PdfPCell createLabelCell(String text){
        // font
        Font font = new Font(FontFamily.HELVETICA, 8, Font.BOLD, BaseColor.DARK_GRAY);

        // create cell
        PdfPCell cell = new PdfPCell(new Phrase(text,font));

        // set style
        Style.labelCellStyle(cell);
        return cell;
    }

    // create cells
    private static PdfPCell createValueCell(String text){
        // font
        Font font = new Font(FontFamily.HELVETICA, 8, Font.NORMAL, BaseColor.BLACK);

        // create cell
        PdfPCell cell = new PdfPCell(new Phrase(text,font));

        // set style
        Style.valueCellStyle(cell);
        return cell;
    }
}

( 2 ) Cells Style

Style for header, label & value cells.

  • Style.java
package com.hmkcode;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.PdfPCell;

public class Style {

	public static void headerCellStyle(PdfPCell cell){

	// alignment
	cell.setHorizontalAlignment(Element.ALIGN_CENTER);

	// padding
        cell.setPaddingTop(0f);
        cell.setPaddingBottom(7f);

        // background color
        cell.setBackgroundColor(new BaseColor(0,121,182));

        // border
        cell.setBorder(0);
        cell.setBorderWidthBottom(2f);

	}
	public static void labelCellStyle(PdfPCell cell){
	// alignment
	cell.setHorizontalAlignment(Element.ALIGN_LEFT);
    	cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

    	// padding
    	cell.setPaddingLeft(3f);
        cell.setPaddingTop(0f);

        // background color
        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);

        // border
        cell.setBorder(0);
        cell.setBorderWidthBottom(1);
        cell.setBorderColorBottom(BaseColor.GRAY);

        // height
	    cell.setMinimumHeight(18f);
	}

	public static void valueCellStyle(PdfPCell cell){
	// alignment
	cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

	    // padding
	    cell.setPaddingTop(0f);
	    cell.setPaddingBottom(5f);

	    // border
	    cell.setBorder(0);
	    cell.setBorderWidthBottom(0.5f);

	    // height
	    cell.setMinimumHeight(18f);
	}
}

( 3 ) Run

package com.hmkcode;

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

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;

public class App 
{
    public static void main( String[] args ) throws FileNotFoundException, DocumentException
    {
    	// step 1
    	Document document = new Document();
    	document.setPageSize(PageSize.A4);

    	// step 2
    	PdfWriter.getInstance(document, new FileOutputStream("pdf.pdf"));

    	// step 3
        document.open();

        // step 4 create PDF contents
        document.add(TableBuilder.createTable());

        //step 5
        document.close();

        System.out.println( "PDF Created!" );
    }  
}

Source Code @ GitHub