Inserting a SectionBreakNewPage continues the list onto the next page

We have an application that combines multiple documents into a single document. I have a situation where First.docx is a numbered list, with no hard returns or content after the last character of the last list item. I then take Second.docx, insert a BreakType.SectionBreakNewPage because I want the next document to be on a new page, and then import the content of the second document.


The result is at the top of the 2nd page, there is a numbered List item with no content, followed by the content of the new document. The list item at the top of this page contains no other content, it is just a Number with nothing following on the line.

If I turn on “show hidden characters” I can see the “Section Break (Next Page)” in the document at the end of the first page.

Unfortunately - this appears to be the correct behavior from MS Words perspective. If I open up a blank MS Word document, make a numbered list, and then hit Ctrl+Enter to insert a page break at the end of the list, I see the exact same behavior. A numbered item on the next page with no content. If I were working in Word, I would avoid this by entering a hard-return at the end of the list (which starts the next numbered line), but then hit backspace once to delete the numbered item. Hitting Ctrl+Enter at this point would give me the correct behavior.

My question is this: Is there something I can do with Aspose to avoid this problem? I want the next page be a clean break from the previous. No continuation of lists, just the content of the next document. I have a sample project that reproduces the problem in code. The way we are combining documents is a little round-a-bout, because we have occassions where we actually do continue lists across documents, and so I have reproduced our basic approached in the attched code (C#). Copy/pasted below is the meat of the actual code.

public class Program
{
private static void Main(string[] args)
{
var wordSubdoc = new Document(@“First.docx”);

var builder = new DocumentBuilder(wordSubdoc);
builder.MoveToDocumentEnd();
builder.InsertBreak(BreakType.SectionBreakNewPage);
builder.MoveToDocumentEnd();

var body = wordSubdoc.LastSection.Body;
var lastNode = body.LastChild;

var docB = new Document(@“Second.docx”);
foreach (Section section in docB.Sections)
{
foreach (Node node in section.Body)
{
var importedNode = wordSubdoc.ImportNode(node, true);
body.InsertAfter(importedNode, lastNode);
lastNode = importedNode;
}
}

wordSubdoc.Save(“result.docx”);
}
}

EDIT: Added a png showing the offending line highlighted in orange

Hi Matt,

Thanks for your inquiry. In your case, I suggest you please use the Document.AppendDocument method to append the specified document to the end of this document. Please use the following code examples to achieve your requirements.

Hope this helps you. Please let us know if you have any more queries.


var wordSubdoc = new Document(MyDir + @"First.docx");

var docB = new Document(MyDir + @"Second.docx");

// Set the appended document to appear on a new page.

docB.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;

wordSubdoc.AppendDocument(docB, ImportFormatMode.KeepSourceFormatting);

wordSubdoc.Save(MyDir + "result.docx");

var wordSubdoc = new Document(MyDir + @"First.docx");

var builder = new DocumentBuilder(wordSubdoc);

builder.MoveToDocumentEnd();

builder.InsertBreak(BreakType.SectionBreakNewPage);

builder.MoveToDocumentEnd();

//builder.ListFormat.RemoveNumbers();

builder.ParagraphFormat.ClearFormatting();

var body = wordSubdoc.LastSection.Body;

var lastNode = body.LastChild;

var docB = new Document(MyDir + @"Second.docx");

foreach (Section section in docB.Sections)

{

foreach (Node node in section.Body)

{

var importedNode = wordSubdoc.ImportNode(node, true);

body.InsertAfter(importedNode, lastNode);

lastNode = importedNode;

}

}

wordSubdoc.Save(MyDir + "result.docx");