iText API- Ordered and Unordered List Example in Java


In this tutorial, we are going to create an ORDERED and UNORDERED list in the PDF document. iText API have class List inside the  com.itextpdf.text  package that handles the list type data in the PDF file.

Related Post- iText API – Creating table in PDF using Java

Get the JAR Dependencies

To resolve the JAR dependencies you can add following in your pom.xml

pom.xml
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.10</version>
</dependency>

Steps in Mind

1- Create the object of the Document class.

Document document = new Document();

2- Get the instance of PdfWriter.

PdfWriter.getInstance(document, new FileOutputStream("iText/list.pdf"));

3- Open the document.

document.open();

4- Create List class object and pass the List type.

List list = new List(List.ORDERED); // List list = new List(List.UNORDERED);

5- Add the ListItem into the list.

list.add(new ListItem("Google"));

6- Add all list into the document.

document.add(list);

7 – Close the document.

document.close();

Ordered Type List

This example will help you to create an ordered list in the PDF document.

OrderedListExample.java
package org.websparrow.itext;

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

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

public class OrderedListExample {
	public static void main(String[] args) throws FileNotFoundException, DocumentException {

		Document document = new Document();
		PdfWriter.getInstance(document, new FileOutputStream("iText/orderedlist.pdf"));
		document.open();

		Paragraph paragraph = new Paragraph("Ordered list has been created - WebSparrow.org\n");
		document.add(paragraph);

		List list = new List(List.ORDERED);
		list.add(new ListItem("Apple"));
		list.add(new ListItem("Google"));
		list.add(new ListItem("Microsoft"));
		list.add(new ListItem("Facebook"));

		document.add(list);
		document.close();

		System.out.println("List Created Successfully...");
	}
}

Output :

iText API- Ordered and Unordered List Example in Java

Unordered Type List

This example will help you to create an unordered list in the PDF document.

UnorderedListExample.java
package org.websparrow.itext;

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

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

public class UnorderedListExample {
	public static void main(String[] args) throws FileNotFoundException, DocumentException {

		Document document = new Document();
		PdfWriter.getInstance(document, new FileOutputStream("iText/unorderedlist.pdf"));
		document.open();

		Paragraph paragraph = new Paragraph("Unordered list has been created - WebSparrow.org\n");
		document.add(paragraph);

		List list = new List(List.UNORDERED);

		list.add(new ListItem("Apple"));
		list.add(new ListItem("Google"));
		list.add(new ListItem("Microsoft"));
		list.add(new ListItem("Facebook"));

		document.add(list);
		document.close();

		System.out.println("List Created Successfully...");
	}
}

Output :

iText API- Ordered and Unordered List Example in Java

Related Post- iText API‐ Protect PDF Document with Password in Java


Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.