Why are the section breaks continuous in the Word document concatenated

String path = "C:\\Users\\z_jia\\Desktop\\a\\";
Document doc = new Document(path + "a1.docx");
Document main = new Document(path + "封面.docx");
main.appendDocument(doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
main.save(path + "o.docx");

a1.docx (68.2 KB)

封面.docx (14.3 KB)

o.docx (53.7 KB)

I need keep ImportFormatMode.KEEP_SOURCE_FORMATTING, how could I change the section breaks?

@GusGus

To change the section breaks in your concatenated Word document while using ImportFormatMode.KEEP_SOURCE_FORMATTING, you can manually adjust the section settings of the source document before appending it to the destination document.

Here’s how you can do it:

  1. Set the Section Start Type: Before appending the source document, you can set the SectionStart property of the first section in the source document to either SectionStart.Continuous or SectionStart.NewPage, depending on your requirement.

  2. Restart Page Numbering: If you want the page numbering to restart in the appended document, you can set the RestartPageNumbering property to true and specify the starting page number.

Here’s a code example demonstrating how to achieve this:

using Aspose.Words;

string path = "C:\\Users\\z_jia\\Desktop\\a\\";
Document doc = new Document(path + "a1.docx");
Document main = new Document(path + "封面.docx");

// Set the section start type to Continuous
doc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;

// Optionally restart page numbering
doc.FirstSection.PageSetup.RestartPageNumbering = true;
doc.FirstSection.PageSetup.PageStartingNumber = 1;

// Append the document while keeping the source formatting
main.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting);
main.Save(path + "o.docx");

In this example, the first section of the source document (a1.docx) is set to start continuously, which will prevent any unwanted page breaks when it is appended to the destination document (封面.docx). Adjust the SectionStart property as needed to control how the sections are joined.

This approach allows you to maintain the formatting of the source document while controlling the section breaks effectively.

// Set the section start type to Continuous
doc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;

Would this change the doc format?
I would like keep the format of doc same as before

@GusGus In your document section start in the second document is set to Continuous. If you would like to start the appended document on the next page you should reset section start to NewPage. This will not change to the original document format:

String path = "C:\\Temp\\";
Document doc = new Document(path + "a1.docx");
doc.getFirstSection().getPageSetup().setSectionStart(SectionStart.NEW_PAGE);
Document main = new Document(path + "封面.docx");
main.appendDocument(doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
main.save(path + "o.docx");