How do I rotate Cell contents in iText?

You can set the rotation of the cell contents using the com.itextpdf.text.pdf.PdfPCell‘s setRotation() method. The valid rotation options are 0, 90, 180 and 270.

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 TableCellRotation {
    public static void main(String[] args) {
        Document doc = new Document();
        try {
            PdfWriter.getInstance(doc, new FileOutputStream("TableCellRotate.pdf"));
            doc.open();

            PdfPTable table = new PdfPTable(3);
            PdfPCell cell1 = new PdfPCell(new Phrase("Cell 1 - 90"));
            // 90 degree rotation
            cell1.setRotation(90);
            table.addCell(cell1);

            PdfPCell cell2 = new PdfPCell(new Phrase("Cell 2 - 180"));
            // 180 degree rotation
            cell2.setRotation(180);
            table.addCell(cell2);

            PdfPCell cell3 = new PdfPCell(new Phrase("Cell 3 - 270"));
            // 270 degree rotation
            cell3.setRotation(270);
            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 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 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 create ZapfDingbats List in iText?

This example show you how to use the com.itextpdf.text.ZapfDingbatsList class to create a ZapfDingbatsList list using the iText pdf library.

package org.kodejava.itextpdf;

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

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

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

            // Create a ZapfDingbatsList with char number 50
            List zapf1 = new ZapfDingbatsList(50);
            zapf1.add(new ListItem("Item 1"));
            zapf1.add(new ListItem("Item 2"));
            zapf1.add(new ListItem("Item 3"));
            doc.add(zapf1);

            // Create a ZapfDingbatsList with char number 55 and indent
            // it by 30.
            List zapf2 = new ZapfDingbatsList(55, 30);
            zapf2.add(new ListItem("Item 1"));
            zapf2.add(new ListItem("Item 2"));
            zapf2.add(new ListItem("Item 3"));
            doc.add(zapf2);
        } 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