How do I create an unordered list in iText 8?

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

Here is an example of how you can do this:

package org.kodejava.itext;

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

public class UnorderedListExample {
    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
            List list = new List();
            list.setListSymbol("\u2022 ");

            // 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 code:

  • A PdfDocument object is created to write to a PDF file named “output.pdf”.
  • A Document object is created, which represents an actual PDF document.
  • A List object is created, representing the unordered list.
  • ListItem objects representing individual list items are added to the List.
  • Finally, the List is added to the Document, and the Document is automatically closed by the try-with-resource block, and flush any remaining content to the PDF.

You may put different things in your ListItem objects, and nest other List objects within them, if you wish.

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.