Comment ranges that are across paragraphs are being removed when saving

I am importing nodes from another document that contain comments & comment ranges. When a comment has a range across several paragraphs, the comment range tags appear to be removed when saving the document.

private static void TestCommentRange(Document sourceDocument) throws Exception {

    Document destinationDocument = new Document();
   
    NodeCollection nc = sourceDocument.getFirstSection().getBody().getChildNodes();
    for (Object node : nc) {
        if (node instanceof Node) {
            Node dstNode = destinationDocument.importNode((Node) node, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
            destinationDocument.getFirstSection().getBody().appendChild(dstNode);
        }
    }

    destinationDocument.save("C:\\reportresults\\DestinationDocument.docx", SaveFormat.DOCX);
}


SourceDocument.docx (16.9 KB)

@faith113 You should use NodeImporter to keep comment range. Please see the following code:

Document src = new Document("C:\\Temp\\SourceDocument.docx");
Document dst = new Document();

NodeImporter importer = new NodeImporter(src, dst, ImportFormatMode.KEEP_SOURCE_FORMATTING);
Iterable<Node> nc = src.getFirstSection().getBody().getChildNodes(NodeType.ANY, false);
for (Node node : nc)
{
    Node dstNode = importer.importNode(node, true);
    dst.getFirstSection().getBody().appendChild(dstNode);
}

dst.save("C:\\Temp\\out.docx", SaveFormat.DOCX);

Thank you! Thank fixes the issue. There is one other issue related to this that I am still seeing. If I surround the import code with a destinationDocument.startTrackRevisions(“Owner”); and a destinationDocument.stopTrackRevisions(); the comment doesn’t appear properly. See attachment.

The rationale for doing this is so that the inserted content will appear as a change in the destination document. Then it can be toggled with the review visibility options. “All Markup”, “Original”, etc.

@faith113 Unfortunately, I cannot reproduce the problem on my side using the latest 24.1 version of Aspose.Words for Java. I have used the same code with modification you have mentioned:

Document src = new Document("C:\\Temp\\SourceDocument.docx");
Document dst = new Document();

dst.startTrackRevisions("Aspose");

NodeImporter importer = new NodeImporter(src, dst, ImportFormatMode.KEEP_SOURCE_FORMATTING);
Iterable<Node> nc = src.getFirstSection().getBody().getChildNodes(NodeType.ANY, false);
for (Node node : nc)
{
    Node dstNode = importer.importNode(node, true);
    dst.getFirstSection().getBody().appendChild(dstNode);
}

dst.stopTrackRevisions();

dst.save("C:\\Temp\\out.docx", SaveFormat.DOCX);

Here is the output produced on my side: out.docx (11.8 KB)

I have the same issue with your document. I just tried another machine with a different version of office and it works there. It looks to be some inconsistency between the Word versions.

It works with: Office LTSC Professional Plus 2021
It doesn’t work with: MS 365 Apps for Enterprise

Thank you for your help!

@faith113 I use MS Office Professional 2019 on my side.

1 Like