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

The width of a table column defined relatively between each columns. On the following example we define an array floats that stores the column widths. We define the second column twice as bigger 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.example.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 document = new Document();
        try {
            PdfWriter.getInstance(document,
                    new FileOutputStream("TableColumnWidth.pdf"));
            document.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);

            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.9</version>
</dependency>

How do I create a List in iText?

You can create a list in iText using the com.itextpdf.text.List. This class represent a list. The list item is created using the com.itextpdf.text.ListItem. You can create an ordered list or unordered list. To create ordered list pass the List.ORDERED as the parameter to class List. To create an unordered list pass the List.UNORDERED.

Let’s see an example below:

package org.kodejava.example.itextpdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.pdf.PdfWriter;

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

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

            List ordered = new List(List.ORDERED);
            ordered.add(new ListItem("Item 1"));
            ordered.add(new ListItem("Item 2"));
            ordered.add(new ListItem("Item 3"));
            document.add(ordered);

            List unordered = new List(List.UNORDERED);
            unordered.add(new ListItem("Item 1"));
            unordered.add(new ListItem("Item 2"));
            unordered.add(new ListItem("Item 3"));
            document.add(unordered);
        } catch (DocumentException | FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
    }
}

Maven Dependencies

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

How do I create Roman or Greek numeral list in iText?

To create a Roman or Greek numeral list we can use special implementation list in the iText. The com.itextpdf.text.RomanList represent a Roman numeral list while the com.itextpdf.text.GreekList represent a Greek numeral list.

The list item can be created using the com.itextpdf.text.ListItem class. Let’s check out an example below:

package org.kodejava.example.itextpdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.GreekList;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.RomanList;
import com.itextpdf.text.pdf.PdfWriter;

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

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

            List roman = new RomanList();
            roman.add(new ListItem("Item 1"));
            roman.add(new ListItem("Item 2"));
            roman.add(new ListItem("Item 3"));
            document.add(roman);

            List greek = new GreekList();
            greek.add(new ListItem("Item 1"));
            greek.add(new ListItem("Item 2"));
            greek.add(new ListItem("Item 3"));
            document.add(greek);
        } catch (DocumentException | FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
    }
}

Maven Dependencies

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

How do I create internal Anchor in iText?

The com.itextpdf.text.Anchor class in iText can be used to create an internal link or external link in a PDF document. To create an internal link we must format the anchor reference using the # + referenceName. On the other side the target anchor should be named using the same reference name excluding the # symbol.

To set the reference we use the setReference() method. To define the target anchor we can name the anchor using the setName() method.

package org.kodejava.example.itextpdf;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

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

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

            Anchor anchor = new Anchor("[Continue Here]");
            anchor.setReference("#targetLink");
            Paragraph para1 = new Paragraph(
                    "The quick brown fox jumps over the lazy dog. ");
            para1.add(anchor);
            document.add(para1);

            Anchor target = new Anchor(
                    "The quick onyx goblin jumps over the lazy dwarf.");
            anchor.setName("targetLink");
            Paragraph para2 = new Paragraph();
            para2.setSpacingBefore(550);
            para2.add(target);
            document.add(para2);

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

Maven Dependencies

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

How do I create Chapter in iText?

This brief example show you how to create chapter in the PDF document using iText. To create chapter we use the com.itextpdf.text.Chapter class. We can pass the chapter title and the chapter number as the parameter for the Chapter constructor.

To create a section for the chapter we can use the com.itextpdf.text.Section class. To add a section we call the Chapter.addSection() method of the Chapter object. Let’s look at the example below:

package org.kodejava.example.itextpdf;

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

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

public class PDFChapterDemo {
    public static void main(String[] args) {
        // Creates a new document
        Document document = new Document();
        try {
            // Prepare PDF writer and open the document.
            PdfWriter.getInstance(document,
                    new FileOutputStream("PDFChapterDemo.pdf"));
            document.open();

            // Creates a new Chapter object
            Chapter chapter = new Chapter("Chapter One", 1);

            // Add sections to the chapter
            Section section = chapter.addSection("This is Section 1", 2);
            Paragraph paragraph = new Paragraph("This is the paragraph of the Section 1");
            section.add(paragraph);

            chapter.addSection("This is Section 2", 2);

            document.add(chapter);
        } catch (DocumentException | FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
    }
}

Maven Dependencies

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

How do I create Anchor or link in iText?

This example show you how to use the com.itextpdf.text.Anchor class to create an external link in the PDF document. You can create an instance of Anchor with a phrase of string and set its reference, in this case a website URL, by calling the setReference() method of this object.

package org.kodejava.example.itextpdf;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

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

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

            String content = "You can learn Java programming on the " +
                    "following website: ";
            Paragraph paragraph = new Paragraph(content);

            // Creates a new anchor that link to external website
            // and add this anchor to the paragraph.
            Anchor anchor = new Anchor("Learn Java Programming by Examples");
            anchor.setReference("http://www.kodejava.org");
            paragraph.add(anchor);

            document.add(paragraph);
        } catch (DocumentException | FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }

    }
}

Maven Dependencies

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

How do I set paragraph indentation in iText?

To set paragraph indentation in the iText Paragraph object we can use the setFirstLineIndent(), setIndentationLeft() and setIndentationRight(). These methods accept a float value as a parameter.

