Adding new sections to a document, when the previous section ends with a Section break

We have a project that uses the MailMerge method to automatically generate documents, based on templates and information we get from our database.
Recently we added some code from here, which we use to divide the generated documents in pages, and then check if there are empty pages so that we can remove those.

But we are now facing an issue where when the template has a page that ends with a sectionbreak, a new page will be created after it that starts with a section break, which results in an empty page.

Example template: image.png (5.8 KB)
Results in: image.png (7.4 KB)

So my question, is there a way to add a new section to a document, after a page that ends in a section without the empty page in between.

@LarsKolt The code from GitHub to split document into pages is obsolete. Not you can use a built-in Document.ExtractPages method to achieve the same.

You can specify section start in section’s page setup to SectionStart.Continuous. In this case there will not be an explicit page break at the beginning of the section.

You should note that MS Word documents are flow document and they do not actually have a concept of a page. Document is layout into pages on the fly by the consumer application. So splitting the document into pages and then reassembling the pages back might have side effects, since in such case each page will be represented as a separate section in the document.