I am writing into a document multiple tables using ITextSharp.
How to know if a table will go to a next page, so then I could remove a certain cell?
P.S. I am using table.KeepTogether = true;
.
You can use ColumnText
in simulation mode to find out if elements fit a specific rectangle. See also How to fit a String inside a rectangle?
For instance:
ColumnText ct = new ColumnText(writer.DirectContent);
ct.SetSimpleColumn(36, 36, 559, 806);
ct.AddElement(table);
int status = ct.Go(true);
I used coordinates (36, 36)
and (559, 806)
because I assume that you have an A4 document with a margin of half an inch. If ColumnText.HasMoreText(status)
is true
, then the table object didn't fit the page. If that value is false
, then the table fits the page and you can even use ct.GetYLine()
method to find out the Y coordinate where the table ends.
Based on this information, you can add an altered table or the existing table to a ColumnText
object, reset the simple column and use Go(false)
(or Go()
) to add the content for real.
See also: