Thursday, 29 September 2016

Itext: Working with List object

List is a collection of ListItem objects. As you see the implementation of ListItem class, it extends Paragraph class. So List is a collection of paragraph objects.

public class ListItem extends Paragraph {
         ....
         ....
}

ListItem
A ListItem is a Paragraph that can be added to a List.

List
List is a collection of ListItem objects. There are two kinds of lists.
a.   Ordered Lists
b.   Unordered lists

How to create Ordered List?
List list = new List(List.ORDERED);

How to create Unordered List?
List list = new List(List.UNORDERED);

Following application displays list of countries in Asia using ordered list.
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Arrays;

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

public class ListEx {
  private static java.util.List<String> countries = Arrays.asList("Afghanistan", "Armenia", "Azerbaijan", "Bahrain",
      "Bangladesh", "Bhutan", "Brunei", "Cambodia", "China", "Cyprus", "Georgia", "India", "Indonesia", "Iran",
      "Iraq", "Israel", "Japan", "Jordan", "Kazakhstan", "Kuwait", "Kyrgyzstan", "Laos", "Lebanon", "Malaysia",
      "Maldives", "Mongolia", "Myanmar (Burma)", "Nepal", "North Korea", "Oman", "Pakistan", "Palestine",
      "Philippines", "Qatar", "Russia", "Saudi Arabia", "Singapore", "South Korea", "Sri Lanka", "Syria",
      "Taiwan", "Tajikistan", "Thailand", "Timor-Leste", "Turkey", "Turkmenistan", "United Arab Emirates (UAE)",
      "Uzbekistan", "Vietnam", "Yemen");

  public static void main(String args[]) throws FileNotFoundException, DocumentException {
    Document document = new Document();

    PdfWriter.getInstance(document, new FileOutputStream("countries.pdf"));

    List list = new List(List.ORDERED);

    Paragraph paragraph = new Paragraph("List Of Countries in Asia");

    for (String country : countries) {
      list.add(country);
    }

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

  }
}


As you see by default it displays list items using numbers.


Following application displays list of countries in Asia using unordered list.
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Arrays;

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

public class ListEx {
  private static java.util.List<String> countries = Arrays.asList("Afghanistan", "Armenia", "Azerbaijan", "Bahrain",
      "Bangladesh", "Bhutan", "Brunei", "Cambodia", "China", "Cyprus", "Georgia", "India", "Indonesia", "Iran",
      "Iraq", "Israel", "Japan", "Jordan", "Kazakhstan", "Kuwait", "Kyrgyzstan", "Laos", "Lebanon", "Malaysia",
      "Maldives", "Mongolia", "Myanmar (Burma)", "Nepal", "North Korea", "Oman", "Pakistan", "Palestine",
      "Philippines", "Qatar", "Russia", "Saudi Arabia", "Singapore", "South Korea", "Sri Lanka", "Syria",
      "Taiwan", "Tajikistan", "Thailand", "Timor-Leste", "Turkey", "Turkmenistan", "United Arab Emirates (UAE)",
      "Uzbekistan", "Vietnam", "Yemen");

  public static void main(String args[]) throws FileNotFoundException, DocumentException {
    Document document = new Document();

    PdfWriter.getInstance(document, new FileOutputStream("countries.pdf"));

    List list = new List(List.UNORDERED);

    Paragraph paragraph = new Paragraph("List Of Countries in Asia");

    for (String country : countries) {
      list.add(country);
    }

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

  }
}


By default, unordered list uses – (hyphen) for each list item.







Previous                                                 Next                                                 Home

No comments:

Post a Comment