List examples

This is a code example of iText PDF, discover more.

8th November 2015
admin-marketing

Switch code for this example

ListAlignment.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/27844914/align-justified-for-itext-list-item
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Element;
  10. import com.itextpdf.text.List;
  11. import com.itextpdf.text.ListItem;
  12. import com.itextpdf.text.pdf.PdfWriter;
  13.  
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17.  
  18. import sandbox.WrapToTest;
  19.  
  20. @WrapToTest
  21. public class ListAlignment {
  22.  
  23.     public static final String DEST = "results/objects/list_alignment.pdf";
  24.    
  25.     public static void main(String[] args) throws IOException,
  26.             DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new ListAlignment().createPdf(DEST);
  30.     }
  31.    
  32.     public void createPdf(String dest) throws IOException, DocumentException {
  33.         Document document = new Document();
  34.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  35.         document.open();
  36.         String text = "test 1 2 3 ";
  37.         for (int i = 0; i < 5; i++) {
  38.             text = text + text;
  39.         }
  40.         List list = new List(List.UNORDERED);
  41.         ListItem item = new ListItem(text);
  42.         item.setAlignment(Element.ALIGN_JUSTIFIED);
  43.         list.add(item);
  44.         text = "a b c align ";
  45.         for (int i = 0; i < 5; i++) {
  46.             text = text + text;
  47.         }
  48.         item = new ListItem(text);
  49.         item.setAlignment(Element.ALIGN_JUSTIFIED);
  50.         list.add(item);
  51.         text = "supercalifragilisticexpialidocious ";
  52.         for (int i = 0; i < 3; i++) {
  53.             text = text + text;
  54.         }
  55.         item = new ListItem(text);
  56.         item.setAlignment(Element.ALIGN_JUSTIFIED);
  57.         list.add(item);
  58.         document.add(list);
  59.         document.close();
  60.     }
  61. }
ListNumbers.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/33277998/itextpdf-numbered-list-with-no-indent
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.List;
  10. import com.itextpdf.text.Paragraph;
  11. import com.itextpdf.text.pdf.PdfWriter;
  12. import java.io.File;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15. import sandbox.WrapToTest;
  16.  
  17. /**
  18.  *
  19.  * @author Bruno Lowagie (iText Software)
  20.  */
  21. @WrapToTest
  22. public class ListNumbers {
  23.  
  24.     public static final String DEST = "results/objects/list_without_indent.pdf";
  25.     public static void main(String[] args) throws IOException,
  26.             DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new ListNumbers().createPdf(DEST);
  30.     }
  31.    
  32.     public void createPdf(String dest) throws IOException, DocumentException {
  33.         Document document = new Document();
  34.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  35.         document.open();
  36.         document.add(new Paragraph("Numbered list with automatic indentation"));
  37.         List list1 = new List(List.ORDERED);
  38.         list1.setFirst(8);
  39.         for (int i = 0; i < 5; i++) {
  40.             list1.add("item");
  41.         }
  42.         document.add(list1);
  43.         document.add(new Paragraph("Numbered list without indentation"));
  44.         List list2 = new List(List.ORDERED);
  45.         list2.setFirst(8);
  46.         list2.setAlignindent(false);
  47.         for (int i = 0; i < 5; i++) {
  48.             list2.add("item");
  49.         }
  50.         document.add(list2);
  51.         document.add(new Paragraph("Nested numbered list"));
  52.         List list3 = new List(List.ORDERED);
  53.         list3.setFirst(8);
  54.         list3.setAlignindent(false);
  55.         list3.setPostSymbol(" ");
  56.         for (int i = 0; i < 5; i++) {
  57.             list3.add("item");
  58.             List list = new List(List.ORDERED);
  59.             list.setPreSymbol(String.valueOf(8 + i) + "_");
  60.             list.setPostSymbol(" ");
  61.             list.add("item 1");
  62.             list.add("item 2");
  63.             list3.add(list);
  64.         }
  65.         document.add(list3);
  66.         document.close();
  67.     }
  68.    
  69. }
