We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

DocumentBuilder Cannot insert a node of this type at this location

billanalysisissue.zip (133.2 KB)
Document srcDoc = new Document(“src\asposewords\billanalysisissue\AB2808.docx”);
NodeCollection srcSdtNodes = srcDoc.getChildNodes(NodeType.STRUCTURED_DOCUMENT_TAG, true);

        Document destDoc = new Document("src\\asposewords\\billanalysisissue\\Bill Analysis.docx");
        DocumentBuilder docBuilder = new DocumentBuilder(destDoc);
        
        NodeImporter importer = new NodeImporter(srcDoc, destDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
        String[] fieldNames = destDoc.getMailMerge().getFieldNames();
        for (Iterator srcIterator = srcSdtNodes.iterator(); srcIterator.hasNext();) {
            StructuredDocumentTag srcSdtNode = (StructuredDocumentTag) srcIterator.next();
            for (String fieldName : fieldNames) {
                if (StringUtils.equals(srcSdtNode.getTag(), fieldName)) {                        
                    docBuilder.moveToMergeField(fieldName, true, true);
                    for (Iterator childNodeIterator = srcSdtNode.iterator(); childNodeIterator.hasNext();) {
                        Node node = importer.importNode((Node) childNodeIterator.next(), false);
                        if (NodeType.PARAGRAPH == node.getNodeType()) {
                            Paragraph para = (Paragraph) node;
                            para.getParagraphFormat().setAlignment(ParagraphAlignment.JUSTIFY);
                            para.getParagraphFormat().getStyle().getFont().setSize(12);
                        }
                        docBuilder.insertNode(node);
                    }
                    break;
                }
            }
        }
        destDoc.save("src\\asposewords\\billanalysisissue\\BillAnalysisIssue.docx");

insertNode is failing

@wrushasendakhane,

We are checking this scenario and will get back to you soon.

@wrushasendakhane,

Please try using the following code:

Document destDoc = new Document("D:\\temp\\billanalysisissue\\Bill Analysis.docx");
Document srcDoc = new Document("D:\\temp\\billanalysisissue\\AB2808.docx");

DocumentBuilder docBuilder = new DocumentBuilder(destDoc);

NodeCollection srcSdtNodes = srcDoc.getChildNodes(NodeType.STRUCTURED_DOCUMENT_TAG, true);

NodeImporter importer = new NodeImporter(srcDoc, destDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
String[] fieldNames = destDoc.getMailMerge().getFieldNames();
for (Iterator srcIterator = srcSdtNodes.iterator(); srcIterator.hasNext();) {
    StructuredDocumentTag srcSdtNode = (StructuredDocumentTag) srcIterator.next();
    for (String fieldName : fieldNames) {
        if (srcSdtNode.getTag().equals(fieldName)) {
            docBuilder.moveToMergeField(fieldName, true, true);
            for (Iterator childNodeIterator = srcSdtNode.iterator(); childNodeIterator.hasNext();) {
                Node node = importer.importNode((Node) childNodeIterator.next(), false);
                if (NodeType.PARAGRAPH == node.getNodeType()) {
                    Paragraph para = (Paragraph) node;
                    para.getParagraphFormat().setAlignment(ParagraphAlignment.JUSTIFY);
                    para.getParagraphFormat().getStyle().getFont().setSize(12);
                }

                if (node.getNodeType() == NodeType.PARAGRAPH){
                    Paragraph para = (Paragraph) node;
                    for (Node paraNodes : (Iterable<Node>) para.getChildNodes(NodeType.ANY, true)){
                        docBuilder.insertNode(paraNodes);
                    }
                }else
                {
                    docBuilder.insertNode(node);
                }
            }
            break;
        }
    }
}

destDoc.save("D:\\temp\\billanalysisissue\\awjava-18.11.docx"); 

Hope, this helps.