Free Support Forum -

Insert PageBreak depending on available whitespace

We use Aspose.Words to generate a report that contains lots of input from the end user. The document consists of multiple titles and underneath those titles multiple paragraphs.

The issue we have now is that sometimes a title is printed on page 1 (because there is size to print it), but the following paragraph is printed on the second page.

We would like to improve this behavior by checking before we print the title using the DocumentBuilder how much space we have left on the current page. Once we know that we can compare it to a minimum expected space and if needed add a pagebreak.

We tried to achieve this by using LayoutCollector/LayoutEnumerator like this:


last paragraph of title1


LayoutCollector layoutCollector = new LayoutCollector(Document);
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(Document);

layoutEnumerator.Current = layoutCollector.GetEntity(Builder.Document.FirstSection.Body.LastParagraph);
var x = layoutEnumerator.Rectangle.Bottom;

var y = Builder.PageSetup.PageHeight;
var dif = y - x;
if (dif < 100)

Builder.Writeln(“TITLE 2”);

first paragraph of title2


But this does not work and has as consequence that only the first page is returned when we save the document. I suppose this might be because the LayoutEnumerator already builds the layout and blocks further updates.

Any ideas on how we can resolve this issue and make this work?

Hi Jeroen,

Can you please share your complete code and input document (if any) to reproduce the issue?

Best Regards,