How to Create Lists And SubLists In Pdf in java


In this core java tutorial we will learn How to Create Lists And SubLists In Pdf in java using iText library - core java tutorial with program and examples.

First create List (com.itextpdf.text.List) i.e. using (new List(true); Or new List(List.ORDERED);)
you may make it unordered list as well depending on your requirement and

before adding sublist in list add ListItems which will act as heading for sublist and then create and add sublist i.e. unordered list (again you may create sublist as ordered or unordered depending on your requirement)
but remember to give pass second argument of List while defining sublist like (sublist = new List(List.UNORDERED, 20)), second argument passed as 20 here is space between between bullet and ListItem added in sublist.

We will generate PDF files in java using iText library. PDF files can be generated dynamically in java using iText library.

How to Create Roman List In pdf in java - iText java tutorial

Create ZapfDingbats List In pdf in java - iText java tutorial program

How to Create Ordered List In pdf in java using iText - iText java tutorial

Creating the UnOrdered List In pdf in java - iText java tutorial example




Download jars required to execute program >

Or, you may download jars from here.

Program/Example How to Create List And SubList In Pdf in java  - Using iText library - core java tutorial

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.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
/**
*
*  How to Create List And SubList In Pdf - Example Using iText library - core java tutorial
*
*/
public class CreateListAndSubListInPdfExample {
   public static void main(String[] args) {
          try {
                 String pdfFilePath = "E:/Create List and SubList In Pdf Example.pdf";
                
                 OutputStream fos = new FileOutputStream(new File(pdfFilePath));
                 Document document = new Document();
                 PdfWriter.getInstance(document, fos);
                 document.open();
                 document.add(new Paragraph("This is List and SubList example"));
                
                 //Create List and Sublist
                 List list = new List(List.ORDERED); //create List (OrderedList)
                 List sublist = null; //create SubList reference variable
                 list.add(new ListItem("Item 1"));
                 sublist = new List(List.UNORDERED, 20); //create SubList (UnOrderedList)
                                     //20 is space between bullet and list items
                 sublist.add("Item 1 - SubItem 1");
                 sublist.add("Item 1 - SubItem 1");
                 list.add(sublist);
                 list.add(new ListItem("Item 2"));
                 sublist = new List(List.UNORDERED, 20); //create SubList (UnOrderedList)
                                     //20 is space between bullet and list items
                 sublist.add("Item 2 - SubItem 1");
                 sublist.add("Item 2 - SubItem 1");
                 list.add(sublist);
                 document.add(list);
                 document.close();             
                 fos.close();
                
                 System.out.println("PDF created in >> "+ pdfFilePath);
          } catch (Exception e) {
                 e.printStackTrace();
          }
   }
}
/* OUTPUT of above program/Example -
PDF created in >> E:/Create List and SubList In Pdf Example.pdf
*/


PDF formed after executing above java program(How to Create Lists And SubLists In Pdf in java) will look like this >


Summary -
So in this core java tutorial we learned How to Create Lists And SubLists In Pdf in java using iText library in java.
First we created List (new List(true); Or new List(List.ORDERED);) and then before adding sublist in list we added ListItems which acted as heading for sublist and then created and added sublist i.e. unordered list, and passed second argument of List while defining sublist like (sublist = new List(List.UNORDERED, 20)), second argument passed as 20 here was space between between bullet and ListItem added in sublist.



Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

References >



RELATED LINKS>

iText library tutorial in java - How to Create and work with PDF files in java


Creating first pdf and setting attributes using itext in java - iText tutorial >

Create the first Pdf Example Using iText library in java - iText java tutorial example



Creating TABLES in pdf in java - iText tutorial >

How to Create the Table In Pdf in java - iText java tutorial

How to do the Cell Alignment In Pdf generated table in Java program

How to Set Cell Background Colour In Pdf Table in java example


Creating PASSWORD PROTECTED PDF in java - iText tutorial >

How to Create Password Protected Pdf in java - iText Example



Creating IMAGES in pdf in java - iText tutorial >

Adding the Images In Pdf in java example using iText - iText java tutorial

How to Add And Rotate Images In Pdf in java - iText java tutorial example

How can you Add the Images from URL In Pdf in java program





Adding WATERMARK in pdf in java - iText tutorial >

How to add TEXT as Watermark In Pdf in java - iText java tutorial

How to add IMAGE as Watermark In Pdf in iText in java example




Create new page, set page height and width in java - iText tutorial >

How To Create New Pages In Pdf Using Itext - iText java tutorial example

How to Find Pdf Page Height And Width in java - iText java tutorial program



HEADER and FOOTER in pdf in java - iText tutorial >

How To Set Header and Footer in pdf in java using Itext Example - iText java tutorial



BAR AND PIE CHARTS in pdf in java - iText tutorial >

How to create Bar Chart In Pdf in java - iText java tutorial

Create Pie Chart In Pdf in java using itext - iText java tutorial



MODIFY/ EDIT pdf in java - iText tutorial >

How to Modify - Add Text To Existing PDF in java - iText java tutorial example

How To Read And Add Image To Existing Pdf in java Example - iText java tutorial



Solve common Exceptions in itext in java - iText tutorial >

How to solve the Document Has No Pages IOException in iText in java



eEdit
Must read for you :