How to add a rich Textbox (HTML) to a table cell?

I have a rich text box with the following content: Overview line1 Overview line2 Overview line3 Overview line4 Overview line4 Overview line5 When I add the HTML

I have a rich text box with the following content:

Overview line1

Overview line2

Overview line3

Overview line4

Overview line4

Overview line5

When I add the HTML content of this rich text box to a PdfPCell, I expect to see:

Overview line1
Overview line2
Overview line3
Overview line4
Overview line5
The problem is that the text appears as HTML instead of being rendered.

Posted on StackOverflow on Nov 19, 2014 by Kate

Please take a look at the HtmlContentForCell example.

In this example, we have the HTML you mention:

public static final String HTML = "

Overview line1

" + "

Overview line2

Overview line3

" + "

Overview line4

Overview line4

" + "

Overview line5 

";

We also create a font for the </p><p> tag:

public static final String CSS = "p { font-family: Cardo; }";

In your case, you may want to replace Cardo with Arial.

Note that we registered the regular version of the Cardo font:

FontFactory.register("resources/fonts/Cardo-Regular.ttf");

If you need bold, italic and bold-italic, you also need to register those fonts of the same Cardo family. (In case of arial, you'd register arial.ttf, arialbd.ttf, ariali.ttf and arialbi.ttf).

Now we can parse this HTML and CSS into a list of Element objects with the parseToElementList() method. We can use these objects inside a cell:

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

See html_in_cell.pdf for the resulting PDF.



Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial
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