I’m trying to write a extension method for Aspose’s DocumentBuilder
class that allows you to check if inserting a number of paragraphs into a document will cause a page break or not, I hoped this would be rather simple, but it turns out otherwise:
public static bool WillPageBreakAfter(this DocumentBuilder builder, int numParagraphs)
{
// Get current number of pages
int pageCountBefore = builder.Document.PageCount;
for (int i = 0; i < numParagraphs; i++)
{
builder.InsertParagraph();
}
// Get the number of pages after adding those paragraphs
int pageCountAfter = builder.Document.PageCount;
// Delete the paragraphs, we don't need them anymore
...
if (pageCountBefore != pageCountAfter)
{
return true;
}
else
{
return false;
}
}
MY problem is, that inserting paragraphs does not seem to update the builder.Document.PageCount
property. Even plugging in something crazy like 5000 paragraphs does seem to modify that property. I’ve also tried InsertBreak()
(including using BreakType.PageBreak
) and Writeln()
but those don’t work either.
What’s going on here? Is there anyway I can achieve the desired result?
It seems that absolutely nothing done on the DocumentBuilder
parameter actually happens on the DocumentBuilder
that is calling the method. In other words:
If I modify the for loop to do something like builder.InsertParagraph(i.ToString());
and then remove the code that deletes the paragraphs afterwords. I can call:
myBuilder.WillPageBreakAfter(10);
And expect to see 0-9 written to the document when it is saved, however it is not. None of the Writeln()
s in the extension methods seem to do anything at all.
UPDATE
It appears for what ever reasons, I cannot write anything with the DocumentBuilder
after accessing the page count. So calling something like Writeln()
before the int pageCountBefore = builder.Document.PageCount;
line works, but trying to write after that line simply does nothing.