Removing extra pages in a merged document

We are currently using a template and merge fields to populate a document from a dataset, we have page breaks that we create in the data, but sometimes we get a double break depending on when Word performs a page break. Is there any way to prevent this double breaking from happening, or removing the extra blank page?

Hello
Thanks for your request. Word document is flow document and does not contain any information about its layout into lines and pages. Therefore, technically there is no “Page” concept in Word document.
I think, in your case, you can try deleting all empty paragraphs from the end of each document sections, please see the following code example:

Document doc = new Document("C:\\Temp\\in.doc");
foreach(Section sect in doc.Sections)
{
    // Remove empty paragraphs from the end of the document.
    while (sect.Body.LastParagraph != null && string.IsNullOrEmpty(sect.Body.LastParagraph.GetText().Trim()))
        sect.Body.LastParagraph.Remove();
}
doc.Save("C:\\Temp\\out.doc");

Hope this helps.
Best regards,

haven’t really worked with sections, do I need to somehow set sections in my template? Thanks for the speedy reply by the way.

Hi
Thanks for your request. Any document contains sections. Please see Aspose.Words DOM for more information:
https://docs.aspose.com/words/net/aspose-words-document-object-model/
If the code does not work for you, please attach your sample document here for testing. We will check it and try to help you.
Best regards,