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

Free Support Forum - aspose.com

importNode imports entire contents as Shape when any of the child nodes contain a Shape

importNode imports entire contents as Shape when any of the child nodes contain a Shape. I’m executing the below method. When the node and child nodes of the src document are all text (paragraphs with runs) it works as expected. I any of the child nodes is a shape the entire contents (node and all child nodes) are imported as a single non-editable Shape object. I expected that each node would be imported as the same node type as in the src document.

public static void setValue(final Document dstDoc, final String name, final Document srcDoc, final int importFormatMode)
		throws WTException {
	final Bookmark bookmark = dstDoc.getRange().getBookmarks().get(name);
	Node destination = bookmark.getBookmarkStart().getParentNode();
	if (!matchesAnyNodeType(destination, NodeType.PARAGRAPH, NodeType.TABLE)) {
		throw new IllegalArgumentException("The destination node should be either a paragraph or table.");
	}
	final CompositeNode<?> destinationParent = destination.getParentNode();
	final NodeImporter importer = newNodeImpter(srcDoc, destination.getDocument(), importFormatMode);
	for (final Section section : srcDoc.getSections()) {
		for (final Node node : (Iterable<Node>) section.getBody().getChildNodes()) {
			if (node.getNodeType() == NodeType.PARAGRAPH) {
				if (((Paragraph) node).isEndOfSection() && !((Paragraph) node).hasChildNodes()) {
					continue;
				}
			}
			final Node clone = importNode(importer, node, true);
			destinationParent.insertAfter(clone, destination);
			destination = clone;
		}
	}
}

@stsdan Could you please attach your document here for testing? Unfortunately, your question is not clear enough. In your code you simply import child nodes from the source document into the destination, so the child nodes should be imported without changes. If there was not editable shape in the source document it is imported as not editable shape in the destination document.

Alexey -
I’m uploading a srcDoc file that has the information to be imported and a resultingDoc that is what I get after the import action is executed. Note that in the srcDoc file the text (runs) are editable and in the resultingDoc the entire contents of the srcDoc appears to have been imported as a shape.resultingDoc.docx (81.7 KB)
srcDoc.docx (91.5 KB)

@stsdan Thank you for additional information. I have tested the scenario on my side and cannot reproduce the problem. Here is my test code, input and output documents:

Document dst = new Document("C:\\Temp\\dst.docx");
Document src = new Document("C:\\Temp\\src.docx");
setValue(dst, "test", src, ImportFormatMode.KEEP_SOURCE_FORMATTING);
dst.save("C:\\Temp\\out.docx");
public static void setValue(final Document dstDoc, final String name,
                                final Document srcDoc, final int importFormatMode)
            throws Exception {
    final Bookmark bookmark = dstDoc.getRange().getBookmarks().get(name);
    Node destination = bookmark.getBookmarkStart().getParentNode();
    if (destination.getNodeType() != NodeType.PARAGRAPH &&
            destination.getNodeType() != NodeType.TABLE) {
        throw new IllegalArgumentException("The destination node should be either a paragraph or table.");
    }
    final CompositeNode<?> destinationParent = destination.getParentNode();
    final NodeImporter importer = new NodeImporter(srcDoc, destination.getDocument(), importFormatMode);
    for (final Section section : srcDoc.getSections()) {
        for (final Node node : (Iterable<Node>) section.getBody().getChildNodes()) {
            if (node.getNodeType() == NodeType.PARAGRAPH) {
                if (((Paragraph) node).isEndOfSection() && !((Paragraph) node).hasChildNodes()) {
                    continue;
                }
            }
            final Node clone = importer.importNode(node, true);
            destinationParent.insertAfter(clone, destination);
            destination = clone;
        }
    }
}

srcDoc.docx (91.5 KB)
dst.docx (12.0 KB)
out.docx (91.8 KB)
As you can see content of the source document is properly imported into the destination document.

Alexey -

I’ve run that code and it works for me as well. I extracted the code and the data samples as best I could from an application that is actually retrieving the srcDoc data from a database as a stored RTF. So this is proof that it is not aspose that is importing incorrectly, it must be something in the way the byte array is being handled prior to the import. Strange, though, that I generated the srcDoc.docx file that I provided by placing a save action in the code immediately before I do the import.

I appreciate your quick help with this. I’ll continue debugging my code and see if I can find where it went wrong.

@stsdan Thank you for additional information. Please feel free to ask if you need more assistance from our side, we are always glad to help you.
Maybe the problem is with the RFT stored in the database?

Alexey -

I found the problem. A careless mistake. Instead of …

for (final Node node : (Iterable) section.getBody().getChildNodes())

I had …

for (final Node node : (Iterable) section.getBody())

Fixing that resolved the problem.

1 Like