简体   繁体   中英

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.

When the list size is smaller than 5, nothing is shown. If the list size is 5 or greater, it is shown properly.

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. iText will only write a table row to the output document when that row is complete, ie when it contains 5 cells. If you add less than 5 cells, the row is never flushed.

You say: If the list size is 5 or greater, it is shown properly.

This is not correct. Unless the amount of cells is a multiple of 5, the last row will not be shown.

So you have to make sure there are 5 cells in the final row. You can easily do this by using this convenience method, right before adding the table to the document: table.completeRow()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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