How to make a particular sub-string Bold when converting 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.

24th October 2015
admin-marketing

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?

Posted on StackOverflow on Jun 4, 2014 by Anuranjan

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

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("

The String I want to render to PDF

"));

There are more complex examples available in case you need support for images, special fonts, and so on.


Share this article

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