ListWithImageAsBullet.java
  1. package sandbox.objects;
  2.  
  3. import com.itextpdf.text.Chunk;
  4. import com.itextpdf.text.Document;
  5. import com.itextpdf.text.DocumentException;
  6. import com.itextpdf.text.Image;
  7. import com.itextpdf.text.List;
  8. import com.itextpdf.text.pdf.PdfWriter;
  9.  
  10. import java.io.File;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13.  
  14. import sandbox.WrapToTest;
  15.  
  16. @WrapToTest
  17. public class ListWithImageAsBullet {
  18.  
  19.     public static final String IMG = "resources/images/bulb.gif";
  20.     public static final String DEST = "results/objects/list_with_image_bullet.pdf";
  21.    
  22.     public static void main(String[] args) throws IOException,
  23.             DocumentException {
  24.         File file = new File(DEST);
  25.         file.getParentFile().mkdirs();
  26.         new ListWithImageAsBullet().createPdf(DEST);
  27.     }
  28.    
  29.     public void createPdf(String dest) throws IOException, DocumentException {
  30.         Document document = new Document();
  31.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  32.         document.open();
  33.         Image image = Image.getInstance(IMG);
  34.         image.scaleAbsolute(12, 12);
  35.         image.setScaleToFitHeight(false);
  36.         List list = new List();
  37.         list.setListSymbol(new Chunk(Image.getInstance(image), 0, 0));
  38.         list.add("Hello World");
  39.         list.add("This is a list item with a lot of text. It will certainly take more than one line. This shows that the list item is indented and that the image is used as bullet.");
  40.         list.add("This is a test");
  41.         document.add(list);
  42.         document.close();
  43.     }
  44.  
  45. }
ListWithLabel.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/21579204/how-to-set-label-to-itext-list
  4.  *
  5.  * Workaround to add a label to a list.
  6.  */
  7. package sandbox.objects;
  8.  
  9. import com.itextpdf.text.Chunk;
  10. import com.itextpdf.text.Document;
  11. import com.itextpdf.text.DocumentException;
  12. import com.itextpdf.text.Element;
  13. import com.itextpdf.text.List;
  14. import com.itextpdf.text.ListItem;
  15. import com.itextpdf.text.Paragraph;
  16. import com.itextpdf.text.pdf.PdfPCell;
  17. import com.itextpdf.text.pdf.PdfPTable;
  18. import com.itextpdf.text.pdf.PdfWriter;
  19.  
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23.  
  24. import sandbox.WrapToTest;
  25.  
  26. @WrapToTest
  27. public class ListWithLabel {
  28.  
  29.     public static final String DEST = "results/objects/list_with_label.pdf";
  30.    
  31.     public static void main(String[] args) throws IOException,
  32.             DocumentException {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new ListWithLabel().createPdf(DEST);
  36.     }
  37.    
  38.     public void createPdf(String dest) throws IOException, DocumentException {
  39.         Document document = new Document();
  40.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  41.         document.open();
  42.         PdfPTable table = new PdfPTable(2);
  43.         table.setTotalWidth(200);
  44.         table.setWidths(new int[]{ 1, 10 });
  45.         table.setHorizontalAlignment(Element.ALIGN_LEFT);
  46.         PdfPCell cell;
  47.         cell = new PdfPCell();
  48.         cell.setBorder(PdfPCell.NO_BORDER);
  49.         cell.addElement(new Paragraph("Label"));
  50.         table.addCell(cell);
  51.         cell = new PdfPCell();
  52.         cell.setBorder(PdfPCell.NO_BORDER);
  53.         List list = new List(List.UNORDERED);
  54.         list.add(new ListItem(new Chunk("Value 1")));
  55.         list.add(new ListItem(new Chunk("Value 2")));
  56.         list.add(new ListItem(new Chunk("Value 3")));
  57.         cell.addElement(list);
  58.         table.addCell(cell);
  59.         document.add(table);
  60.         document.close();
  61.     }
  62. }
