Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

itext

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.

like image 796
David Bulté Avatar asked Oct 19 '25 19:10

David Bulté


1 Answers

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.

like image 189
Chris Haas Avatar answered Oct 22 '25 10:10

Chris Haas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!