The setFirstLineIndent() method set the first line indentation of a paragraph, while the setIndentationLeft() and setIndentationRight() methods set the left and the right indent of the paragraph. Let see an example below:

package org.kodejava.example.itextpdf;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

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

public class ParagraphIndentationDemo {
    private static final String CONTENT =
            "Lorem ipsum dolor sit amet, consectetur adipisicing " +
                    "elit, sed do eiusmod tempor incididunt ut labore et " +
                    "dolore magna aliqua. Ut enim ad minim veniam, quis " +
                    "nostrud exercitation ullamco laboris nisi ut aliquip " +
                    "ex ea commodo consequat. Duis aute irure dolor in " +
                    "reprehenderit in voluptate velit esse cillum dolore eu " +
                    "fugiat nulla pariatur. Excepteur sint occaecat cupidatat " +
                    "non proident, sunt in culpa qui officia deserunt mollit " +
                    "anim id est laborum.";

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

            Paragraph paragraph = new Paragraph();
            paragraph.add(new Chunk(ParagraphIndentationDemo.CONTENT));

            // Set paragraph's first line indent
            paragraph.setFirstLineIndent(75);

            // Set paragraph's left side indent
            paragraph.setIndentationLeft(50);

            // Set paragraph's right side indent
            paragraph.setIndentationRight(25);
            document.add(paragraph);
        } catch (DocumentException | FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
    }
}

Maven Dependencies

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

How do I set paragraph alignment in iText?

To set the alignment of a paragraph object we can use the Paragraph.setAlignment() method. We can pass constants such as Paragraph.ALIGN_LEFT, Paragraph.ALIGN_CENTER, Paragraph.ALIGN_RIGHTto the setAlignment() method.

package org.kodejava.example.itextpdf;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

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

public class ParagraphAlignment {
    private static final String CONTENT =
            "Lorem ipsum dolor sit amet, consectetur adipisicing " +
                    "elit, sed do eiusmod tempor incididunt ut labore et " +
                    "dolore magna aliqua. Ut enim ad minim veniam, quis " +
                    "nostrud exercitation ullamco laboris nisi ut aliquip " +
                    "ex ea commodo consequat. Duis aute irure dolor in " +
                    "reprehenderit in voluptate velit esse cillum dolore eu " +
                    "fugiat nulla pariatur. Excepteur sint occaecat cupidatat " +
                    "non proident, sunt in culpa qui officia deserunt mollit " +
                    "anim id est laborum.";

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

            // Creates a check for the paragraphs contents
            Chunk chunk = new Chunk(ParagraphAlignment.CONTENT);

            //
            // Creates paragraphs and set the alignment of the paragraph.
            // We use the Paragraph.ALIGN_LEFT, Paragraph.ALIGN_CENTER
            // and Paragraph.ALIGN_RIGHT
            //
            Paragraph para1 = new Paragraph(chunk);
            para1.setAlignment(Paragraph.ALIGN_LEFT);
            para1.setSpacingAfter(50);
            document.add(para1);

            Paragraph para2 = new Paragraph(chunk);
            para2.setAlignment(Paragraph.ALIGN_CENTER);
            para2.setSpacingAfter(50);
            document.add(para2);

            Paragraph para3 = new Paragraph(chunk);
            para3.setAlignment(Paragraph.ALIGN_RIGHT);
            document.add(para3);

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

Maven Dependencies

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

How do I set line space in Paragraph?

To set the line spacing in a paragraph can be done by passing the line space / leading argument in the Paragraph constructor. In the example below we set the line space to 32. We can also set the space between paragraph by calling the setSpacingBefore() and setSpacingAfter() methods of this object.

package org.kodejava.example.itextpdf;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

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

public class ParagraphSpaceDemo {
    public static void main(String[] args) {
        Document document = new Document();

        try {
            String name = "ParagraphSetting.pdf";
            FileOutputStream fos = new FileOutputStream(name);
            PdfWriter.getInstance(document, fos);
            document.open();

            String content = "The quick brown fox jumps over the lazy dog";

            // Setting paragraph line spacing to 32
            Paragraph para1 = new Paragraph(32);

            // Setting the space before and after the paragraph
            para1.setSpacingBefore(50);
            para1.setSpacingAfter(50);
            for (int i = 0; i < 10; i++) {
                para1.add(new Chunk(content));
            }
            document.add(para1);

            Paragraph para2 = new Paragraph();
            for (int i = 0; i < 10; i++) {
                para2.add(new Chunk(content));
            }
            document.add(para2);

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

Maven Dependencies

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

How do I use iText Paragraph class?

The com.itextpdf.text.Paragraph class represent a paragraph a PDF document. The following example show you how to create a simple paragraph. First we create a Paragraph object and then add some text into it using the Chunk object.

package org.kodejava.example.itextpdf;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

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

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

            String content = "The quick brown fox jumps over the lazy dog";
            Paragraph paragraph = new Paragraph();
            for (int i = 0; i < 20; i++) {
                Chunk chunk = new Chunk(content);
                paragraph.add(chunk);
            }

            document.add(paragraph);
            document.close();
        } catch (DocumentException | FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

Maven Dependencies

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