How to copy a section from another document

I got exception when I tried to insert a section into a document where the section is cloned from another document. The exception is ‘The newChild was created from a different document than the one that created this node’.
The documentation said ‘You can create a copy of a section using Clone. The copy can be inserted into the same or different document.’

Please check the code below and let me know what I have done wrong ? Thanks

var document = new Document();
int i = 0;
while (i <_mergedDocument.Sections.Count)
{
    var section = _mergedDocument.Sections[i].Clone();
    document.Sections.Add(section);
    i++;
}

Hello,

Thanks for your request. Could you please attach you input documents here for testing? I will check them and provide some feedback.

Best regards,

I attached a zip file containing the small project trying to copy different sections of 2 files and insert into a new file.
Please copy 604.docx and the other docx into root c: before running the project.
The program was trying to copy the sections from Surgical report template_Aspose-1.docx until reaching a section containing ‘~eh’, then copy sections from 604.docx after ‘~eh’ in that file.

Hi
Thanks for your request. Before inserting nodes from one document into another, you should import these nodes. You can use NodeImporter or Document.ImportNode method:
https://reference.aspose.com/words/net/aspose.words/nodeimporter/
Also, if you simply need to append one document to another, you can use Document.AppendDocument method:
https://reference.aspose.com/words/net/aspose.words/document/appenddocument/
Best regards,

The Document.ImportNode() does the trick and my sample program works now. Thanks a lot for your help.

Hi
It is perfect, that you already found the solution. If you need more assistance, I will be glad to help you.
Best regards,

Thanks for your help so far, however, my sample program does not work completely as it does not import header for the source document.
Attached is my sample program with documents included. Please put 604.docx and Surgical report template_Aspose-1.docx file to C:\ before running the program.
If you look at the final output, the 1st section of document does not contain same header as the Surgical report template_Aspose-1.docx.
I am sure that I need to add extra code to import header. Your insight is greatly appreciated.

Hello
Thanks for your inquiry. Unfortunately, I am not sure what you would like to achieve. Could you please attach your expected output document? I will check it and provide you more information.
Best regards,

I attached the output file.
In the attached project, I would like to merge the header of Surgical report template_Aspose-1.docx and body of 604.docx into the file 604-1.docx. These 2 files are in the zip file and need to be in c:\ before running project.
If you open the 2 source documents 604.docx & Surgical report template_Aspose-1.docx, the top of both files have header with table and logo. I would like to see the header of Surgical report template_Aspose-1.docx appear in 604-1.docx
The attached project after running it and clicked Merge Document button, will generate 604 - 1. docx in c:\\
Hope I have explained clearly.

Hello
Thank you for additional information. In this case you can try using more simple code:

Document dstDoc = new Document("C:\\Temp\\Surgical report template_Aspose-1.docx");
Document srcDoc = new Document("C:\\Temp\\604.docx");
foreach(Section section in dstDoc.Sections)
{
    section.Body.RemoveAllChildren();
}
dstDoc.AppendDocument(srcDoc, ImportFormatMode.UseDestinationStyles);
dstDoc.Save("C:\\Temp\\out.docx");

Hope this helps.
Best regards,

Instead of doing section.Body.RemoveAllChildren() which leaves empty section breaks in document, I remove section altogether and then append the other document. That does the trick. Thanks for your help.

Hi there,
It’s great things are working now, please feel free to ask any time you have any queries.
Thanks,