2
votes

Working on a calendar projcet and using iText to generate a pdf to print appointments. I can plot a cell with a colspan, and a cell with a rowspan, but I can't combine it. Table has width of 4 cells. I want to achieve something like this:

(A)(B)(C)(C)

(D)(E)(C)(C)

so (1,1), (1,2) and (2,1) (2,2) are regular cells. But there should be a cell in (1,3) covering (1,3) (1,4) (2,3) and (2,4) thus having a colspan of 2 AND a rowspan of 2.

Current code:

PdfPTable table = new PdfPTable(4);

PdfPCell cell = new PdfPCell(new Phrase(" 1,1 "));

table.addCell(cell);

cell = new PdfPCell(new Phrase(" 1,2 "));

table.addCell(cell);

PdfPCell cell23 = new PdfPCell(new Phrase("multi 1,3 and 1,4"));

cell23.setColspan(2);

cell23.setRowspan(2);

table.addCell(cell23);

cell = new PdfPCell(new Phrase(" 2,1 "));

table.addCell(cell);

cell = new PdfPCell(new Phrase(" 2,2 "));

table.addCell(cell);

// 2,3 and 2,4 should be filled because 1,3 has rowspan 2 and colspan 2.

//table.completeRow(); //no effect

However that generates an error:

ExceptionConverter: java.io.IOException: The document has no pages.

If i don't start creating the second row, it just plots fine ( 1 row, and cell on (1,3) has a colspan of 2. Since there is no second row, the rowspan(2) has no effect. Any help is appreciated. Thanks

1

1 Answers

3
votes

At first sight, I'd say: you get a "document has no pages" exception because you're not adding any content to the document. I don't see:

document.add(table);

anywhere in your code snippet.

I have copy/pasted your code into a full example and I posted the full example here: ColspanRowspan. The resulting PDF looks like this:

enter image description here

This seems to be the desired behavior. I can only think of two differences: (1) you're forgetting to add the actual table (which was my initial answer), or (2) you are using a mighty old version of iText in which rowspan wasn't fully supported.