Search code examples
c#vb.netitext

iTextSharp: How to place a summary PdfPTable at the end of a PDF multi page document positioning it in the bottom side of the last page


In my VB.NET project with iTextSharp 5.4.2.0 I'm trying to place a summary PdfPTable at the end of a multi page PDF document.

The document has a header, containing some information, and a central body in which is placed a table with many rows that goes through the document pages up to the last one.

Now, I have to check if the last page has sufficient space, between the "many rows" table bottom side and the end of the document, to place the summary table, otherwise the summary table has to be placed in the following page, aligned to the bottom.

Might you help me on finding a way to do it?

I set the summary table property KeepRowsTogether so if the space in the page is not sufficient the whole table is moved to the next page but I didn't find a way to align the table to the bottom.

I red the post how can I add a PdfPTable as footer in last page? but it does not fit my need.


Solution

  • This is what I decided to do:

    1. I set the height of each row of the summary table (PdfPCell.FixedHeight), so I can calculate the table height.
    2. Then I compare the table height with the ramaining free space in the page (Document.PdfWrite.GetVerticalPosition), after the "rows table" is added to the document. If the free space in the page is less than the table height I add a new page.
    3. In the new page I add an empty paragraph (this allows me to use the table property SpacingBefore later).
    4. I check the free space in the new page (Document.PdfWrite.GetVerticalPosition) and calculate the value of SpacingBefore to place the table at the end of the page.

    Maybe there is a simplest way to do that but at the moment I figure out this one :-)

    Hope this might be useful to someone else.