-->

NoNewLineParagraph不能转换为元素(NoNewLineParagraph canno

2019-10-22 04:33发布

我下面的例子itextpdf http://itextpdf.com/sandbox/htmlworker/HtmlContentForCell 。

我有以下代码:

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

与for循环线“为(元素E ...”导致以下异常:

java.lang.ClassCastException:com.itextpdf.tool.xml.html.pdfelement.NoNewLineParagraph不能转换为com.itextpdf.text.Element

为什么? 我不能使用Google找到关于此异常的任何信息。

在这种情况下,HTML的片段 - 由message.getContent()返回 - 我试图用,看起来原来是这样的:

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

Answer 1:

问题解决了。

它是由我itextpdf和xmlworker稍微不同的版本引起的。

这和许多其他问题被越来越依赖双方的完全相同的版本(5.5.5在我的情况)来解决。

后撞我的头在墙上严格,我不能强调这一点2天:为避免吨问题的iText和xmlworker, 确保他们总是在你的项目中的版本完全相同。

但愿这是有帮助的人。



文章来源: NoNewLineParagraph cannot be cast to Element