1

It's the topic connected with List items in table cell are not formatted. I'm using XmlWorker to process HTML output from formattable control (dijit/Editor from Dojo). There are some blocks (when you use centering or margin formatters) like that:

<div>
    <p align="center"><font size="5"><b>&nbsp;<font color="#32cd32">My centered Para</font></b></font><font color="#32cd32">&nbsp;</font></p>
</div>

However, when I add them to the Paragraph that is added to the Table like here:

    PdfPCell htmlCell = new PdfPCell();
    htmlCell.setBackgroundColor(new BaseColor(213, 226, 187));
    htmlCell.addElement(html2Para(html));
    htmlCell.setPaddingBottom(5);
    htmlCell.setPaddingLeft(5);
    table.addCell(htmlCell);

private Paragraph html2para(String html) {
    final Paragraph para = new Paragraph();
    try {
        XMLWorkerHelper.getInstance().parseXHtml(new ElementHandler() {
            @Override
            public void add(Writable wri) {
                if (wri instanceof WritableElement) {
                    List<Element> elems = ((WritableElement) wri).elements();
                    for (Element elem : elems) {
                        para.add(elem);
                    }
                }
            }
        }, new StringReader(StringUtils.trimToEmpty(html)));
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    }
    return para;
}

Everything that is inside the Element that is the instance of PdfDiv is not visible.

So, when I meet the PdfDiv, I add its content:

private void addElem2para(Element elem, Paragraph target) {
    if (elem instanceof PdfDiv) {
        for (Element inside : ((PdfDiv)elem).getContent()) {
            addElem2para(inside, target);
        }
    } else {
        target.add(elem);
    }
}   

However, now that formatting like centering or margin that were the properties of the PdfDiv are 'lost'.

How should one deal with the PdfDiv elements from XmlWorker?

I'm using iText and XmlWorker in version 5.5.2

Community
  • 1
  • 1
Danubian Sailor
  • 1
  • 38
  • 145
  • 223

1 Answers1

2

(I think) I understand your question, but I don't understand your code.

The ParseHtml example shows how I interpreted your question:

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

You have some HTML (I stored your HTML snippet in sb) that you want to parse into a series of iText objects to be added to a table cell.

This is the result of this operation:

Screen shot

That looks pretty much the way I expected.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • XMLWorkerHelper.parseToElementList what version of library is that? I don't see that method in my 5.5.2 version. I've added version to my question. – Danubian Sailor Feb 12 '15 at 08:54
  • Version 5.5.4. I added that convenience method because I was getting tired to copy/paste the same code sequence over and over in my answers on StackOverflow. – Bruno Lowagie Feb 12 '15 at 09:25
  • OK I've tested it. It much improves processing of HTML and placing it in stand-alone paragraph, but still, if I add the paragraph to the table, I must filter out the PdfDivs, because their content doesn't show up. – Danubian Sailor Feb 12 '15 at 10:09
  • Strange... I can't reproduce this. Maybe it's because I'm using a SNAPSHOT of version 5.5.5. – Bruno Lowagie Feb 12 '15 at 10:31
  • I've tested (with 5.5.4) - If I add PdfDiv to PdfCell directly, it's rendered. But if I add Paragraph with PdfDiv inside, it's not rendered. The problem is, that with the dirty HTML output from Dojo, there are many divs embedded within other elements.... – Danubian Sailor Feb 12 '15 at 10:50
  • @VikasVats Which version of iText are you using? As shown in my example, I can't reproduce the issue. How did you get that issue? – Bruno Lowagie Jun 07 '16 at 07:47
  • I am using itext 5.5.6 with xmlworker 5.5.6. I used addElem2para() method which works for me. But your solution is not working. I get empty elementList from XMLWorkerHelper.parseToElementList(sb.toString(), null); – Vikas Sharma Jun 07 '16 at 10:17
  • @VikasVats Please don't expect someone to be able to solve your problem if you don't provide more info. You are hijacking an answer to a different question. If you have a problem, create a new question. – Bruno Lowagie Jun 07 '16 at 10:24