【发布时间】:2015-06-06 01:53:11
【问题描述】:
我正在关注itextpdf 示例http://itextpdf.com/sandbox/htmlworker/HtmlContentForCell。
我遇到了一个问题,每当我解析为元素并填充pdfCell 的 html 内容中有 bulletlist 时,除了最后一项丢失外,其他所有内容都显示正常.是什么原因造成的?
我有以下代码:
// 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);
}
我很确定它与 html 标签有关,但是当谈到 html 标签的复杂性时,我通常会迷失方向。这是我的代码的示例 HTML。这就是我作为 HTML 提供给 XMLWorkerHelper.parseToElementList 的内容:
<html>
<head></head>
<body>
<span>Lisätty liitteet</span>
<ul>
<li>document2.txt.txt (23 B)</li>
<li>document1.txt.txt (12 B)</li>
<li>document3.txt.txt (27 B)</li>
</ul>
</body>
</html>
这是应用程序创建的 pdf 中的一个单元格的屏幕截图:
【问题讨论】:
-
有很多行?
for-each循环不保留排序...您是否进行了计数以检查元素是否丢失或放错位置??? -
@JordiCastilla 是的,这里的顺序甚至都不重要。该元素未显示在 pdf 中。即使它在循环中的那个列表中。
标签: java html parsing pdf itext