Insert document @ bookmark

We are inserting multiple documents into one “master” document, based on bookmarks.

We are using the Code i believe we found in a different thread here or as one of the examples:

public static 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.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;
        }
    }
}

The problem we are having with this code is it always starts with a new paragraphs. Sometimes that’s fine, but alot of the time the “master” document is styled to perfection and the document should just be inserted as “formated text” if that makes sense.

Is there a way to accomplish this?

Hello
Thanks for your request. In this case, please try using the code below:

// Open documents.
Document dstDoc = new Document("C:\\Temp\\in.doc");
Document srcDoc = new Document("C:\\Temp\\src.doc");
InsertDocumentAtBookmark("Test", dstDoc, srcDoc);
dstDoc.Save("C:\\Temp\\out.doc", SaveFormat.Doc);
public static void InsertDocumentAtBookmark(String bookmarkName, Document dstDoc, Document srcDoc)
{
    NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.UseDestinationStyles);
    // Create DocumentBuilder
    DocumentBuilder builder = new DocumentBuilder(dstDoc);
    // Move cursor to bookmark and insert paragraph break
    builder.MoveToBookmark(bookmarkName);
    builder.Writeln();
    // If current paragraph is a list item, we should clear its formating.
    if (builder.CurrentParagraph.IsListItem)
        builder.ListFormat.RemoveNumbers();
    // 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 = null;
    if (insertAfterNode.NodeType == NodeType.Paragraph)
        insertAfterParagraph = (Paragraph)insertAfterNode;
    // 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.
        for (int nodeIdx = 0; nodeIdx < srcSection.Body.ChildNodes.Count; nodeIdx++)
        {
            Node srcNode = srcSection.Body.ChildNodes[nodeIdx];
            // Do not insert node if it is a last empty paragarph in the section.
            Paragraph para = null;
            if (srcNode.NodeType == NodeType.Paragraph)
                para = (Paragraph)srcNode;
            if ((para != null) && para.IsEndOfSection && (!para.HasChildNodes))
                break;
            // If current paragraph is first paragraph of srcDoc
            // then appent its content to insertAfterParagraph
            bool nodeInserted = false;
            if (para != null && para.Equals(srcDoc.FirstSection.Body.FirstChild))
            {
                nodeInserted = true; // set this flag to know that we already processed this node.

                for (int i = 0; i < para.ChildNodes.Count; i++)
                {
                    Node node = para.ChildNodes[i];
                    Node dstNode = importer.ImportNode(node, true);
                    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))
            {
                nodeInserted = true; // set this flag to know that we already processed this node.
                Node previouseNode = null;
                for (int i = 0; i < para.ChildNodes.Count; i++)
                {
                    Node node = para.ChildNodes[i];
                    Node dstNode = importer.ImportNode(node, true);
                    if (previouseNode == null)
                        insertBeforeParagraph.InsertBefore(dstNode, insertBeforeParagraph.FirstChild);
                    else
                        insertBeforeParagraph.InsertAfter(dstNode, previouseNode);
                    previouseNode = dstNode;
                }
            }
            if (!nodeInserted)
            {
                // This creates a clone of the node, suitable for insertion into the destination document.
                Node newNode = dstDoc.ImportNode(srcNode, true);
                // Insert new node after the reference node.
                dstStory.InsertAfter(newNode, insertAfterNode);
                insertAfterNode = newNode;
            }
        }
    }
}

Best regards,

Works like a charm, thank you very much!

I’m using this InsertDocumentAtBookmark method in my code right now but there are still a few flaws.

When inserting a document which contains only a list into the document that needs to be generated, the following goes wrong: (see Result.JPG).

I’ve attached the required documents to reproduce the unwanted result.

If you could fix this I’d be very grateful.

Thanks in advance,

Aleix

PS: There seems to be a bug when clicking on the “Content Selector” in the compose tab when replying to a message. I’m able to access a few shared folders(?) containing pictures of Mr. Vorobyev.
Ignore this if it is what that button is supposed to do.

``

Hi
Thanks for your request. Please try using the following modified code:

public static void InsertDocumentAtBookmark1(String bookmarkName, Document dstDoc, Document srcDoc)
{
    NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting);
    // Create DocumentBuilder
    DocumentBuilder builder = new DocumentBuilder(dstDoc);
    // Move cursor to bookmark and insert paragraph break
    builder.MoveToBookmark(bookmarkName);
    builder.Writeln();
    // If current paragraph is a list item, we should clear its formating.
    if (builder.CurrentParagraph.IsListItem)
        builder.ListFormat.RemoveNumbers();
    // 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 = null;
    if (insertAfterNode.NodeType == NodeType.Paragraph)
        insertAfterParagraph = (Paragraph)insertAfterNode;
    // 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.
        for (int nodeIdx = 0; nodeIdx < srcSection.Body.ChildNodes.Count; nodeIdx++)
        {
            Node srcNode = srcSection.Body.ChildNodes[nodeIdx];
            // Do not insert node if it is a last empty paragarph in the section.
            Paragraph para = null;
            if (srcNode.NodeType == NodeType.Paragraph)
                para = (Paragraph)srcNode;
            if ((para != null) && para.IsEndOfSection && (!para.HasChildNodes))
                break;
            // If current paragraph is first paragraph of srcDoc
            // then appent its content to insertAfterParagraph
            bool nodeInserted = false;
            if (para != null && para.Equals(srcDoc.FirstSection.Body.FirstChild))
            {
                nodeInserted = true; // set this flag to know that we already processed this node.

                if (!string.IsNullOrEmpty(insertAfterParagraph.ToTxt().Trim()))
                {
                    for (int i = 0; i < para.ChildNodes.Count; i++)
                    {
                        Node node = para.ChildNodes[i];
                        Node dstNode = importer.ImportNode(node, true);
                        insertAfterParagraph.AppendChild(dstNode);
                    }
                }
                else
                {
                    Paragraph dstNode = (Paragraph)dstDoc.ImportNode(para, true);
                    insertAfterParagraph.ParentNode.InsertAfter(dstNode, insertAfterParagraph);
                    while (insertAfterParagraph.HasChildNodes)
                        dstNode.PrependChild(insertAfterParagraph.LastChild);
                    insertAfterParagraph.Remove();
                    insertAfterParagraph = dstNode;
                    insertAfterNode = 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))
            {
                nodeInserted = true; // set this flag to know that we already processed this node.
                Paragraph dstNode = (Paragraph)dstDoc.ImportNode(para, true);
                insertBeforeParagraph.ParentNode.InsertBefore(dstNode, insertBeforeParagraph);
                while (insertBeforeParagraph.HasChildNodes)
                    dstNode.AppendChild(insertBeforeParagraph.FirstChild);
                insertBeforeParagraph.Remove();
                insertBeforeParagraph = dstNode;
            }
            if (!nodeInserted)
            {
                // This creates a clone of the node, suitable for insertion into the destination document.
                Node newNode = dstDoc.ImportNode(srcNode, true);
                // Insert new node after the reference node.
                dstStory.InsertAfter(newNode, insertAfterNode);
                insertAfterNode = newNode;
            }
        }
    }
}

Hope this helps.
Best regards,

The issues you have found earlier (filed as WORDSNET-5251) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(20)