2015-11-05 52 views
1

我正在根据列表的大小生成表。 该表设置为适合avery表格,有列和13行。iText嵌套表 - 第一行未呈现

当列表大小小于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(); 

回答

1

您创建5列的PdfPTable。 iText只会在该行完成时(即包含5个单元格时)向输出文档写入表格行。如果添加少于5个单元格,则该行从不刷新。

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

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

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

+0

对不起,我必须错过未完成的行。 table.completeRow()是解决方案,再次感谢! – nextcard