How to nest tables without extending the inner table?

Category: 
Tags: nested tablestablesiText 5

I am using iTextSharp in a .NET project to generate a PDF file. I have a PdfPTable with two cells in one row. I put another PdfPTable in each cell: the first one has two rows; the second one has three rows. The last row of the smaller table stretches to fill the space. How can I avoid that the smaller table stretches to fit the cell of the outer table? Additionally: how do I align the inner table to the bottom?

Posted on StackOverflow on Feb 11, 2015 by lng

Please take a look at the following screen shot:

Screen shot
Screen shot

This screen shot was taken from the result of the NestedTables example. You are describing what happens when you add the table straight to another table (first table in the screen shot):

outerTable.addCell(innerTable);

Or maybe you are describing what happens when you add the table as a parameter for the constructor of a PdfPCell (second table in the screen shot):

PdfPCell cell = new PdfPCell(innerTable);
outerTable.addCell(cell);

Note that the difference between both is subtle: when you add the inner table straight to the outer table, the padding of the default cell is taken into account. This padding is 2 by default. When you pass the inner table as a parameter to create a PdfPCell, the padding of the cell is 0 by default.

If I understand your question correctly, you want the behavior shown in the third table in the screen shot. That table shows what happens when you add the table to a PdfPCell first:

PdfPCell cell = new PdfPCell();
cell.addElement(innerTable);
outerTable.addCell(cell);

You can align the inner table vertically like this:

cell.VerticalAlignment = Element.ALIGN_BOTTOM;