Could not merge documents with columns

Hi,

I want to merge two documents into one. Both have two columns.
I use function ‘AppendDocument’. If I set ‘SectionStart.NewPage’ , it looks good. If i set ‘Section.Continuous’ is not attached properly. The text sequence is no longer correct. If i remove the sectionbreak ‘Continous’ manually then it looks good.

I add my sample documens

Regards,
Guido

Hello
Thanks for your request. If you try doing the same in MS Word you will get exactly the same result. In your case you should use the code like the following co achieve what you need:

Document dst = new Document("C:\\Temp\\Chaper1.docx");
Node insertAfterNode = dst.FirstSection.Body.LastParagraph;
Document src = new Document("C:\\Temp\\Chapter2.docx");
InsertDocumentAtTheEnd(insertAfterNode, src);
dst.Save("C:\\Temp\\out.docx");
public void InsertDocumentAtTheEnd(Node insertAfterNode, Document srcDoc)
{
    // Make sure that the node is either a pargraph or table.
    if ((!insertAfterNode.NodeType.Equals(NodeType.Paragraph)) & (!insertAfterNode.NodeType.Equals(NodeType.Table)))
        throw new ArgumentException("The destination node should be either a paragraph or table.");
    // We will be inserting into the parent of the destination paragraph.
    CompositeNode dstStory = insertAfterNode.ParentNode;
    // This object will be translating styles and lists during the import.
    NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.Document,
        ImportFormatMode.KeepSourceFormatting);
    // Loop through all sections in the source document.
    foreach(Section srcSection in srcDoc.Sections)
    {
        // Loop through all block level nodes (paragraphs and tables) in the body of the section.
        foreach(Node srcNode in srcSection.Body)
        {
            // Let's skip the node if it is a last empty paragarph in a section.
            if (srcNode.NodeType.Equals(NodeType.Paragraph))
            {
                Paragraph para = (Paragraph) srcNode;
                if (para.IsEndOfSection && !para.HasChildNodes)
                    continue;
            }
            // This creates a clone of the node, suitable for insertion into the destination document.
            Node newNode = importer.ImportNode(srcNode, true);
            // Insert new node after the reference node.
            dstStory.InsertAfter(newNode, insertAfterNode);
            insertAfterNode = newNode;
        }
    }
}

Best regards,

Hi,

how should modify the code if the source documents has different sections, e.g. first section has two columns with some text, second section has one column with a table and third section has two columns again ?

Regards,
Guido

Hi,

how should i modify the code if the source document has different sections, e.g. first section has two columns with some text, second section has one column with a table and third section has two columns again ?

Regards,
Guido

Hi Guido,

Thanks for your inquiry. Please use the latest version of Aspose.Words for .NET. In your case, I suggest you to use the InsertDocument method to merge documents shared at following documentation link.

https://docs.aspose.com/words/java/insert-and-append-documents/

Document mainDoc = new Document(MyDir + "Chaper1.docx");
Document subDoc = new Document(MyDir + "Chapter2.docx");
InsertDocument(mainDoc.LastSection.Body.FirstParagraph, subDoc);
mainDoc.Save(MyDir + "AsposeOut.docx");

Please read following documentation link for your kind reference and let us know if you have any more queries.
https://docs.aspose.com/words/net/insert-and-append-documents/