java iText list

The List represents a list of items and can be ordered or unordered. It is represented by com.itextpdf.text.List class.

Steps:

1. Create Document instance. It represents the current document to which we are adding content.
2. Create OutputStream instance. It represents the generated pdf.
3. Create PDFWriter instance and pass Document and OutputStream instance to its constructor.
4. Open the Document by calling document.open().
5. Add the content to the document by calling document.add() method using List object.
6. Close the document by calling document.close() method.

Example:

PDFListExample.java

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.pdf.PdfWriter;
 
/**
 * This class is used to create a pdf file using iText jar.
 * @author w3schools
 */
public class PDFListExample {
  public static void main(String args[]){
    try {
	//Create Document instance.
	Document document = new Document();
 
	//Create OutputStream instance.
	OutputStream outputStream = 
	    new FileOutputStream(new File("D:\\TestListFile.pdf"));
 
	//Create PDFWriter instance.
        PdfWriter.getInstance(document, outputStream);
 
        //Open the document.
        document.open();
 
        //Create ordered list object
        List orderedList = new List(List.ORDERED);
        orderedList.add(new ListItem("Oredered List item1"));
        orderedList.add(new ListItem("Oredered List item2"));
 
        //Create unordered list object
        List unorderedList = new List(List.UNORDERED);
        unorderedList.add(new ListItem("Unoredered List item1"));
        unorderedList.add(new ListItem("Unoredered List item2"));
 
        //Add content to the document using List objects.
        document.add(orderedList);
        document.add(unorderedList);
 
        //Close document and outputStream.
        document.close();
        outputStream.close();
 
        System.out.println("Pdf created successfully.");
    } catch (Exception e) {
	e.printStackTrace();
    }
  }
}

Output:

Pdf created successfully.

Download this example.
 
Next Topic: Java iText table.
Previous Topic: Java iText anchor.