NoNewLineParagraph cannot be cast to Element

送分小仙女□ 提交于 2019-12-11 05:04:14

问题


I'm following the itextpdf example http://itextpdf.com/sandbox/htmlworker/HtmlContentForCell.

I have the following code:

// Relevant code from main part of the class:

   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   Document document = new Document(PageSize.A4, 40, 40, 40, 40);
   PdfWriter writer = PdfWriter.getInstance(document, baos);
   document.open();
   document.add(buildContent());
   document.close();

// method that should provide content to the document.

public PdfPTable buildContent() throws IOException {
    InfoList infoList = infoListInstance.get();
    PdfPTable table = new PdfPTable(2);
    for (InfoListMessage message
            : infolistList.getMessages()) {
        renderMessageMetadata(message, table);
        renderMessageContent(message, table);
    }
    return table;
}

// method where the problem occurs and exception is thrown in the for-loop line

public void renderMessageContent(
        InfoListMessage message,
        PdfPTable table) throws IOException {

PdfPCell cell = new PdfPCell();

for (Element e : XMLWorkerHelper.parseToElementList(message.getContent(), null)) {
    cell.addElement(e);
}  
    table.addCell(cell);
}

The line with the for-loop "for (Element e ..." causes the following exception:

java.lang.ClassCastException: com.itextpdf.tool.xml.html.pdfelement.NoNewLineParagraph cannot be cast to com.itextpdf.text.Element

Why? I can't find any info on this exception by googling.

In this case, the html-snippet - returned by message.getContent() - I was trying to use, looks originally like this:

<html>
 <head></head>
 <body>
 justrandomtexthere
 </body>
</html>

回答1:


Problem solved.

It was caused by my itextpdf and xmlworker being slightly different versions.

This and MANY other problems were solved by getting the exact same versions (5.5.5 in my case) of both dependencies.

After 2 days of banging my head to the wall rigorously I cannot stress this enough: To avoid tons of problems with itext and xmlworker, make sure they are always the exact same version in your project.

Hopefully this is helpful to others.



来源:https://stackoverflow.com/questions/28871767/nonewlineparagraph-cannot-be-cast-to-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!