How do I set Table’s cell padding in iText?

You can set the cell’s padding using the com.itextpdf.text.pdf.PdfPCell‘s setPadding() method. To set the padding individually you can use the setPaddingTop(), setPaddingRight(), setPaddingBottom(), and setPaddingLeft() methods to set the top, right, bottom and left padding respectively.

package org.kodejava.itextpdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

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

public class TableCellPadding {
    public static void main(String[] args) {
        Document doc = new Document();
        try {
            PdfWriter.getInstance(doc, new FileOutputStream("TableCellPadding.pdf"));
            doc.open();

            int numColumns = 3;
            PdfPTable table = new PdfPTable(numColumns);
            PdfPCell[] cells = new PdfPCell[numColumns];
            for (int i = 0; i < numColumns; i++) {
                cells[i] = new PdfPCell(new Phrase("Cell " + i + 1));
                // Set cell's padding equally for all side of the
                // cell.
                cells[i].setPadding(10);
                table.addCell(cells[i]);
            }
            table.completeRow();

            cells = new PdfPCell[numColumns];
            for (int i = 0; i < numColumns; i++) {
                cells[i] = new PdfPCell(new Phrase("Cell " + i + 1));
                // Set cell's padding individually for top, right,
                // bottom and left padding.
                cells[i].setPaddingTop(20);
                cells[i].setPaddingRight(30);
                cells[i].setPaddingBottom(20);
                cells[i].setPaddingLeft(30);
                table.addCell(cells[i]);
            }
            table.completeRow();
            doc.add(table);
        } catch (DocumentException | FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            doc.close();
        }
    }
}

Maven Dependencies

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.3</version>
</dependency>

Maven Central

How do I create Table cell that span multiple columns in iText?

To create a cell in a table that span into multiple columns you can use the setColspan() method in the com.itextpdf.text.pdf.PdfPCell object. The example below show you how to do it.

package org.kodejava.itextpdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

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

public class TableColumnSpanDemo {
    public static void main(String[] args) {
        Document doc = new Document();
        try {
            PdfWriter.getInstance(doc, new FileOutputStream("TableColumnSpan.pdf"));
            doc.open();

            // Creates a table with four column. The first rows
            // will have cell 1 to cell 4.
            PdfPTable table = new PdfPTable(4);
            table.addCell(new PdfPCell(new Phrase("Cell 1")));
            table.addCell(new PdfPCell(new Phrase("Cell 2")));
            table.addCell(new PdfPCell(new Phrase("Cell 3")));
            table.addCell(new PdfPCell(new Phrase("Cell 4")));
            table.completeRow();

            // Creates another row that only have to columns.
            // The width of cell 5 and cell 6 span two columns
            // in width.
            PdfPCell cell5 = new PdfPCell(new Phrase("Cell 5"));
            cell5.setColspan(2);
            PdfPCell cell6 = new PdfPCell(new Phrase("Cell 6"));
            cell6.setColspan(2);
            table.addCell(cell5);
            table.addCell(cell6);
            table.completeRow();

            // Adds table to the doc
            doc.add(table);
        } catch (DocumentException | FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            doc.close();
        }
    }
}

Maven Dependencies

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.3</version>
</dependency>

Maven Central

How do I set the width of a Table in iText?

We can set the width of a table using the setWidthPercentage() method of the com.itextpdf.text.pdf.PdfPTable class. This method accept a float value as a parameter. This method sets the width in a percentage of the width a table will occupy in the page. The default table’s width is 80%.

Let’s see an example on the following code:

package org.kodejava.itextpdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

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

public class TableWidthDemo {
    public static void main(String[] args) {
        Document doc = new Document();
        try {
            PdfWriter.getInstance(doc, new FileOutputStream("TableWidthDemo.pdf"));
            doc.open();

            // Creates a table with 5 columns
            PdfPTable table = new PdfPTable(5);
            table.addCell(new PdfPCell(new Phrase("Cell 1")));
            table.addCell(new PdfPCell(new Phrase("Cell 2")));
            table.addCell(new PdfPCell(new Phrase("Cell 3")));
            table.addCell(new PdfPCell(new Phrase("Cell 4")));
            table.addCell(new PdfPCell(new Phrase("Cell 5")));

            // Sets the width percentage that the table will occupy
            // in the page.
            table.setWidthPercentage(50);
            doc.add(table);
        } catch (DocumentException | FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            doc.close();
        }
    }
}

Maven Dependencies

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.3</version>
</dependency>

Maven Central

How do I set column width of a Table in iText?

The width of a table column defined relatively between each column. On the following example we define an array floats that stores the column widths. We define the second column twice as big as the first column and the third column is three times bigger than the first column.

To set the width we call the table‘s setWidths() method. This method can accept an array of floats or an array of integers. Here is an example code:

package org.kodejava.itextpdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

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

public class TableColumnWidthDemo {
    public static void main(String[] args) {
        Document doc = new Document();
        try {
            PdfWriter.getInstance(doc, new FileOutputStream("TableColumnWidth.pdf"));
            doc.open();

            PdfPTable table = new PdfPTable(4);
            table.addCell(new PdfPCell(new Phrase("Cell 1")));
            table.addCell(new PdfPCell(new Phrase("Cell 2")));
            table.addCell(new PdfPCell(new Phrase("Cell 3")));
            table.addCell(new PdfPCell(new Phrase("Cell 4")));

            // Defiles the relative width of the columns
            float[] columnWidths = new float[]{10f, 20f, 30f, 10f};
            table.setWidths(columnWidths);

            doc.add(table);
        } catch (DocumentException | FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            doc.close();
        }
    }
}

Maven Dependencies

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.3</version>
</dependency>

Maven Central

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

This example shows how to create a table in a PDF document. Using the iText PDF library we can use the PdfPTable and the PdfPCell classes to create table and cells in our PDF document. In the following example we use array of strings to define the table’s data.

Let’s see the complete code snippet below:

package org.kodejava.itextpdf;

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

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

public class TableDemo {
    public static void main(String[] args) {
        String[] headers = new String[]{"No", "Username", "First Name", "Last Name"};
        String[][] rows = 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("TableDemo.pdf"));
            document.open();

            // Create an instance of PdfPTable. After that we transform
            // the header and rows 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.
            Font fontHeader = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
            Font fontRow = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL);

            PdfPTable table = new PdfPTable(headers.length);
            for (String header : headers) {
                PdfPCell cell = new PdfPCell();
                cell.setGrayFill(0.9f);
                cell.setPhrase(new Phrase(header.toUpperCase(), fontHeader));
                table.addCell(cell);
            }
            table.completeRow();

            for (String[] row : rows) {
                for (String data : row) {
                    Phrase phrase = new Phrase(data, fontRow);
                    table.addCell(new PdfPCell(phrase));
                }
                table.completeRow();
            }

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

Maven Dependencies

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.3</version>
</dependency>

Maven Central