David Bulté
David Bulté

Reputation: 3109

In iText, how to set the rowspan of a right-columned table cell?

I need to draw a table like this.

---------------
|  A   |   C  |
|------|      |
|  B   |      |
---------------      

The following code does not work. It creates a table with a single row, without drawing the 'C' cell:

PdfPTable table = new PdfPTable(2);
table.addCell("A");
table.addCell("B");
PdfPCell cell = new PdfPCell(new Phrase("C"));
cell.setRowspan(2);
table.addCell(cell);

Drawing the opposite table (with the rowspanning cell on the left) works just fine.

I have noticed a similar question here, but the context is different (I am not working on an international app) so I think I can rephrase the question again.

Upvotes: 4

Views: 10173

Answers (2)

Chris Haas
Chris Haas

Reputation: 55417

Tables are always drawn left to right, top to bottom, so you need to add A, then C and then finally B.

PdfPTable table = new PdfPTable(2);
table.addCell("A");
PdfPCell cell = new PdfPCell(new Phrase("C"));
cell.setRowspan(2);
table.addCell(cell);
table.addCell("B");

iText requires that all cells in a table are accounted for. If any cells are missing it skips that entire row. Your original code added A to R1C1, then B to R1C2, then created a new row and added a single cell to it, which, since it was a widow got trimmed off.

Upvotes: 5

Alexis Pigeon
Alexis Pigeon

Reputation: 7512

You should use nested tables:

PdfPTable inner = new PdfPTable(1);
inner.addCell("A");
inner.addCell("B");

PdfPTable outer = new PdfPTAble(2);
outer.addCell(inner);
outer.addCell("C");

Upvotes: 3

Related Questions