Inserting a document with different section break types into an other some section breaks changes type

Hello,
I need to insert a document into another document “master”.
The inserted document contains different types of section breaks: column, continuous, next page.
In the resulting document, some section breaks change type.
image.png (25.2 KB)
I reproduced the problem in the attached test project.
AsposeTest.zip (2.4 MB)
In this test the “master” document is empty, but in general it has a content an we need to insert the other document in a specific position, so it’s not possible to remove all the “master” document content or to modify it if not for the document insertion.
I tried to set section start of the first section of the inserted document to SectionStart.NewPage as suggested in another post, but it doesn’t change the behaviour.
How can I solve the problem?
Thanks

@federico.mameli You have encountered an expected behavior of Aspose.Words. Aspose.Words mimics MS Word behavior in this case, if you insert content of your document into the destination document in MS Word, it will be inserted the same way.
You see Section Break (New page) because the section where you insert content has section start new page. If you change your code like this:

NodeCollection childNodes = doc1.getChildNodes(NodeType.PARAGRAPH, true);
Node node = childNodes.get(0);
doc1.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
DocumentBuilder builder = new DocumentBuilder(doc1);
builder.moveTo(node);
builder.insertDocument(doc2, ImportFormatMode.USE_DESTINATION_STYLES);

You will see Section Break (Continuous), but still there will be a page break because section have different page size.

In your case you can change the code like this to fully keep sections from the source document:

builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);
doc2.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
for (Section s : doc2.getSections()) {
    Node dstSect = doc1.importNode(s, true, ImportFormatMode.USE_DESTINATION_STYLES);
    builder.getCurrentSection().getParentNode().insertBefore(dstSect, builder.getCurrentSection());
}

Sorry but none of these solutions seems to work:
In the first case, as you said, the last section break even if it appears as a section break continuous, it works as a section break new page.
In the second, I have a document that starts with a section break continuous that acts like a section break new page (that I don’t want neither continuous neither new page), and it ends with another section break continuous that acts like a section break new page that I don’t want…
Is there a way to obtain that I insert the exact content of the document into his “master”?

@federico.mameli As I mentioned this occurs because page size of inserted document and of the destination document is different. To have all the content on one page, you should change page size either of the destination section or of sections in the source document.

What do you mean by page size?

doc2.getFirstSection().getPageSetup().setPageHeight(doc1.getFirstSection().getPageSetup().getPageHeight());
doc2.getFirstSection().getPageSetup().setPageWidth(doc1.getFirstSection().getPageSetup().getPageWidth());

Can this reapeted for all sections solve the problem?
Thanks

@federico.mameli You can use PaperSize to set page size of section. For example see the following modified code:

doc2.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
for (Section s : doc2.getSections()) {
   Section dstSect = (Section)doc1.importNode(s, true, ImportFormatMode.USE_DESTINATION_STYLES);
 dstSect.getPageSetup().setPaperSize(builder.getCurrentSection().getPageSetup().getPaperSize());
    builder.getCurrentSection().getParentNode().insertBefore(dstSect, builder.getCurrentSection());
}

You can do the same when use InsertDocument methos - loop through the section in the source document and set page size the same as page size of the current section of DocumentBuilder.

Thanks!
It works for the document section break, but it inserts a new (not present in the original document) section break next page at the end.

To clarify:
I did two tests:

public void testInsertDoc() throws Exception {
    NodeCollection childNodes = doc1.getChildNodes(NodeType.PARAGRAPH, true);
    Node node = childNodes.get(0);
    DocumentBuilder builder = new DocumentBuilder(doc1);
    builder.moveTo(node);

    doc2.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
    for (Section s : doc2.getSections()) {
      s.getPageSetup().setPaperSize(builder.getCurrentSection().getPageSetup().getPaperSize());
    }
    builder.insertDocument(doc2, ImportFormatMode.USE_DESTINATION_STYLES);
    doc1.save("src/test/resources/merged.docx");
  }

and this is the result problem: image.png (8.5 KB)

second test:

public void testInsertSec() throws Exception {
    NodeCollection childNodes = doc1.getChildNodes(NodeType.PARAGRAPH, true);
    Node node = childNodes.get(0);
    DocumentBuilder builder = new DocumentBuilder(doc1);
    builder.moveTo(node);

    doc2.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
    for (Section s : doc2.getSections()) {
      Section dstSect = (Section)doc1.importNode(s, true, ImportFormatMode.USE_DESTINATION_STYLES);
      dstSect.getPageSetup().setPaperSize(builder.getCurrentSection().getPageSetup().getPaperSize());
      builder.getCurrentSection().getParentNode().insertBefore(dstSect, builder.getCurrentSection());
    }
    doc1.save("src/test/resources/mergedSections.docx");
  }

and this is the result problem: image.png (3.1 KB)

Thanks

@federico.mameli The first test should be modified like the following to get the desired result:

NodeCollection childNodes = doc1.getChildNodes(NodeType.PARAGRAPH, true);
Node node = childNodes.get(0);
DocumentBuilder builder = new DocumentBuilder(doc1);
builder.moveTo(node);

for (Section s : doc2.getSections()) {
    s.getPageSetup().setPaperSize(builder.getCurrentSection().getPageSetup().getPaperSize());
}
builder.insertDocument(doc2, ImportFormatMode.USE_DESTINATION_STYLES);
builder.getCurrentSection().getPageSetup().setSectionStart(doc2.getLastSection().getPageSetup().getSectionStart());