Basic HTML examples

Tags: XML WorkerleadingCSSspace between linesiText 5

These examples were written in answer to questions such as:

Files: 
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/28472400/adding-pdfdiv-to-paragraph
 */
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;
import sandbox.WrapToTest;
 
/**
 *
 * @author iText
 */
@WrapToTest
public class ParseHtml {
    public static final String DEST = "results/xmlworker/html_1.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ParseHtml().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
        StringBuilder sb = new StringBuilder();
        sb.append("<div>\n<p align=\"center\">");
        sb.append("<font size=\"5\">");
        sb.append("<b>&nbsp;<font color=\"#32cd32\">My centered Para</font></b>");
        sb.append("</font>");
        sb.append("<font color=\"#32cd32\">&nbsp;</font>");
        sb.append("</p>\n</div>");
 
        PdfPTable table = new PdfPTable(1);
        PdfPCell cell = new PdfPCell();
        ElementList list = XMLWorkerHelper.parseToElementList(sb.toString(), null);
        for (Element element : list) {
            cell.addElement(element);
        }
        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/29290668/set-line-spacing-when-using-xmlworker-to-parse-html-to-pdf-itextsharp-c-sharp
 */
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;
 
/**
 *
 * @author iText
 */
@WrapToTest
public class ParseHtml5 {
    public static final String DEST = "results/xmlworker/lineheight.pdf";
    public static final String HTML = "resources/xml/lineheight.html";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ParseHtml5().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));
        // 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/29290668/set-line-spacing-when-using-xmlworker-to-parse-html-to-pdf-itextsharp-c-sharp
 */
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;
 
/**
 *
 * @author iText
 */
@WrapToTest
public class ParseHeaders {
    public static final String DEST = "results/xmlworker/headers.pdf";
    public static final String HTML = "resources/xml/headers.html";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ParseHeaders().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.setTagged();
        // 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/33795772/itext-not-generating-expected-pdf-format
 */
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;
 
// No @WrapToTest because Windows fonts are used
public class ParseHtml12 {
    public static final String DEST = "results/xmlworker/test2015-11.pdf";
    public static final String HTML = "resources/xml/test2015-11.html";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ParseHtml12().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();
    }
}
File nameRaw URLUpdated
ParseHtml.javaParseHtml.java2015-11-08 7:50 pm
ParseHtml5.javaParseHtml5.java2015-11-08 7:50 pm
ParseHeaders.javaParseHeaders.java2015-11-08 7:50 pm
ParseHtml12.javaParseHtml12.java2015-11-20 7:40 am
Resources: 
<p style="line-height: 16pt">Non eram nescius, Brute, cum, quae summis ingeniis exquisitaque doctrina philosophi Graeco sermone tractavissent, ea Latinis litteris mandaremus, fore ut hic noster labor in varias reprehensiones incurreret. nam quibusdam, et iis quidem non admodum indoctis, totum hoc displicet philosophari. quidam autem non tam id reprehendunt, si remissius agatur, sed tantum studium tamque multam operam ponendam in eo non arbitrantur. erunt etiam, et ii quidem eruditi Graecis litteris, contemnentes Latinas, qui se dicant in Graecis legendis operam malle consumere. postremo aliquos futuros suspicor, qui me ad alias litteras vocent, genus hoc scribendi, etsi sit elegans, personae tamen et dignitatis esse negent.</p>
<p style="line-height: 25pt">Contra quos omnis dicendum breviter existimo. Quamquam philosophiae quidem vituperatoribus satis responsum est eo libro, quo a nobis philosophia defensa et collaudata est, cum esset accusata et vituperata ab Hortensio. qui liber cum et tibi probatus videretur et iis, quos ego posse iudicare arbitrarer, plura suscepi veritus ne movere hominum studia viderer, retinere non posse. Qui autem, si maxime hoc placeat, moderatius tamen id volunt fieri, difficilem quandam temperantiam postulant in eo, quod semel admissum coerceri reprimique non potest, ut propemodum iustioribus utamur illis, qui omnino avocent a philosophia, quam his, qui rebus infinitis modum constituant in reque eo meliore, quo maior sit, mediocritatem desiderent.</p>
<p style="line-height: 13pt">Sive enim ad sapientiam perveniri potest, non paranda nobis solum ea, sed fruenda etiam [sapientia] est; sive hoc difficile est, tamen nec modus est ullus investigandi veri, nisi inveneris, et quaerendi defatigatio turpis est, cum id, quod quaeritur, sit pulcherrimum. etenim si delectamur, cum scribimus, quis est tam invidus, qui ab eo nos abducat? sin laboramus, quis est, qui alienae modum statuat industriae? nam ut Terentianus Chremes non inhumanus, qui novum vicinum non vult 'fodere aut arare aut aliquid ferre denique' -- non enim illum ab industria, sed ab inliberali labore deterret --, sic isti curiosi, quos offendit noster minime nobis iniucundus labor.</p>
<html>
<body>
    <h1>This is a H1 title: Chapter 1</h1>
    <h2>This is a H2 title: Section 1.1</h2>
	<p>This is some text</p>
    <h3>This is a H3 title: 1.1.1</h3>
    <h4>This is a H4 title: 1.1.1.1</h4>
	<p>This is usually as far as it goes</p>
    <h5>This is a H5 title: 1.1.1.1.1</h5>
    <h6>This is a H6 title: 1.1.1.1.1.1</h6>
	<p>Subsections of 6 deep don't improve the reading experience. Try to avoid them</p>
</body>
</html>
<html>
<head>
    <style>.col{padding:3px 20px 3px 20px}</style>
</head>
<body style="font-family:tahoma">
    <div style="background:rgb(230,230,230); padding:5px ;border:1px solid black;">
    <b style="color:rgb(51,153,255)">Sample header</b>
    <img style="float:right" height="25px" width= "80px" src="resources/images/itext.png" />
    </div>
    <br />
    <table border='0' style='border-collapse: collapse;'>
    <tr>
    <td class="col">String 1</td>
    <td class="col">: 1234354545</td> 
    </tr>
    <tr>
    <td class="col">String 2</td>
    <td class="col">: rere</td> 
    </tr>
    <tr>
    <td class="col">String 3</td>
    <td class="col">: ureuiu</td> 
    </tr>
    <tr>
    <td class="col">Date</td>
    <td class="col">: dfdfjkdjk</td> 
    </tr>
    </table>
    <br />
    <br />
    <br />
    <hr/>
    <br />
    Contact us
</body>
</html>
File nameRaw URLUpdated
lineheight.htmllineheight.html2015-11-08 7:52 pm
headers.htmlheaders.html2015-11-08 7:52 pm
test2015-11.htmltest2015-11.html2015-11-20 7:42 am
Results: 
File nameRaw URLUpdated
cmp_html_1.pdfcmp_html_1.pdf2015-11-08 7:51 pm
cmp_lineheight.pdfcmp_lineheight.pdf2015-11-08 7:51 pm
cmp_headers.pdfcmp_headers.pdf2015-11-08 7:51 pm
cmp_test2015-11.pdfcmp_test2015-11.pdf2015-11-20 7:43 am