HTML Lists

Tags: XML WorkerXHTMLListList in tableiText 5
Files: 
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/34488212
 */
package sandbox.xmlworker;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class List_Dirty {
    public static final String DEST = "results/xmlworker/list_dirty.pdf";
    public static final String HTML = "resources/xml/list_dirty.html";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new List_Dirty().createPdf(DEST);
    }
 
    /**
     * Creates a PDF with the words "Hello World"
     * @param file
     * @throws IOException
     * @throws DocumentException
     */
    public void createPdf(String file) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        writer.setInitialLeading(12);
        // step 3
        document.open();
        // step 4
        XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                new FileInputStream(HTML));
        // step 5
        document.close();
    }
}
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/34488212
 */
package sandbox.xmlworker;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Utilities;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class List_Clean {
    public static final String DEST = "results/xmlworker/list_clean.pdf";
    public static final String HTML = "resources/xml/list_clean.html";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new List_Clean().createPdf(DEST);
    }
 
    /**
     * Creates a PDF with the words "Hello World"
     * @param file
     * @throws IOException
     * @throws DocumentException
     */
    public void createPdf(String file) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        writer.setInitialLeading(12);
        // step 3
        document.open();
        // step 4
        XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                new FileInputStream(HTML));
 
        String html = Utilities.readFileToString(HTML);
        String css = "ul { list-style: disc } li { padding: 10px }";
        PdfPTable table = new PdfPTable(1);
        table.setSpacingBefore(20);
        PdfPCell cell = new PdfPCell();
        for (Element e : XMLWorkerHelper.parseToElementList(html, css)) {
            cell.addElement(e);
        }
        table.addCell(cell);
        document.add(table);
        // step 5
        document.close();
    }
}
File nameRaw URLUpdated
List_Dirty.javaList_Dirty.java2015-12-28 11:46 am
List_Clean.javaList_Clean.java2015-12-28 11:46 am
Resources: 
<div style="text-align: center"><b><span style="font-size: large">Terms and Conditions</span></b></div>
<div>*<span class="Apple-tab-span" style="white-space: pre"> </span>Prices are in AED</div>
<div><br /></div>
<div>*<span class="Apple-tab-span" style="white-space: pre"> </span>All Credit Card transactions are subject to a 3.25% processing fee</div>
<div><br /></div>
<div>*<span class="Apple-tab-span" style="white-space: pre"> </span>In the event production is required per customer request, 50% of the entire bill will be due prior to start of production, and the <span class="Apple-tab-span" style="white-space: pre"> </span>balance due upon delivery.</div>
<div><br /></div>
<div>*<span class="Apple-tab-span" style="white-space: pre"> </span>All furniture will be delivered in A+ condition. In the event that the equipment is damaged, the renter shall be liable for all <span class="Apple-tab-span" style="white-space: pre"> </span>repair costs to restore the equipment to its state at the beginning of the rental period.</div>
<div><br /></div>
<div>*<span class="Apple-tab-span" style="white-space: pre"> </span>Equipment shall be utilized for the stated purpose and at the stated location only.</div>
<div style="text-align: center"><b><span style="font-size: large">Terms and Conditions</span></b></div>
<ul>
<li>Prices are in AED</li>
<li>All Credit Card transactions are subject to a 3.25% processing fee</li>
<li>In the event production is required per customer request, 50% of the entire bill will be due prior to start of production, and the balance due upon delivery.</li>
<li>All furniture will be delivered in A+ condition. In the event that the equipment is damaged, the renter shall be liable for all repair costs to restore the equipment to its state at the beginning of the rental period.</li>
<li>Equipment shall be utilized for the stated purpose and at the stated location only.</li>
</ul>
File nameRaw URLUpdated
list_dirty.htmllist_dirty.html2015-12-28 11:47 am
list_clean.htmllist_clean.html2015-12-28 11:47 am
Results: 
File nameRaw URLUpdated
cmp_list_dirty.pdfcmp_list_dirty.pdf2015-12-28 11:49 am
cmp_list_clean.pdfcmp_list_clean.pdf2015-12-28 11:49 am