简体   繁体   English

iText嵌套表-未呈现第一行

[英]iText nested table - first row not rendered

i am generating a table, based on the size of a list. 我正在根据列表的大小生成一个表。 The table is set up to fit on an avery form, there are 5 columns and 13 rows. 表格设置为适合平均表格,共有5列和13行。

When the list size is smaller than 5, nothing is shown. 当列表大小小于5时,不显示任何内容。 If the list size is 5 or greater, it is shown properly. 如果列表大小为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();

You create a PdfPTable with 5 columns. 您创建具有5列的PdfPTable iText will only write a table row to the output document when that row is complete, ie when it contains 5 cells. 当该行完成时,即当iText包含5个单元格时,iText只会将该表行写入输出文档。 If you add less than 5 cells, the row is never flushed. 如果添加的单元格少于5个,则该行永远不会被刷新。

You say: If the list size is 5 or greater, it is shown properly. 您说: 如果列表大小为5或更大,则显示正确。

This is not correct. 这是不正确的。 Unless the amount of cells is a multiple of 5, the last row will not be shown. 除非单元格的数量是5的倍数,否则将不会显示最后一行。

So you have to make sure there are 5 cells in the final row. 因此,您必须确保最后一行中有5个单元格。 You can easily do this by using this convenience method, right before adding the table to the document: table.completeRow() 您可以在将表添加到文档之前,使用此便捷方法轻松地做到这一点: table.completeRow()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM