Inserting document

Hi,

I’m using your example code to insert a document:

public void InsertDocument(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;
        }
    }
}

I modified the code in this way to get a paragraph node:

if ((!insertAfterNode.NodeType.Equals(NodeType.Paragraph)) && (!insertAfterNode.NodeType.Equals(NodeType.Table)))
{
    insertAfterNode = insertAfterNode.GetAncestor(NodeType.Paragraph);
}

if ((!insertAfterNode.NodeType.Equals(NodeType.Paragraph)) && (!insertAfterNode.NodeType.Equals(NodeType.Table)))
{
    throw new ArgumentException("The destination node should be either a paragraph or table.");
}

I am passing in a node of type FieldStart which I get from the merge field I want to replace with the inserted document. The problem is that it seems to create a paragraph break where the code is inserted. So if the document looks like this:

insert document here: <>

after your code has executed the document looks like:

insert document here:
new document here.

is there anyway to insert it so the new document starts where the merge field was so the resulting document would look like:

insert document here: new document here.

Thanks,

Adam

Hi
Thanks for your request. I think the following implementation of InsertDocument could be useful for you.

public void InsertDocumentAtBookamrk(string bookmarkName, Document dstDoc, Document srcDoc)
{
    //Create DocumentBuilder
    DocumentBuilder builder = new DocumentBuilder(dstDoc);
    //Move cursor to bookmark and insert paragraph break
    builder.MoveToBookmark(bookmarkName);
    builder.Writeln();
    //Content of srcdoc will be inserted after this node
    Node insertAfterNode = builder.CurrentParagraph.PreviousSibling;
    //Content of first paragraph of srcDoc will be apended to this parafraph
    Paragraph insertAfterParagraph = insertAfterNode as Paragraph;
    //Content of last paragraph of srcDoc will be apended to this parafraph
    Paragraph insertBeforeParagraph = builder.CurrentParagraph;
    //We will be inserting into the parent of the destination paragraph.
    CompositeNode dstStory = insertAfterNode.ParentNode;
    //Remove empty paragraphs from the end of document
    while (!srcDoc.LastSection.Body.LastParagraph.HasChildNodes)
    {
        srcDoc.LastSection.Body.LastParagraph.Remove();
    }
    //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)
        {
            //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;
            }
            //If current paragraph is first paragraph of srcDoc 
            //then appent its content to insertAfterParagraph
            if (para.Equals(srcDoc.FirstSection.Body.FirstParagraph))
            {
                foreach (Node node in para.ChildNodes)
                {
                    Node dstNode = dstDoc.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting);
                    insertAfterParagraph.AppendChild(dstNode);
                }
                //If subdocument contains only one paragraph 
                //then copy content of insertBeforeParagraph to insertAfterParagraph
                //and remove insertBeforeParagraph
                if (srcDoc.FirstSection.Body.FirstParagraph.Equals(srcDoc.LastSection.Body.LastParagraph))
                {
                    while (insertBeforeParagraph.HasChildNodes)
                    {
                        insertAfterParagraph.AppendChild(insertBeforeParagraph.FirstChild);
                    }
                    insertBeforeParagraph.Remove();
                }
            }
            //If current paragraph is last paragraph of srcDoc 
            //then appent its content to insertBeforeParagraph
            if (para.Equals(srcDoc.LastSection.Body.LastParagraph))
            {
                Node previouseNode = null;
                foreach (Node node in para.ChildNodes)
                {
                    Node dstNode = dstDoc.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting);
                    if (previouseNode == null)
                    {
                        insertBeforeParagraph.InsertBefore(dstNode, insertBeforeParagraph.FirstChild);
                    }
                    else
                    {
                        insertBeforeParagraph.InsertAfter(dstNode, previouseNode);
                    }
                    previouseNode = dstNode;
                }
            }
            else
            {
                //This creates a clone of the node, suitable for insertion into the destination document.
                Node newNode = dstDoc.ImportNode(srcNode, true, ImportFormatMode.KeepSourceFormatting);
                //Insert new node after the reference node.
                dstStory.InsertAfter(newNode, insertAfterNode);
                insertAfterNode = newNode;
            }
        }
    }
}

You can just use MoveToMergeField method instead MoveToBookmark
Hope this helps.
Best regards.

Thanks…this looks like it works. Tough to tell for sure with all of the evaluation nag messages though!

Hi
Thanks for your request. If you want to test Aspose.Words without the evaluation version limitations, you can request a 30-day Temporary License. Please refer to
https://purchase.aspose.com/temporary-license
Best regards.

This code is not inserting the tables from documents that have tables in them. If the source document has a table this code:

foreach (Node srcNode in srcSection.Body)
{
    //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;
    }
… 

throws a null pointer exception because para is null.

Hi
Thanks for your request. I modified the code. Please try using this method:

