Import paragraph into another document to replace a bookmark

Hello,

I use Aspose.Word 2.4.1 with Java and i have problem to import a Paragraph from a document into another document to replace a bookmark…

I have always this error :
java.lang.IllegalArgumentException: The reference node is not a child of this node.

My code is :

/**
* Method to replace bookmarks by paragraphs
*
* @param document
* Source document
* @param bookmarks
* Map with the bookmarks to replace and the target images. The
* key is the bookmark name and the value is a paragrah object
* @return the updated document
* @throws Exception
*/
public static Document replaceBookmarksByParagraphs(Document document,
                                                    Hashtable bookmarks) throws Exception {

    // Check document input parameters
    if (document == null) {
        throw new IllegalArgumentException(
                "Document object can't be null !");
    }

    // Check bookmarks replacements list
    if (bookmarks == null || bookmarks.size() == 0) {
        return document;
    }

    // Create a builder to modify document
    DocumentBuilder documentBuilder = new DocumentBuilder(document);

    // Extract the FormFields collection of the document
    FormFields formFields = document.getRange().getFormFields();

    // Parse bookmarks values collections
    for (String bookmarkTagName : bookmarks.keySet()) {

        // Security check
        if ("".equals(bookmarkTagName.trim()))
            continue;

        // Get the associated FormField if exists
        FormField formField = formFields.get(bookmarkTagName.trim());

        // If yes we replace it with the associated paragraph (check also
        // that a
        // bookmark is associated with the FormField)
        if (formField != null
                && documentBuilder.moveToBookmark(bookmarkTagName.trim())) {

            // Delete the FormField
            // formFields.remove(formField);

            // Move to bookmarks
            documentBuilder.moveToDocumentStart();
            documentBuilder.moveToBookmark(bookmarkTagName.trim());

            // Import the paragraph associated
            Node newNode = document.importNode(bookmarks
                            .get(bookmarkTagName), true,
                    ImportFormatMode.KEEP_SOURCE_FORMATTING);
            Node paraNode = documentBuilder.getCurrentParagraph();
            System.out.println(paraNode.toTxt());
            document.insertAfter(newNode, paraNode);

        }

    }

    return document;
}

Thanks in advance for your help.

Best regards

Dominique

Hi
Thanks for your inquiry. I found the problem in your code. Please see the following code snippet.

// Import the paragraph associated
Node newNode = document.importNode(bookmarks.get(bookmarkTagName), true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
Node paraNode = documentBuilder.getCurrentParagraph();
System.out.println(paraNode.toTxt());
paraNode.getParentNode().insertAfter(newNode, paraNode);
// document.insertAfter(newNode, paraNode); //problem occured here

Hope this helps.
Best regards.

Hi,
Another time this week you have find my error in less than 1 hour ;o)
Thanks for you support (very quick) and for your work.
Best regards,
Dominique