Aspose word generation Section is creating Section Break (Continuous) and resetting the pagenumber

When I try to generate Word document by merging 3 input files Aspose is adding Section Break (Continuous) in the Word Generation. The section break is not visible in the individual documents, but gets added in the generation. This is also resetting the page number.

I have merged the documents using Word Merger

I have included input files(Input_File1, Input_File2 and Input_File3) and output file in the zip.
Below is the output after generation

HGHHY PageNumIssue 2023-11-01T141830.zip (95.3 KB)

@manikya.rao In the second document section start is set to continues:

<w:sectPr w:rsidR="00A04F82" w:rsidRPr="00A04F82" w:rsidSect="00472CFE">
	<w:headerReference w:type="even" r:id="rId11"/>
	<w:headerReference w:type="default" r:id="rId12"/>
	<w:footerReference w:type="even" r:id="rId13"/>
	<w:footerReference w:type="default" r:id="rId14"/>
	<w:headerReference w:type="first" r:id="rId15"/>
	<w:footerReference w:type="first" r:id="rId16"/>
	<w:type w:val="continuous"/>
	<w:pgSz w:w="11910" w:h="16840"/>
	<w:pgMar w:top="1871" w:right="995" w:bottom="1440" w:left="1304" w:header="1191" w:footer="720" w:gutter="0"/>
	<w:pgNumType w:start="1"/>
	<w:cols w:space="720"/>
	<w:docGrid w:linePitch="299"/>
</w:sectPr>

See <w:type w:val="continuous"/>.

If you need to start each document from new page, you can use the following code:

string[] documents = new string[] { @"C:\Temp\Input_File1.docx", @"C:\Temp\Input_File2.docx", @"C:\Temp\Input_File3.docx" };

Document result = null;
foreach (string docPath in documents)
{
    Document doc = new Document(docPath);
    // Make sure the appended document starts from new page.
    doc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
    if (result == null)
        result = doc;
    else
        result.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting);
}
result.Save(@"C:\Temp\out.docx");

@alexey.noskov I am ok with page formatting but if you look at the page numbers of output file, they are set to P1. I want the page numbers to be continuous (P1,P2 etc).

@manikya.rao You should set PageSetup.RestartPageNumbering to false. Please modify the code like this:

string[] documents = new string[] { @"C:\Temp\Input_File1.docx", @"C:\Temp\Input_File2.docx", @"C:\Temp\Input_File3.docx" };

Document result = null;
foreach (string docPath in documents)
{
    Document doc = new Document(docPath);
    // Make sure the appended document starts from new page.
    doc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
    // Do not restart page numbering.
    doc.FirstSection.PageSetup.RestartPageNumbering = false;
    if (result == null)
        result = doc;
    else
        result.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting);
}
result.Save(@"C:\Temp\out.docx");

@alexey.noskov What is the reason Section Break (Continuous) is added to the Input_File2 content in the generation. Is it having something to do with page number reset. Could you please provide a way to remove section breaks.

@manikya.rao Each document in MS Word document contains at leas one section. Section defines page setup, headers/footers and content body. When you append one document to another, whole section is appended to the end of the document to preserve the source document page setup and other section’s setting.
Please see our documentation to learn more about Aspose.Words Document Object Model:
https://docs.aspose.com/words/net/aspose-words-document-object-model/

I have explained why section break continuous is added when Input_File2 is appended in this answer:
https://forum.aspose.com/t/aspose-word-generation-section-is-creating-section-break-continuous-and-resetting-the-pagenumber/273702/2

If you need to remove section breaks you should put content of the whole document into a single section. You can achieve this using code like this:

Document doc = new Document(@"C:\Temp\in.docx");
// join sections in the document
while (doc.Sections.Count > 1)
{
    doc.FirstSection.AppendContent(doc.Sections[1]);
    doc.Sections[1].Remove();
}
doc.Save(@"C:\Temp\out.docx");