How can I determine if a MigraDoc table would be split among two pages?
Asked Answered
M

1

6

I'm using MigraDoc to generate some PDFs. I have code to create a table of text for each element in an array and am printing out pages with these tables.

However, the requirements I am being given is that if I have 2 tables, and the 2nd table would not fit on the page due to the length of the first table, I need the 2nd table to start the next page. I then need to repeat this for each table I am adding to my document.

How would I go about doing this?

Marsiella answered 15/10, 2012 at 20:4 Comment(0)
T
8

If all tables are small enough to fit a single page, then there is a simple solution: set the KeepWith property of the first row to row count minus one to keep the whole table on one page.

If tables do not always fit a single page: you could try a hack, e.g. setting KeepWith to 6 or 8 or 10 (depends on the height of your table rows). If the value is close to what fits on a single page (without going over), tables will start on a new page automatically.
Obviously this will work very good if table rows have a constant height; if tables rows have varying heights, this will not work reliably, but will still prevent tables that will only have one or two rows on the first page (this is not your requirement, but maybe the requirement can be discussed?).

The clean (but complicated) way to fulfill your requirements: get access to the internal GetRenderInfoFromPage method. You will have to start an incremental process:
1) render the document;
2) if you find a split table, insert a page break before that table and repeat from 1.
See also here:
http://forum.pdfsharp.net/viewtopic.php?p=1960#p1960

Transcontinental answered 16/10, 2012 at 8:8 Comment(1)
Each table should be at most 3/4ths of a page, so the KeepWith property should be sufficient. I'll mark this as the answer once I can finally test it out thanks!Marsiella

© 2022 - 2024 — McMap. All rights reserved.