4
votes

I am using iText in Java to convert a HTML to PDF.

I want a particular paragraph which has some words as Bold and some as Bold+Underlined to be passed as a string to the Java code and to be converted to PDF using the iText library.

I am unable to find a suitable method for this. How should I do this?

1
First feed your HTML into this demo: demo.itextsupport.com/xmlworker Then hit transform. Do you get the desired result? If so, I'll show you the code.Bruno Lowagie
Yes, it worked. Can you please share the code?Anuranjan
See my answer. You may want to try all the examples before writing your own code.Bruno Lowagie
I am following your book Bruno. It's great to hear from you.Anuranjan
If you refer to "iText in Action": the HTML to PDF part needs to be replaced. If you refer to "The ABC of PDF", that's a work in progress. Once that book is finished, I'll use the XML Worker examples as the basis for a book about templates for PDF.Bruno Lowagie

1 Answers

3
votes

If you want to convert XHTML to PDF, you need iText + XML Worker.

You can find a number of examples here: http://itextpdf.com/sandbox/xmlworker

The most simple examples looks like this:

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();
}

Note that the HTML file is passed as a FileInputStream in this case. You want to pass a String. This means you'll have to do something like this:

XMLWorkerHelper.getInstance().parseXHtml(writer, document,
        new StringReader("<p>The <b>String</b> I want to render to PDF</p>"));

There are more complex examples in the Sandbox in case you need support for images, special fonts, and so on. For instance this example will convert XHTML to a series of iText objects instead of rendering them to a page rightaway.