XML Worker for HTML snippets

Tags: XML Workerrich textXHTMLiText 5

Examples written in answer to questions such as"

Files: 
/*
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/27015644/add-a-rich-textbox-to-pdf-using-itextsharp
 */
package sandbox.xmlworker;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.FontFactory;
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.FileOutputStream;
import java.io.IOException;
 
import sandbox.WrapToTest;
 
@WrapToTest
public class HtmlContentForCell {
 
    public static final String HTML = "<p>Overview&#160;line1</p>"
            + "<p>Overview&#160;line2</p><p>Overview&#160;line3</p>"
            + "<p>Overview&#160;line4</p><p>Overview&#160;line4</p>"
            + "<p>Overview&#160;line5&#160;</p>";
    public static final String CSS = "p { font-family: Cardo; }";
    public static final String DEST = "results/xmlworker/html_in_cell.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new HtmlContentForCell().createPdf(DEST);
    }
 
    public void createPdf(String file) throws IOException, DocumentException {
        FontFactory.register("resources/fonts/Cardo-Regular.ttf");
 
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(file));
        // step 3
        document.open();
        // step 4
        PdfPTable table = new PdfPTable(2);
        table.addCell("Some rich text:");
        PdfPCell cell = new PdfPCell();
        for (Element e : XMLWorkerHelper.parseToElementList(HTML, CSS)) {
            cell.addElement(e);
        }
        table.addCell(cell);
        document.add(table);
        // step 5
        document.close();
    }
 
}
/*
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/29390428/itext-why-the-last-item-of-an-unordered-html-bullet-list-is-not-showing-on-my-p
 */
package sandbox.xmlworker;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
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.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class HtmlContentForCell2 {
 
    public static final String HTML = "<ul><li>Overview&#160;line1</li>"
            + "<li>Overview&#160;line2</li><li>Overview&#160;line3</li>"
            + "<li>Overview&#160;line4</li><li>Overview&#160;line5</li></ul>";
    public static final String DEST = "results/xmlworker/html_in_cell2.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new HtmlContentForCell2().createPdf(DEST);
    }
 
    public void createPdf(String file) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(file));
        // step 3
        document.open();
        // step 4
        PdfPTable table = new PdfPTable(2);
        table.addCell("Some rich text:");
        PdfPCell cell = new PdfPCell();
        for (Element e : XMLWorkerHelper.parseToElementList(HTML, null)) {
            cell.addElement(e);
        }
        table.addCell(cell);
        document.add(table);
        // step 5
        document.close();
    }
 
}
/*
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/29390428/itext-why-the-last-item-of-an-unordered-html-bullet-list-is-not-showing-on-my-p
 */
package sandbox.xmlworker;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
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.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class HtmlContentForCell3 {
 
    public static final String HTML = "<html>" +
        " <head></head>" +
        " <body>" +
        "  <span>List</span>" +
        "  <ul>" +
        "   <li>document2.txt.txt (23 B)</li>" +
        "   <li>document1.txt.txt (12 B)</li>" +
        "   <li>document3.txt.txt (27 B)</li>" +
        "  </ul>" +
        " </body>" +
        "</html>";
    public static final String DEST = "results/xmlworker/html_in_cell3.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new HtmlContentForCell3().createPdf(DEST);
    }
 
    public void createPdf(String file) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(file));
        // step 3
        document.open();
        // step 4
        PdfPTable table = new PdfPTable(2);
        table.addCell("Some rich text:");
        PdfPCell cell = new PdfPCell();
        for (Element e : XMLWorkerHelper.parseToElementList(HTML, null)) {
            cell.addElement(e);
        }
        table.addCell(cell);
        document.add(table);
        // step 5
        document.close();
    }
 
}
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/35429441
 */
package sandbox.xmlworker;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.ElementList;
import com.itextpdf.tool.xml.XMLWorkerHelper;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
// No @WrapToTest because Windows fonts are used
public class ParseHtml13 {
    public static final String DEST = "results/xmlworker/test-herin.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ParseHtml13().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
        String html = "<div><span class=\"bluetextwhitebackground\">zzz</span></div>"
            + "<div>"
            + "<span class=\"bluetextwhitebackground\">Test</span> "
            + "<span class=\"redtext\">Description</span> "
            + "<span class=\"italicpurple\">Other Color</span>"
            + "</div>";
        String css = ".bluetextwhitebackground { font-family: times; color: white; background: blue}"
                + ".redtext { font-family: times; color: red; }"
                + ".italicpurple { font-family: times; font-style: italic; color: purple }";
        ElementList elements = XMLWorkerHelper.parseToElementList(html, css);
        PdfPTable table = new PdfPTable(1);
        PdfPCell cell = new PdfPCell();
        for (Element e : elements) {
            cell.addElement(e);
        }
        table.addCell(cell);
        document.add(table);
        // step 5
        document.close();
    }
}
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/35429441
 */
package sandbox.xmlworker;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.ElementList;
import com.itextpdf.tool.xml.XMLWorkerHelper;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
// No @WrapToTest because Windows fonts are used
public class ParseHtml14 {
    public static final String DEST = "results/xmlworker/test-herin2.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ParseHtml14().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
        String html = "<b style=\"font-family: Arial, Verdana; font-size: 10pt; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; color: rgb(204, 255, 255); background-color: rgb(51, 102, 255);\">zzz&nbsp;</b>"
                + "<div style=\"font-family: Arial, Verdana; font-size: 10pt; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal;\"><b style=\"color: rgb(204, 255, 255); background-color: rgb(51, 102, 255);\"><br /></b></div>"
                + "<div><b style=\"font-family: Arial, Verdana; font-size: 10pt; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; color: rgb(204, 255, 255); background-color: rgb(51, 102, 255);\">Test &nbsp;</b> &nbsp; "
                + "<span style=\"color: rgb(255, 0, 0);\">&nbsp;<span style=\"font-weight: bold;\">Description</span> &nbsp;</span>"
                + "<span style=\"color: rgb(153, 51, 153); font-style: italic;\">Other Color</span>"
                + "</div>";
        ElementList elements = XMLWorkerHelper.parseToElementList(html, null);
        PdfPTable table = new PdfPTable(1);
        PdfPCell cell = new PdfPCell();
        for (Element e : elements) {
            cell.addElement(e);
        }
        table.addCell(cell);
        document.add(table);
        // step 5
        document.close();
    }
}