2015-11-05 50 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