How to merge two dynamically generated document

Hi,
I want to merge two document which are generated dynamically.

  1. first document is generated through APIs
  2. Second document is created through Mail merge method.

Now i want to merge second document to first one.
the example which is given in documentation wants that the document should have MailMerge field but i don’t know how to insert mailmerge field dynamically.
how can i merge these two document ?

Hi
Thanks for your request. You can insert the document at any location. See the following code example. This code inserts the document at the end of the destination document.

Document doc1 = new Document(@"265_98568_jonathanh\in1.doc");
Document doc2 = new Document(@"265_98568_jonathanh\in2.doc");
InsertDocument(doc1.LastSection.Body.LastParagraph, doc2);
doc1.Save(@"265_98568_jonathanh\out.doc");

Also see the following link.
https://docs.aspose.com/words/net/insert-and-append-documents/
I hope that it will help you.
Best regards.

Thanks a lot.

The mentioned URL is not working…

Hi
Here is code of InsertDocument method.

/// 
/// Inserts content of the external document after the specified node.
/// Section breaks and section formatting of the inserted document are ignored.
/// The insertion is going on the story child level, so the specified node should be either paragraph or table.
/// 
/// Node in the destination document after which the external document content should be inserted.
/// This node should be a story child level node (paragraph or table).
/// Document to insert.
public void InsertDocument(Node insertAfterNode, Document srcDoc)
{
    // We need to make sure that the specified node is either pargraph or table.
    if (!((insertAfterNode.NodeType == NodeType.Paragraph) || (insertAfterNode.NodeType == NodeType.Table)))
        throw new ArgumentException("The destination node should be either 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)
    {
        int i = 0;
        // Loop through all block level nodes (paragraphs and tables) in the body of the section.
        foreach (Node srcNode in srcSection.Body)
        {
            i++;
            // Do not insert node if it is a last empty paragarph in the section.
            Paragraph para = srcNode as Paragraph;
            if ((para != null) && para.IsEndOfSection && !para.HasChildNodes)
                break;
            // 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
Thanks for you reply…
The code is working but it is not fulfilling my requirement.
I need to add the another document as new page not at the end of the page…
could you please help me regarding the same.
Regards
Jaipal

Hi
Thank you for inquiry. You can also use ApendDoc method. For example see the following code.

public void TestMergeDocument()
{
    Document doc1 = new Document("in1.doc");
    Document doc2 = new Document("in2.doc");
    AppendDoc(doc1, doc2);
    doc1.Save("out.doc");
}
public void AppendDoc(Document dstDoc, Document srcDoc)
{
    for (int i = 0; i < srcDoc.Sections.Count; i++)
    {
        Section newSection = (Section)dstDoc.ImportNode(srcDoc.Sections[i], true, ImportFormatMode.KeepSourceFormatting);
        dstDoc.Sections.Add(newSection);
    }
}

I hope that this will help you.
Best regards.

Hi
Thank you for inquiry. You can also use ApendDoc method. For example see the following code.

public void TestMergeDocument()
{
    Document doc1 = new Document("in1.doc");
    Document doc2 = new Document("in2.doc");
    AppendDoc(doc1, doc2);
    doc1.Save("out.doc");
}
public void AppendDoc(Document dstDoc, Document srcDoc)
{
    for (int i = 0; i < srcDoc.Sections.Count; i++)
    {
        Section newSection = (Section)dstDoc.ImportNode(srcDoc.Sections[i], true, ImportFormatMode.KeepSourceFormatting);
        dstDoc.Sections.Add(newSection);
    }
}

I hope that this will help you.
Best regards.