I am trying to use nested tables with iText. I get a DocumentException:
The table width must be greater than zero.
The outer table has 16 columns. I call a method that passes in the outer table. This method adds two inner tables. If I add either one of the inner tables it works fine. If I add the second inner table I get the exception which is thrown form the PdfPTable.writeSelectedRows method.
private final float[] columnWidths = {183, 31, 88, 49, 35, 25, 35, 35, 35, 32, 32, 33, 35, 60, 46, 26 };
public void buildPdfReport(ByteArrayOutputStream out) {
Document document;
PdfWriter pdfWriter;
document= new Document(PageSize.LETTER.rotate(), 2, 2, 5, 2);
pdfWriter = PdfWriter.getInstance(document, out);
document.open();
PdfPTable outerTable = new PdfPTable(columnWidths);
table.setTotalWidth(770F);
table.setLockedWidth(true);
buildNestedTables(table);
document.add(outerTable);
document.newPage();
document.close();
pdfWriter.close();
}
private void buildNestedTables(PdfPTable outerTable) {
PdfPTable innerTable1 = new PdfPTable(1);
PdfPTable innerTable2 = new PdfPtable(2);
PdfPCell cell;
innerTable1.addCell("Cell 1");
innerTable1.addCell("Cell 2");
outerTable.addCell(innerTable1);
innerTable2.addCell("Cell 3");
innerTable2.addCell("Cell 4");
outerTable.addCell(innerTable2);
cell = new PdfPCell("");
cell.setColspan(14);
outerTable.addCell(cell);
}
I'm not sure what I am missing.
Thank you for any help,
Greg
The problem you're reporting can't be reproduced. I have copy/pasted your code into a standalone example NestedTables and the resulting PDF looks OK: nested_tables.pdf
I see two possible causes for your problem:
These are things that are impossible in your snippet:
PdfPTable outerTable = new PdfPTable(columnWidths);
table.setTotalWidth(770F);
You define a variable named outerTable
and in the next line you set the width for a variable named table
(that isn't defined anywhere in your code).
You use:
cell = new PdfPCell("");
But there is no PdfPCell
constructor that accepts a String
value.
Please download the standalone example and check if it works for you. It works for me; if it doesn't work for you, you should upgrade your iText version.