iText 嵌套 table - 第一行未呈现

iText nested table - first row not rendered

我正在根据列表的大小生成一个 table。 table 被设置为适合每个表格,有 5 列和 13 行。

当列表大小小于 5 时,不显示任何内容。 如果列表大小为 5 或更大,则会正确显示。

Document doc = new Document(PageSize.A4, pageMargin, pageMargin, pageMargin, pageMargin);   
//5 rows for the table
PdfPTable table = new PdfPTable(5);

for (int i = 0; i < list.size(); i++) {

Object obj = list.get(i);
//this is the superior cell
PdfPCell cell = new PdfPCell();
cell.setFixedHeight(60.4f);

// Nested Table, table in the cell
PdfPTable nestedTable = new PdfPTable(2);
nestedTable.setWidthPercentage(100);
nestedTable.setWidths(new int[] { 24, 76 });

// First Cell in nested table
PdfPCell firstCell = new PdfPCell();
// fill cell...

// second cell in nested table
PdfPCell secondCell = new PdfPCell();
// fill cell

// put both cells into the nestedTable
nestedTable.addCell(firstCell);
nestedTable.addCell(secondCell);

// put nestedTable into superior table
cell.addElement(nestedTable);
table.addCell(cell);
}

doc.add(table);
doc.close();

您创建了一个包含 5 列的 PdfPTable。当该行完成时,iText 只会将 table 行写入输出文档,即当它包含 5 个单元格时。如果您添加的单元格少于 5 个,则永远不会刷新该行。

你说: 如果列表大小为 5 或更大,则会正确显示。

这是不正确的。除非单元格数量是5的倍数,否则不会显示最后一行。

所以你必须确保最后一行有 5 个单元格。在将 table 添加到文档之前,您可以使用这种便捷方法轻松地做到这一点:table.completeRow()