iText supports various ways in which list can be displayed in a PDF. The List can be a simple list, numbered, list with symbols, no symbols list and many more. Lets see examples on how to add List in PDF using iText.
Important class – com.itextpdf.text.List
Below are few useful methods of the List class
a)
setIndentationLeft(float indentation) Sets the indentation of this paragraph on the left side.
b) setIndentationRight(float indentation) Sets the indentation of this paragraph on the right side.
c) setNumbered(boolean numbered) Sets numbering to the list
d) setLettered(boolean lettered) Starts the list elements with letters- a,b,c etc
e) setListSymbol(String symbol) Sets the symbol of the list elements. Default is – . If you wish not to have a symbol then use blank.
Example
The example below illustrates use of most of the above methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
package com.kscodes.examples.itext; import java.io.FileOutputStream; import com.itextpdf.text.Document; import com.itextpdf.text.List; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; public class ListExample1 { public static void main(String args[]) { // Create a Document object Document document = new Document(); try { // Create a PdfWriter instance and use the document to write the pdf // to a specified location PdfWriter.getInstance(document, new FileOutputStream("K:\\Kscodes\\pdf\\itext-list.pdf")); // open the document document.open(); // basic list example Paragraph para1 = new Paragraph("Simple List Example"); document.add(para1); List simpleList = new List(); simpleList.add("One"); simpleList.add("Two"); simpleList.add("Three"); document.add(simpleList); // Numbered List example Paragraph para2 = new Paragraph("Numbered List Example - Indentation Left"); para2.setSpacingBefore(30f); document.add(para2); List numberedList = new List(); numberedList.setIndentationLeft(60f); numberedList.setNumbered(true); numberedList.add("One"); numberedList.add("Two"); numberedList.add("Three"); document.add(numberedList); // No Symbol list example Paragraph para3 = new Paragraph("No Symbol List Example"); para3.setSpacingBefore(30f); document.add(para3); List noSymbolList = new List(); noSymbolList.setListSymbol(""); noSymbolList.add("One"); noSymbolList.add("Two"); noSymbolList.add("Three"); document.add(noSymbolList); // No Symbol list example Paragraph para4 = new Paragraph("Lettered List Example"); para4.setSpacingBefore(30f); document.add(para4); List letteredList = new List(); letteredList.setLettered(true); letteredList.add("One"); letteredList.add("Two"); letteredList.add("Three"); document.add(letteredList); // close the document document.close(); System.out.println("PDF created at the location !!!"); } catch (Exception e) { System.out.println("Exception occured :: " + e); } } } |
Output