Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing iTextSharp from putting a page break in the middle of a PdfPTable?

I'm using iTextSharp v5.3 to build a PDF with one or more tables that are have the same cells but can vary in height due to dynamic content. Currently, if a PdfPTable is too large to fit on the current page the table is broken and the remaining cell data ends up on the next page. Is there a way I can tell iTextSharp that if the table won't fit, to put the whole thing on the next page?

Bad Table Break

I've tried messing around with SplitRows and SplitLate but they don't have the desired effect. I have also tried to do some calculations after adding the PdfPTable to the document but can't figure out how to get the "cursor position" on the page. Do I need to track the current page's height in my loop and do all of the calculations myself?

like image 549
AJ. Avatar asked Dec 06 '22 10:12

AJ.


2 Answers

You can also use:

tableName.KeepTogether = true;
like image 100
Rob Avatar answered Dec 08 '22 22:12

Rob


You can ask the table for its total height. This is done like this: table.TotalHeight

If the height is 0, make sure you've defined the width first: iText can't calculate the width if it doesn't know the available width to render the table.

Once you have the height, ask the writer for the vertical position: writer.GetVerticalPosition(true)

As you probably know, the Y coordinate is 0 at the bottom of the page. If the vertical position minus the total height of the page exceeds the margin size, you know that the table won't fit, and you can trigger a new page.

like image 28
Bruno Lowagie Avatar answered Dec 08 '22 22:12

Bruno Lowagie