Free Support Forum - aspose.com

Merging Documents at a Bookmark

I'm trying to merge two documents. I'd like the second document to be inserted at a particular location in the master document. I've used a bookmark to mark the spot. When it goes in, I'd like the numbering scheme in place to account for the new section. Both documents have exactly the same styles.

I began with some sample code from this site. But the code appends the new document, it does not insert the new document at an arbitrary location.

Thanks in advance!

private void button1_Click(object sender, System.EventArgs e)

{

Aspose.Words.Document master_document;

Aspose.Words.Document child_document;

Aspose.Words.DocumentBuilder builder;

master_document = new Document("master.doc");

child_document = new Document("child.doc");

builder = new DocumentBuilder(master_document);

builder.MoveToBookmark("CHILDDOC");

AppendDoc(master_document, child_document);

master_document.Save("final.doc", Aspose.Words.SaveFormat.FormatDocument);

}

public void AppendDoc(Document dstDoc, Document srcDoc)

{

// Loop through all sections in the source document.

// Section nodes are immediate children of the Document node so we can just enumerate the Document.

foreach (Section srcSection in srcDoc)

{

// Because we are copying a section from one document to another,

// it is required to import the Section node into the destination document.

// This adjusts any document-specific references to styles, lists, etc.

//

// Importing a node creates a copy of the original node, but the copy

// is ready to be inserted into the destination document.

Node dstSection = dstDoc.ImportNode(srcSection, true, ImportFormatMode.KeepSourceFormatting);

// Now the new section node can be appended to the destination document.

dstDoc.AppendChild(dstSection);

}

}

---------------------------------
www.technosutra.com

I tried creating this variation of AppendDoc and got an error saying:

"Cannot insert a node of this type at this location"

It was referring to the InsertAfter() statement.

Help!

        public void AppendDoc(DocumentBuilder dstDoc, Document srcDoc)
        {
            Node insertPositionNode;
            insertPositionNode = dstDoc.CurrentNode;

            // Loop through all sections in the source document. 
            // Section nodes are immediate children of the Document node so we can just enumerate the Document.
            foreach (Section srcSection in srcDoc)
            {
                foreach (Node n in srcSection.Body.ChildNodes)
                {
                    dstDoc.MoveTo(insertPositionNode);
                    insertPositionNode = dstDoc.CurrentNode.ParentNode.InsertAfter(dstDoc.Document.ImportNode(n, true, ImportFormatMode.KeepSourceFormatting), dstDoc.CurrentNode);
                }
            }
        }

The problem of inserting the document at the arbitrary location in the file was recently discussed in the following thread:

http://www.aspose.com/Community/forums/3/51201/ShowThread.aspx

I have posted a nice code example there. Please check it and let me know if it fits your needs.