ListWithLeading.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/29566115/adding-veritcal-spacing-itextsharp-in-list
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Font;
  10. import com.itextpdf.text.Font.FontFamily;
  11. import com.itextpdf.text.List;
  12. import com.itextpdf.text.ListItem;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. @WrapToTest
  21. public class ListWithLeading {
  22.  
  23.     public static final String DEST = "results/objects/list_with_leading.pdf";
  24.    
  25.     public static void main(String[] args) throws IOException,
  26.             DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new ListWithLeading().createPdf(DEST);
  30.     }
  31.    
  32.     public void createPdf(String dest) throws IOException, DocumentException {
  33.         Document document = new Document();
  34.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  35.         document.open();
  36.         Font font = new Font(FontFamily.HELVETICA, 12);
  37.         List list1 = new List(12);
  38.         list1.setListSymbol("\u2022");
  39.         list1.add(new ListItem("Value 1", font));
  40.         list1.add(new ListItem("Value 2", font));
  41.         list1.add(new ListItem("Value 3", font));
  42.         document.add(list1);
  43.         List list2 = new List(12);
  44.         list2.setListSymbol("\u2022");
  45.         list2.add(new ListItem(30, "Value 1", font));
  46.         list2.add(new ListItem(30, "Value 2", font));
  47.         list2.add(new ListItem(30, "Value 3", font));
  48.         list2.setIndentationLeft(60);
  49.         document.add(list2);
  50.         document.close();
  51.     }
  52. }
RemoveListSymbol.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/27820159/itext-list-how-to-remove-symbol-from-list
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.List;
  10. import com.itextpdf.text.ListItem;
  11. import com.itextpdf.text.Paragraph;
  12. import com.itextpdf.text.Phrase;
  13. import com.itextpdf.text.pdf.PdfPCell;
  14. import com.itextpdf.text.pdf.PdfPTable;
  15. import com.itextpdf.text.pdf.PdfWriter;
  16.  
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import sandbox.WrapToTest;
  21.  
  22. @WrapToTest
  23. public class RemoveListSymbol {
  24.  
  25.     public static final String DEST = "results/objects/remove_listsymbol.pdf";
  26.    
  27.     public static void main(String[] args) throws IOException, DocumentException {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new RemoveListSymbol().createPdf(DEST);
  31.     }
  32.    
  33.     public void createPdf(String dest) throws IOException, DocumentException {
  34.         Document document = new Document();
  35.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  36.         document.open();
  37.        
  38.         // This is how not to do it (but it works anyway):
  39.        
  40.         // We create a list:
  41.         List list = new List();          
  42.         list.setListSymbol("");
  43.         list.add(new ListItem("Item 1"));
  44.         list.add(new ListItem("Item 2"));
  45.         list.add(new ListItem("Item 3"));
  46.        
  47.         // We wrap this list in a phrase:    
  48.         Phrase phrase = new Phrase();
  49.         phrase.add(list);
  50.         // We add this phrase to a cell
  51.         PdfPCell phraseCell = new PdfPCell();
  52.         phraseCell.addElement(phrase);          
  53.        
  54.         // We add the cell to a table:
  55.         PdfPTable phraseTable = new PdfPTable(2);
  56.         phraseTable.setSpacingBefore(5);
  57.         phraseTable.addCell("List wrapped in a phrase:");
  58.         phraseTable.addCell(phraseCell);
  59.  
  60.         // We wrap the phrase table in another table:
  61.         Phrase phraseTableWrapper = new Phrase();
  62.         phraseTableWrapper.add(phraseTable);
  63.        
  64.         // We add these nested tables to the document:
  65.         document.add(new Paragraph("A list, wrapped in a phrase, wrapped in a cell, wrapped in a table, wrapped in a phrase:"));
  66.         document.add(phraseTableWrapper);
  67.        
  68.         // This is how to do it:
  69.        
  70.         // We add the list directly to a cell:
  71.         PdfPCell cell = new PdfPCell();
  72.         cell.addElement(list);
  73.        
  74.         // We add the cell to the table:
  75.         PdfPTable table = new PdfPTable(2);
  76.         table.setSpacingBefore(5);
  77.         table.addCell("List placed directly into cell");
  78.         table.addCell(cell);
  79.        
  80.         // We add the table to the document:
  81.         document.add(new Paragraph("A list, wrapped in a cell, wrapped in a table:"));
  82.         document.add(table);
  83.        
  84.         // Avoid adding tables to phrase (but it works anyway):
  85.        
  86.         Phrase tableWrapper = new Phrase();
  87.         tableWrapper.add(table);document.add(new Paragraph("A list, wrapped in a cell, wrapped in a table, wrapped in a phrase:"));
  88.         document.add(tableWrapper);
  89.        
  90.         document.close();
  91.     }
  92. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now