Can you tell if a table will fit on one page?

Is there a way to tell if the table fits on one page?

If it will not fit on one page I would like to set KeepWithNext to false so that the table does not push to the next page at all.


You could check the number of pages before and after adding the Table element to your document:

int pagesCount = document.GetPaginator().Pages.Count;

But I wouldn’t recommend this, it is an expensive task.
Also, I’m not sure what exactly you mean by this:

so that the table does not push to the next page at all

Perhaps you can send us a sample document that you have and specify what result you would like to have?

When you create a table and use KeepWithNext in the paragraph style, if there is not enough room on the current page for the table it will be pushed to the next page in an attempt to keep the table together.

If the table is too big for a page, it will still be pushed to the next page and then span over 2 pages anyway.

If the table is not going to fit on one page, I would like to set KeepWithNext to false and just start the table from the starting point without attempting to keep the table together.


I see now, that is a bit tricky and it will involve using the GetPaginator method.
Can you send us a sample of your document that I could use for testing purposes?


Try something like this:

var document = DocumentModel.Load("input.docx");
var table = (Table)document.GetChildElements(true, ElementType.Table).First();
var section = table.Parent as Section;

var tempDocument = new DocumentModel();
tempDocument.DefaultCharacterFormat = document.DefaultCharacterFormat;
tempDocument.DefaultParagraphFormat = document.DefaultParagraphFormat;

var tempSection = tempDocument.Import(section, false);
tempSection.Blocks.Add(tempDocument.Import(table, true));

bool tableOccupiesMultiplePages = tempDocument.GetPaginator().Pages.Count > 1;

Note, this assumes that the header and footer that is on the page where your Table element is, won’t impact the available area on that page.

However, note that the size of the header and footer can be larger than what you have by default.
Also, there are different types of headers and footers (first, even, and odd) so based on which exact page your Table element is, the different header and footer would apply.

In that case, you would need to use the GetPaginator method on the original DocumentModel object. But note that this is an expensive task, it is like saving the whole document to PDF.

Nevertheless, let me know if the above-provided solution doesn’t work for you.