How do I set Cell border width and color in iText?

The following example show you how to set the cell’s border width and color attributes. We can set the width and color at once using the setBorderWidth() and setBorderColor(). Or you can set it individually for each side of the cell’s border. You can also set the background color of a cell using the setBackgroundColor() method.

package org.kodejava.itextpdf;

import com.itextpdf.text.BaseColor;
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 TableCellBorderDemo {
    public static void main(String[] args) {
        Document doc = new Document();
        try {
            PdfWriter.getInstance(doc, new FileOutputStream("TableCellBorder.pdf"));
            doc.open();

            PdfPTable table = new PdfPTable(3);
            PdfPCell cell1 = new PdfPCell(new Phrase("Cell 1"));
            cell1.setUseBorderPadding(true);

            // Setting cell's border width and color
            cell1.setBorderWidth(5f);
            cell1.setBorderColor(BaseColor.BLUE);
            table.addCell(cell1);

            PdfPCell cell2 = new PdfPCell(new Phrase("Cell 2"));
            cell2.setUseBorderPadding(true);

            // Setting cell's background color
            cell2.setBackgroundColor(BaseColor.GRAY);

            // Setting cell's individual border color
            cell2.setBorderWidthTop(1f);
            cell2.setBorderColorTop(BaseColor.RED);
            cell2.setBorderColorRight(BaseColor.GREEN);
            cell2.setBorderColorBottom(BaseColor.BLUE);
            cell2.setBorderColorLeft(BaseColor.BLACK);
            table.addCell(cell2);

            PdfPCell cell3 = new PdfPCell(new Phrase("Cell 3"));
            cell3.setUseBorderPadding(true);

            // Setting cell's individual border width
            cell3.setBorderWidthTop(2f);
            cell3.setBorderWidthRight(1f);
            cell3.setBorderWidthBottom(2f);
            cell3.setBorderWidthLeft(1f);
            table.addCell(cell3);
            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 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 set Table’s cell alignment in iText?

We can set table cell’s content alignment horizontally and vertically. To set the horizontal alignment we use the setHorizontalAlignment() method. To align it vertically we use the setVerticalAlignment() method. The alignment constant is defined in the com.itextpdf.text.Element class.

package org.kodejava.itextpdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
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 TableCellAlignment {
    public static void main(String[] args) {
        Document doc = new Document();
        try {
            PdfWriter.getInstance(doc, new FileOutputStream("TableCellAlignment.pdf"));
            doc.open();

            // Setting table's cells horizontal alignment
            PdfPTable table = new PdfPTable(3);
            PdfPCell cell1 = new PdfPCell(new Phrase("Cell 1"));
            cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
            table.addCell(cell1);
            PdfPCell cell2 = new PdfPCell(new Phrase("Cell 2"));
            cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell2);
            PdfPCell cell3 = new PdfPCell(new Phrase("Cell 3"));
            cell3.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(cell3);
            table.completeRow();

            // Setting table's cells vertical alignment
            PdfPCell[] cells = new PdfPCell[3];
            int[] alignments = new int[]{
                    Element.ALIGN_TOP,
                    Element.ALIGN_MIDDLE,
                    Element.ALIGN_BOTTOM
            };
            for (int i = 0; i < cells.length; i++) {
                cells[i] = new PdfPCell(new Phrase("Cell " + (i + 1)));
                cells[i].setMinimumHeight(50);
                cells[i].setVerticalAlignment(alignments[i]);
                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