public void InsertDocumentAtBookamrk(string bookmarkName, Document dstDoc, Document srcDoc)
{
    //Create DocumentBuilder
    DocumentBuilder builder = new DocumentBuilder(dstDoc);
    //Move cursor to bookmark and insert paragraph break
    builder.MoveToBookmark(bookmarkName);
    builder.Writeln();
    //Content of srcdoc will be inserted after this node
    Node insertAfterNode = builder.CurrentParagraph.PreviousSibling;
    //Content of first paragraph of srcDoc will be apended to this parafraph
    Paragraph insertAfterParagraph = insertAfterNode as Paragraph;
    //Content of last paragraph of srcDoc will be apended to this parafraph
    Paragraph insertBeforeParagraph = builder.CurrentParagraph;
    //We will be inserting into the parent of the destination paragraph.
    CompositeNode dstStory = insertAfterNode.ParentNode;
    //Remove empty paragraphs from the end of document
    while (!((CompositeNode)srcDoc.LastSection.Body.LastChild).HasChildNodes)
    {
        srcDoc.LastSection.Body.LastParagraph.Remove();
        if (srcDoc.LastSection.Body.LastChild == null)
            break;
    }
    //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)
        {
            //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;
            }
            //If current paragraph is first paragraph of srcDoc 
            //then appent its content to insertAfterParagraph
            if (para != null && para.Equals(srcDoc.FirstSection.Body.FirstChild))
            {
                foreach (Node node in para.ChildNodes)
                {
                    Node dstNode = dstDoc.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting);
                    insertAfterParagraph.AppendChild(dstNode);
                }
                //If subdocument contains only one paragraph 
                //then copy content of insertBeforeParagraph to insertAfterParagraph
                //and remove insertBeforeParagraph
                if (srcDoc.FirstSection.Body.FirstParagraph.Equals(srcDoc.LastSection.Body.LastChild))
                {
                    while (insertBeforeParagraph.HasChildNodes)
                    {
                        insertAfterParagraph.AppendChild(insertBeforeParagraph.FirstChild);
                    }
                    insertBeforeParagraph.Remove();
                }
            }
            //If current paragraph is last paragraph of srcDoc 
            //then appent its content to insertBeforeParagraph
            if (para != null && para.Equals(srcDoc.LastSection.Body.LastChild))
            {
                Node previouseNode = null;
                foreach (Node node in para.ChildNodes)
                {
                    Node dstNode = dstDoc.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting);
                    if (previouseNode == null)
                    {
                        insertBeforeParagraph.InsertBefore(dstNode, insertBeforeParagraph.FirstChild);
                    }
                    else
                    {
                        insertBeforeParagraph.InsertAfter(dstNode, previouseNode);
                    }
                    previouseNode = dstNode;
                }
            }
            else
            {
                //This creates a clone of the node, suitable for insertion into the destination document.
                Node newNode = dstDoc.ImportNode(srcNode, true, ImportFormatMode.KeepSourceFormatting);
                //Insert new node after the reference node.
                dstStory.InsertAfter(newNode, insertAfterNode);
                insertAfterNode = newNode;
            }
        }
    }
}

Hope this helps.
Best regards.

This works except it seems to insert parts of documents twice for some reason. I have attached the destination document and the document that was inserted into it. You can see on the 2nd to last page that the header appears twice…

Think I fixed it by adding another else:

private void InsertDocumentAtMergeField(string theMergeFieldName, Document dstDoc, Document srcDoc)
{
    //Create DocumentBuilder
    DocumentBuilder builder = new DocumentBuilder(dstDoc);

    //Move cursor to bookmark and insert paragraph break
    builder.MoveToMergeField(theMergeFieldName);
    builder.Writeln();

    //Content of srcdoc will be inserted after this node
    Node insertAfterNode = builder.CurrentParagraph.PreviousSibling;

    //Content of first paragraph of srcDoc will be apended to this parafraph
    Paragraph insertAfterParagraph = insertAfterNode as Paragraph;

    //Content of last paragraph of srcDoc will be apended to this parafraph
    Paragraph insertBeforeParagraph = builder.CurrentParagraph;

    //We will be inserting into the parent of the destination paragraph.
    CompositeNode dstStory = insertAfterNode.ParentNode;

    //Remove empty paragraphs from the end of document
    while (!((CompositeNode)srcDoc.LastSection.Body.LastChild).HasChildNodes)
    {
        srcDoc.LastSection.Body.LastParagraph.Remove();

        if (srcDoc.LastSection.Body.LastChild == null)
        {
            break;
        }
    }

    //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)
        {
            //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;
            }

            //If current paragraph is first paragraph of srcDoc
            //then appent its content to insertAfterParagraph
            if (para != null && para.Equals(srcDoc.FirstSection.Body.FirstChild))
            {
                foreach (Node node in para.ChildNodes)
                {
                    Node dstNode = dstDoc.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting);
                    insertAfterParagraph.AppendChild(dstNode);
                }

                //If subdocument contains only one paragraph
                //then copy content of insertBeforeParagraph to insertAfterParagraph
                //and remove insertBeforeParagraph
                if (srcDoc.FirstSection.Body.FirstParagraph.Equals(srcDoc.LastSection.Body.LastChild))
                {
                    while (insertBeforeParagraph.HasChildNodes)
                    {
                        insertAfterParagraph.AppendChild(insertBeforeParagraph.FirstChild);
                    }

                    insertBeforeParagraph.Remove();
                }
            }
            else
            {
                //If current paragraph is last paragraph of srcDoc
                //then appent its content to insertBeforeParagraph
                if (para != null && para.Equals(srcDoc.LastSection.Body.LastChild))
                {
                    Node previouseNode = null;
                    foreach (Node node in para.ChildNodes)
                    {
                        Node dstNode = dstDoc.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting);

                        if (previouseNode == null)
                        {
                            insertBeforeParagraph.InsertBefore(dstNode, insertBeforeParagraph.FirstChild);
                        }

                        else
                        {
                            insertBeforeParagraph.InsertAfter(dstNode, previouseNode);
                        }
                        previouseNode = dstNode;
                    }
                }

                else
                {
                    //This creates a clone of the node, suitable for insertion into the destination document.
                    Node newNode = dstDoc.ImportNode(srcNode, true, ImportFormatMode.KeepSourceFormatting);
                    //Insert new node after the reference node.
                    dstStory.InsertAfter(newNode, insertAfterNode);
                    insertAfterNode = newNode;
                }
            }
        }
    }
}

Hi

It is nice that you have already found solution independently.
Best regards.