Java importing a document at a bookmark position

No doubt I do something wrong but I try to insert a document at at bookmark position using the below piece of code (much of it comes from sample code).

Unfortunately it seems to replace the content of the original document (doc) and then move the inserted document (insertionDoc) to the top

The original.doc, insertion.doc and result.doc are attached.

// I move to the bookmark here
try {
    // Get the current node (the position after the bookmark)
    final Node node = builder.getCurrentNode();
    int index = 0;
    if (node.getAncestor(Cell.class) != null || node.getAncestor(Shape.class) != null) {
        index = node.getParentNode().getChildNodes().indexOf(node);
        for (int j = 0; j < insertionDoc.getSections().getCount(); j++)
        final Section section = insertionDoc.getSections().get(j);
        final NodeCollection children = section.getBody().getChildNodes();
        for (final Node child : children) {
            if (child.getNodeType() == NodeType.PARAGRAPH || child.getNodeType() == NodeType.TABLE) {
                if (child.getNodeType() == NodeType.PARAGRAPH && child == section.getBody().getLastChild()) {
                    break;
                }
                final Node insertedNode = doc.importNode(child, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
                node.getParentNode().getChildNodes().insert(index++, insertedNode);
            }
        }
    }
} else {
    Section dstSection = null;
    final Body body = (Body) node.getAncestor(Body.class);
    if (body.getLastChild() != node) {
        final DocumentBuilder builder = new DocumentBuilder(doc);
        builder.moveTo(node);
        dstSection = builder.getCurrentParagraph().getParentSection();
    } else {
        dstSection = (Section) node.getAncestor(Section.class);
    }
    index = doc.getSections().indexOf(dstSection);
    for (int k = 0; k < insertionDoc.getSections().getCount(); k++) {
        final Section section = insertionDoc.getSections().get(k);
        final Section insertedSection = (Section) doc.importNode(section, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
        insertedSection.clearHeadersFooters();
        doc.getSections().insert(index++, insertedSection);
    }
}
} catch (final Exception e) {
//
}

// I remove the bookmark here

Hi
Thanks for your request. Please try using the following code as example to insert Document at bookmark.

Document original = new Document("C:\\Temp\\original.doc");
Document insertionDoc = new Document("C:\\Temp\\insertion.doc");
InsertDocumentAtBookmark("LetRules", original, insertionDoc);
original.save("C:\\Temp\\out.doc");
// .............................................
public static void InsertDocumentAtBookmark(String bookmarkName, Document dstDoc, Document srcDoc) throws Exception
{
    // 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.getCurrentParagraph().getPreviousSibling();
    // Content of first paragraph of srcDoc will be apended to this parafraph
    Paragraph insertAfterParagraph = (Paragraph)insertAfterNode;
    // Content of last paragraph of srcDoc will be apended to this parafraph
    Paragraph insertBeforeParagraph = builder.getCurrentParagraph();
    // We will be inserting into the parent of the destination paragraph.
    CompositeNode dstStory = insertAfterNode.getParentNode();
    // Create temporary list
    List srcTmpList = null;
    List dstTmpList = null;
    // Remove empty paragraphs from the end of document
    while (!srcDoc.getLastSection().getBody().getLastParagraph().hasChildNodes())
    {
        srcDoc.getLastSection().getBody().getLastParagraph().remove();
    }
    // Loop through all sections in the source document.
    int sectCount = srcDoc.getSections().getCount();
    for (int sectIndex = 0; sectIndex < sectCount; sectIndex++)
    {
        Section srcSection = srcDoc.getSections().get(sectIndex);
        // Loop through all block level nodes (paragraphs and tables) in the body of the section.
        int nodeCount = srcSection.getBody().getChildNodes().getCount();
        for (int nodeIndex = 0; nodeIndex < nodeCount; nodeIndex++)
        {
            Node srcNode = srcSection.getBody().getChildNodes().get(nodeIndex);
            // Do not insert node if it is a last empty paragarph in the section.
            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
            if (para.equals(srcDoc.getFirstSection().getBody().getFirstParagraph()) && insertAfterParagraph.hasChildNodes())
            {
                int firstParaChildCount = para.getChildNodes().getCount();
                for (int childIndex = 0; childIndex < firstParaChildCount; childIndex++)
                {
                    Node node = para.getChildNodes().get(childIndex);
                    Node dstNode = dstDoc.importNode(node, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
                    insertAfterParagraph.appendChild(dstNode);
                }
                // If subdocument contains only one paragraph
                // then copy content of insertBeforeParagraph to insertAfterParagraph
                // and remove insertBeforeParagraph
                if (srcDoc.getFirstSection().getBody().getFirstParagraph().equals(srcDoc.getLastSection().getBody().getLastParagraph()))
                {
                    while (insertBeforeParagraph.hasChildNodes())
                    {
                        insertAfterParagraph.appendChild(insertBeforeParagraph.getFirstChild());
                    }
                    insertBeforeParagraph.remove();
                }
            }
            // If current paragraph is last paragraph of srcDoc
            // then appent its content to insertBeforeParagraph
            else if (para.equals(srcDoc.getLastSection().getBody().getLastParagraph()) && insertBeforeParagraph.hasChildNodes())
            {
                Node previouseNode = null;
                int firstParaChildCount = para.getChildNodes().getCount();
                for (int childIndex = 0; childIndex < firstParaChildCount; childIndex++)
                {
                    Node node = para.getChildNodes().get(childIndex);
                    Node dstNode = dstDoc.importNode(node, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
                    if (previouseNode == null)
                    {
                        insertBeforeParagraph.insertBefore(dstNode, insertBeforeParagraph.getFirstChild());
                    }
                    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.KEEP_SOURCE_FORMATTING);
                if (srcNode.getNodeType() == NodeType.PARAGRAPH)
                {
                    Paragraph srcTmpPar = (Paragraph)srcNode;
                    Paragraph dstTmpPar = (Paragraph)newNode;
                    if (srcTmpPar.getListFormat().getList() == srcTmpList)
                    {
                        dstTmpPar.getListFormat().setList(dstTmpList);
                    }
                    else
                    {
                        srcTmpList = srcTmpPar.getListFormat().getList();
                        dstTmpList = dstTmpPar.getListFormat().getList();
                    }
                }
                // Insert new node after the reference node.
                dstStory.insertAfter(newNode, insertAfterNode);
                insertAfterNode = newNode;
            }
        }
        if (!insertAfterParagraph.hasChildNodes())
            insertAfterParagraph.remove();
        if (!insertBeforeParagraph.hasChildNodes())
            insertBeforeParagraph.remove();
    }
}

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.
(11)