List object in cell

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

8th November 2015
admin-marketing

Switch code for this example

ListInCell.java
  1. /**
  2.  * This example was written by Bruno Lowagie for a prospective customer.
  3.  * The code in this sample works with the latest version of iText.
  4.  * It doesn't work with versions predating iText 5.
  5.  */
  6. package sandbox.tables;
  7.  
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.List;
  11. import com.itextpdf.text.ListItem;
  12. import com.itextpdf.text.Paragraph;
  13. import com.itextpdf.text.Phrase;
  14. import com.itextpdf.text.pdf.PdfPCell;
  15. import com.itextpdf.text.pdf.PdfPTable;
  16. import com.itextpdf.text.pdf.PdfWriter;
  17.  
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21.  
  22. import sandbox.WrapToTest;
  23.  
  24. @WrapToTest
  25. public class ListInCell {
  26.  
  27.     public static final String DEST = "results/tables/list_in_cell.pdf";
  28.    
  29.     public static void main(String[] args) throws IOException, DocumentException {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new ListInCell().createPdf(DEST);
  33.     }
  34.    
  35.     public void createPdf(String dest) throws IOException, DocumentException {
  36.         Document document = new Document();
  37.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  38.         document.open();
  39.        
  40.         // This is how not to do it (but it works anyway):
  41.        
  42.         // We create a list:
  43.         List list = new List();                
  44.         list.add(new ListItem("Item 1"));
  45.         list.add(new ListItem("Item 2"));
  46.         list.add(new ListItem("Item 3"));
  47.        
  48.         // We wrap this list in a phrase:    
  49.         Phrase phrase = new Phrase();
  50.         phrase.add(list);
  51.         // We add this phrase to a cell
  52.         PdfPCell phraseCell = new PdfPCell();
  53.         phraseCell.addElement(phrase);          
  54.        
  55.         // We add the cell to a table:
  56.         PdfPTable phraseTable = new PdfPTable(2);
  57.         phraseTable.setSpacingBefore(5);
  58.         phraseTable.addCell("List wrapped in a phrase:");
  59.         phraseTable.addCell(phraseCell);
  60.  
  61.         // We wrap the phrase table in another table:
  62.         Phrase phraseTableWrapper = new Phrase();
  63.         phraseTableWrapper.add(phraseTable);
  64.        
  65.         // We add these nested tables to the document:
  66.         document.add(new Paragraph("A list, wrapped in a phrase, wrapped in a cell, wrapped in a table, wrapped in a phrase:"));
  67.         document.add(phraseTableWrapper);
  68.        
  69.         // This is how to do it:
  70.        
  71.         // We add the list directly to a cell:
  72.         PdfPCell cell = new PdfPCell();
  73.         cell.addElement(list);
  74.        
  75.         // We add the cell to the table:
  76.         PdfPTable table = new PdfPTable(2);
  77.         table.setSpacingBefore(5);
  78.         table.addCell("List placed directly into cell");
  79.         table.addCell(cell);
  80.        
  81.         // We add the table to the document:
  82.         document.add(new Paragraph("A list, wrapped in a cell, wrapped in a table:"));
  83.         document.add(table);
  84.        
  85.         // Avoid adding tables to phrase (but it works anyway):
  86.        
  87.         Phrase tableWrapper = new Phrase();
  88.         tableWrapper.add(table);document.add(new Paragraph("A list, wrapped in a cell, wrapped in a table, wrapped in a phrase:"));
  89.         document.add(tableWrapper);
  90.        
  91.         document.close();
  92.     }
  93. }
NestedListHtml.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/26755315/how-can-i-convert-xhtml-nested-list-to-pdf-with-itext
  4.  */
  5. package sandbox.xmlworker;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Element;
  11. import com.itextpdf.text.Paragraph;
  12. import com.itextpdf.text.pdf.PdfPCell;
  13. import com.itextpdf.text.pdf.PdfPTable;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15. import com.itextpdf.tool.xml.ElementList;
  16. import com.itextpdf.tool.xml.XMLWorker;
  17. import com.itextpdf.tool.xml.XMLWorkerHelper;
  18. import com.itextpdf.tool.xml.html.Tags;
  19. import com.itextpdf.tool.xml.parser.XMLParser;
  20. import com.itextpdf.tool.xml.pipeline.css.CSSResolver;
  21. import com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline;
  22. import com.itextpdf.tool.xml.pipeline.end.ElementHandlerPipeline;
  23. import com.itextpdf.tool.xml.pipeline.html.HtmlPipeline;
  24. import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext;
  25.  
  26. import java.io.File;
  27. import java.io.FileInputStream;
  28. import java.io.FileOutputStream;
  29. import java.io.IOException;
  30. import sandbox.WrapToTest;
  31.  
  32. /**
  33.  *
  34.  * @author Bruno Lowagie (iText Software)
  35.  */
  36. @WrapToTest
  37. public class NestedListHtml {
  38.    
  39.     public static final String HTML = "resources/xml/list.html";
  40.     public static final String DEST = "results/xmlworker/nested_list.pdf";
  41.    
  42.     public static void main(String[] args) throws IOException, DocumentException {
  43.         File file = new File(DEST);
  44.         file.getParentFile().mkdirs();
  45.         new NestedListHtml().createPdf(DEST);
  46.     }
  47.    
  48.    
  49.     public void createPdf(String file) throws IOException, DocumentException {
  50.        
  51.         // Parse HTML into Element list
  52.        
  53.         // CSS
  54.         CSSResolver cssResolver =
  55.                 XMLWorkerHelper.getInstance().getDefaultCssResolver(true);
  56.        
  57.         // HTML
  58.         HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
  59.         htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
  60.         htmlContext.autoBookmark(false);
  61.        
  62.         // Pipelines
  63.         ElementList elements = new ElementList();
  64.         ElementHandlerPipeline end = new ElementHandlerPipeline(elements, null);
  65.         HtmlPipeline html = new HtmlPipeline(htmlContext, end);
  66.         CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
  67.        
  68.         // XML Worker
  69.         XMLWorker worker = new XMLWorker(css, true);
  70.         XMLParser p = new XMLParser(worker);
  71.         p.parse(new FileInputStream(HTML));
  72.        
  73.         // step 1
  74.         Document document = new Document();
  75.         PdfWriter.getInstance(document, new FileOutputStream(file));
  76.         document.open();
  77.         for (Element e : elements) {
  78.             document.add(e);
  79.         }
  80.         document.add(Chunk.NEWLINE);
  81.         Paragraph para = new Paragraph();
  82.         for (Element e : elements) {
  83.             para.add(e);
  84.         }
  85.         document.add(para);
  86.         document.add(Chunk.NEWLINE);
  87.         PdfPTable table = new PdfPTable(2);
  88.         table.addCell("Nested lists don't work in a cell");
  89.         PdfPCell cell = new PdfPCell();
  90.         for (Element e : elements) {
  91.             cell.addElement(e);
  92.         }
  93.         table.addCell(cell);
  94.         document.add(table);
  95.        
  96.         document.close();
  97.     }
  98.        
  99. }
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