List object in cell

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

31st May 2016
iText PDF

Switch code for this example

ListInCell.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. package com.itextpdf.samples.sandbox.tables;
  9.  
  10. import com.itextpdf.kernel.geom.PageSize;
  11. import com.itextpdf.kernel.pdf.PdfDocument;
  12. import com.itextpdf.kernel.pdf.PdfWriter;
  13. import com.itextpdf.layout.Document;
  14. import com.itextpdf.layout.element.*;
  15. import com.itextpdf.samples.GenericTest;
  16. import com.itextpdf.test.annotations.type.SampleTest;
  17.  
  18. import org.junit.experimental.categories.Category;
  19.  
  20. import java.io.File;
  21.  
  22.  
  23. @Category(SampleTest.class)
  24. public class ListInCell extends GenericTest {
  25.     public static final String DEST = "./target/test/resources/sandbox/tables/list_in_cell.pdf";
  26.  
  27.     public static void main(String[] args) throws Exception {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new ListInCell().manipulatePdf(DEST);
  31.     }
  32.  
  33.     @Override
  34.     protected void manipulatePdf(String dest) throws Exception {
  35.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  36.         // Note that it is not necessary to create new PageSize object,
  37.         // but for testing reasons (connected to parallelization) we call constructor here
  38.         Document doc = new Document(pdfDoc, new PageSize(PageSize.A4).rotate());
  39.  
  40.         // This is how not to do it (but it works anyway):
  41.         // We create a list:
  42.         List list = new List();
  43.         list.add(new ListItem("Item 1"));
  44.         list.add(new ListItem("Item 2"));
  45.         list.add(new ListItem("Item 3"));
  46.  
  47.         Cell phraseCell = new Cell();
  48.         phraseCell.add(list);
  49.  
  50.         // We add the cell to a table:
  51.         Table phraseTable = new Table(2);
  52.         phraseTable.setMarginTop(5);
  53.         phraseTable.addCell("List wrapped in a phrase:");
  54.         phraseTable.addCell(phraseCell);
  55.  
  56.         // We add these nested tables to the document:
  57.         doc.add(new Paragraph("A list, wrapped in a phrase, wrapped in a cell, " +
  58.                 "wrapped in a table, wrapped in a phrase:"));
  59.         phraseTable.setMarginTop(5);
  60.         doc.add(phraseTable);
  61.  
  62.  
  63.         // We add the list directly to a cell:
  64.         Cell cell = new Cell();
  65.         cell.add(list);
  66.  
  67.         // We add the cell to the table:
  68.         Table table = new Table(2);
  69.         table.setMarginTop(5);
  70.         table.addCell("List placed directly into cell");
  71.         table.addCell(cell);
  72.  
  73.         // We add the table to the document:
  74.         doc.add(new Paragraph("A list, wrapped in a cell, wrapped in a table:"));
  75.         doc.add(table);
  76.  
  77.         // Avoid adding tables to phrase (but it works anyway):
  78.  
  79.         doc.add(new Paragraph("A list, wrapped in a cell, wrapped in a table, wrapped in a phrase:"));
  80.         table.setMarginTop(5);
  81.         doc.add(table);
  82.  
  83.         doc.close();
  84.     }
  85. }
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