Saturday 1 October 2016

Itext: Set indentation of list item

By using 'setSymbolIndent' method, you can set the indentation between the symbol and the ListItem.

list.setSymbolIndent(36); // Half inch, 1 inch = 72
list.setAutoindent(false);

Above two statements sets the indentation of list item to half inch.

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Arrays;

import com.itextpdf.text.Chunk;
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 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");

 private static java.util.List<Integer> countriesNumber = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 2, 3, 5,
   7, 11, 13, 17, 19, 23, 29, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 2, 3, 5,
   7, 11, 13, 17, 19, 23, 29);

 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, List.ALPHABETICAL);

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

  list.setSymbolIndent(36); // Half inch, 1 inch = 72
  list.setAutoindent(false);

  int counter = 0;

  for (String country : countries) {
   ListItem item = new ListItem(country);
   item.setListSymbol(new Chunk(countriesNumber.get(counter++) + ""));
   list.add(item);
  }

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

 }
}



Previous                                                 Next                                                 Home

No comments:

Post a Comment