How do I create an ordered list in iText 8?

Creating an ordered list in iText 8 involves creating a List object and adding ListItem objects to it, similarly to creating an unordered list.

For automatic numbering or bullets in a list, you’ll have to use ListNumberingType with the appropriate configuration. For example, ListNumberingType.DECIMAL for Arabic number (1, 2, 3, etc.) and ListNumberingType.ENGLISH_LOWER for English lower case alphabet (a, b, c, etc.).

Here’s an example:

package org.kodejava.itext;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.element.List;
import com.itextpdf.layout.element.ListItem;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.properties.ListNumberingType;

public class OrderedListExample {
    public static void main(String[] args) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter("output.pdf"));
        try (Document doc = new Document(pdfDoc)) {
            // Create a List object and set numbering type
            List list = new List(ListNumberingType.DECIMAL);

            // Add ListItems
            list.add(new ListItem("First item"));
            list.add(new ListItem("Second item"));
            list.add(new ListItem("Third item"));
            list.add(new ListItem("Fourth item"));

            // Add the list to our document
            doc.add(list);
        }
    }
}

In this example, we’re passing ListNumberingType.DECIMAL to the List constructor. This will number our list items as follows: “1.”, “2.”, “3.”, etc.

Other options you’ll find in the ListNumberingType enum are:

  • DECIMAL_LEADING_ZERO
  • ENGLISH_LOWER
  • ENGLISH_UPPER
  • ROMAN_LOWER
  • ROMAN_UPPER
  • GREEK_LOWER
  • GREEK_UPPER
  • ZAPF_DINGBATS_1
  • ZAPF_DINGBATS_2
  • ZAPF_DINGBATS_3
  • ZAPF_DINGBATS_4

Maven Dependencies

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext-core</artifactId>
    <version>8.0.2</version>
    <type>pom</type>
</dependency>

Maven Central

Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.