HTML Lists

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

28th December 2015
admin-marketing

Switch code for this example

List_Dirty.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/34488212
  4.  */
  5. package sandbox.xmlworker;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.pdf.PdfWriter;
  10. import com.itextpdf.tool.xml.XMLWorkerHelper;
  11.  
  12. import java.io.File;
  13. import java.io.FileInputStream;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. import sandbox.WrapToTest;
  17.  
  18. @WrapToTest
  19. public class List_Dirty {
  20.     public static final String DEST = "results/xmlworker/list_dirty.pdf";
  21.     public static final String HTML = "resources/xml/list_dirty.html";
  22.    
  23.     public static void main(String[] args) throws IOException, DocumentException {
  24.         File file = new File(DEST);
  25.         file.getParentFile().mkdirs();
  26.         new List_Dirty().createPdf(DEST);
  27.     }
  28.        
  29.     /**
  30.      * Creates a PDF with the words "Hello World"
  31.      * @param file
  32.      * @throws IOException
  33.      * @throws DocumentException
  34.      */
  35.     public void createPdf(String file) throws IOException, DocumentException {
  36.         // step 1
  37.         Document document = new Document();
  38.         // step 2
  39.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
  40.         writer.setInitialLeading(12);
  41.         // step 3
  42.         document.open();
  43.         // step 4
  44.         XMLWorkerHelper.getInstance().parseXHtml(writer, document,
  45.                 new FileInputStream(HTML));
  46.         // step 5
  47.         document.close();
  48.     }
  49. }
List_Clean.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/34488212
  4.  */
  5. package sandbox.xmlworker;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Element;
  10. import com.itextpdf.text.Utilities;
  11. import com.itextpdf.text.pdf.PdfPCell;
  12. import com.itextpdf.text.pdf.PdfPTable;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14. import com.itextpdf.tool.xml.XMLWorkerHelper;
  15.  
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import sandbox.WrapToTest;
  21.  
  22. @WrapToTest
  23. public class List_Clean {
  24.     public static final String DEST = "results/xmlworker/list_clean.pdf";
  25.     public static final String HTML = "resources/xml/list_clean.html";
  26.    
  27.     public static void main(String[] args) throws IOException, DocumentException {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new List_Clean().createPdf(DEST);
  31.     }
  32.        
  33.     /**
  34.      * Creates a PDF with the words "Hello World"
  35.      * @param file
  36.      * @throws IOException
  37.      * @throws DocumentException
  38.      */
  39.     public void createPdf(String file) throws IOException, DocumentException {
  40.         // step 1
  41.         Document document = new Document();
  42.         // step 2
  43.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
  44.         writer.setInitialLeading(12);
  45.         // step 3
  46.         document.open();
  47.         // step 4
  48.         XMLWorkerHelper.getInstance().parseXHtml(writer, document,
  49.                 new FileInputStream(HTML));
  50.        
  51.         String html = Utilities.readFileToString(HTML);
  52.         String css = "ul { list-style: disc } li { padding: 10px }";
  53.         PdfPTable table = new PdfPTable(1);
  54.         table.setSpacingBefore(20);
  55.         PdfPCell cell = new PdfPCell();
  56.         for (Element e : XMLWorkerHelper.parseToElementList(html, css)) {
  57.             cell.addElement(e);
  58.         }
  59.         table.addCell(cell);
  60.         document.add(table);
  61.         // step 5
  62.         document.close();
  63.     }
  64. }
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