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>