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
PdfDocumentobject is created to write to a PDF file named “output.pdf”. - A
Documentobject is created, which represents an actual PDF document. - A
Listobject is created, representing the unordered list. ListItemobjects representing individual list items are added to the List.- Finally, the
Listis added to theDocument, and theDocumentis 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.4</version>
<type>pom</type>
</dependency>

