Insert comment problem

Dear Team,

I have problem while inserting comment in the source document. I have attached sample screenshot : comment.zip (27.4 KB)

While manually added comment at text “recent” is enclosed with commentRangeStart and End tags in ooxml file.

But, comments inserted at runtime for the text “years” using the solution provided fails to add tags commentRangeStart and End tags in the XML file via Aspose.

Code:
int CommentID=0;
CommentRangeStart start = new CommentRangeStart(document, CommentID);
CommentRangeEnd end = new CommentRangeEnd(document, CommentID);

Is this related to comment id, when we are inserting multiple comments via ASPOSE the same is not working.

Attached the currently working code. code.zip (1.3 KB)

Please provide a workaround solution to solve the same.

Current Output : Output.zip (21.8 KB)

Expected OP : expected_output.zip (22.0 KB)

@ssvel,

Yes, the Comment ID matters in this case. Please see the following simple example that shows how to anchor a comment to a region of text.

Document doc = new Document();

Paragraph para1 = new Paragraph(doc);
Run run1 = new Run(doc, "Some ");
Run run2 = new Run(doc, "text ");
para1.appendChild(run1);
para1.appendChild(run2);
doc.getFirstSection().getBody().appendChild(para1);

Paragraph para2 = new Paragraph(doc);
Run run3 = new Run(doc, "is ");
Run run4 = new Run(doc, "added ");
para2.appendChild(run3);
para2.appendChild(run4);
doc.getFirstSection().getBody().appendChild(para2);

Comment comment = new Comment(doc, "Awais Hafeez", "AH", new Date());
comment.getParagraphs().add(new Paragraph(doc));
comment.getFirstParagraph().getRuns().add(new Run(doc, "Comment text."));

CommentRangeStart commentRangeStart = new CommentRangeStart(doc, comment.getId());
CommentRangeEnd commentRangeEnd = new CommentRangeEnd(doc, comment.getId());

run1.getParentNode().insertAfter(commentRangeStart, run1);
run3.getParentNode().insertAfter(commentRangeEnd, run3);
commentRangeEnd.getParentNode().insertAfter(comment, commentRangeEnd);

doc.save("D:\\Temp\\awjava-18.4.docx");

@awais.hafeez

Thanks for your quick replying. Its working fine.

@awais.hafeez

We need to insert comment at specific locations, I have Word start and end offset i.e location and the word where comment needs to be inserted.

Example :
Start & End Range : {406 - 414, 1422 - 1429, 990 - 995, 782 - 790}
Word : {client’s,essence,wants,client’s}

Input : input.zip (9.9 KB)

Expected OP : Expected OP.zip (12.4 KB)

@ssvel,

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

@ssvel,

Thanks for being patient. The following code example will insert comment at the desired Range of Run nodes (see awjava-18.4.zip (14.1 KB)). Hope, this helps.

Document doc = new Document("D:\\Temp\\input\\input.docx");

Node[] runs = doc.getChildNodes(NodeType.RUN, true).toArray();
for (int i = 0; i < runs.length; i++) {
    Run run = (Run) runs[i];
    int length = run.getText().length();

    Run currentNode = run;
    for (int x = 1; x < length; x++) {
        currentNode = splitRun(currentNode, 1);
    }
}

commentRangeOfRuns(doc, "Some Text", 406, 414);
commentRangeOfRuns(doc, "Some Text", 1422, 1429);
commentRangeOfRuns(doc, "Some Text", 990, 995);
commentRangeOfRuns(doc, "Some Text", 782, 790);

doc.save("D:\\Temp\\input\\awjava-18.4.docx");

private static void commentRangeOfRuns(Document doc, String commentText, int start, int end){
    Comment comment = new Comment(doc, "Awais Hafeez", "AH", new Date());
    comment.getParagraphs().add(new Paragraph(doc));
    //comment.getFirstParagraph().getRuns().add(new Run(doc, commentText));

    CommentRangeStart commentRangeStart = new CommentRangeStart(doc, comment.getId());
    CommentRangeEnd commentRangeEnd = new CommentRangeEnd(doc, comment.getId());

    Node[] runs = doc.getChildNodes(NodeType.RUN, true).toArray();

    runs[start - 1].getParentNode().insertBefore(commentRangeStart, runs[start - 1]);
    runs[end - 1].getParentNode().insertBefore(commentRangeEnd, runs[end - 1]);

    commentRangeEnd.getParentNode().insertAfter(comment, commentRangeEnd);
}

private static Run splitRun(Run run, int position) throws Exception {
    Run afterRun = (Run) run.deepClone(true);
    afterRun.setText(run.getText().substring(position));
    run.setText(run.getText().substring((0), (0) + (position)));
    run.getParentNode().insertAfter(afterRun, run);
    return afterRun;
}

@awais.hafeez

Thanks for your responses. This scenario is working fine now. But we have multiple paragraph mean it’s not applicable for all paragraphs.

Example :
First Paragraph : Bookmark1
Start & End Range : {406 - 414, 1422 - 1429, 990 - 995, 782 - 790}
Word : {client’s,essence,wants,client’s}

Second Paragraph : Bookmark2
Start & End Range : {259 - 269, 449 - 455}
Word : {desiccator,Bruker}

Third Paragraph : Bookmark3
Start & End Range : {224 - 234}
Word : {water}

Input : input.zip (10.9 KB)

Exp_OP : Exp_OP.zip (13.5 KB)

@ssvel,

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

@ssvel,

Please try using the following code:

Document doc = new Document("D:\\Temp\\input\\input.docx");

Node[] runs = doc.getChildNodes(NodeType.RUN, true).toArray();
for (int i = 0; i < runs.length; i++) {
    Run run = (Run) runs[i];
    int length = run.getText().length();

    Run currentNode = run;
    for (int x = 1; x < length; x++) {
        currentNode = splitRun(currentNode, 1);
    }
}

Node[] paras = doc.getChildNodes(NodeType.PARAGRAPH, true).toArray();

commentRangeOfRuns((Paragraph) paras[1], "Some Text", 406, 414);
commentRangeOfRuns((Paragraph) paras[1], "Some Text", 1422, 1429);
commentRangeOfRuns((Paragraph) paras[1], "Some Text", 990, 995);
commentRangeOfRuns((Paragraph) paras[1], "Some Text", 782, 790);

commentRangeOfRuns((Paragraph) paras[3], "Some Text", 259, 269);
commentRangeOfRuns((Paragraph) paras[3], "Some Text", 449, 455);

commentRangeOfRuns((Paragraph) paras[5], "Some Text", 224, 234);

doc.save("D:\\Temp\\input\\awjava-18.4.docx");

private static void commentRangeOfRuns(Paragraph para, String commentText, int start, int end){
    Document doc = (Document) para.getDocument();

    Comment comment = new Comment(doc, "Awais Hafeez", "AH", new Date());
    comment.getParagraphs().add(new Paragraph(doc));
    //comment.getFirstParagraph().getRuns().add(new Run(doc, commentText));

    CommentRangeStart commentRangeStart = new CommentRangeStart(doc, comment.getId());
    CommentRangeEnd commentRangeEnd = new CommentRangeEnd(doc, comment.getId());

    Node[] runs = para.getChildNodes(NodeType.RUN, true).toArray();

    runs[start - 1].getParentNode().insertBefore(commentRangeStart, runs[start - 1]);
    runs[end - 1].getParentNode().insertBefore(commentRangeEnd, runs[end - 1]);

    commentRangeEnd.getParentNode().insertAfter(comment, commentRangeEnd);
}

private static Run splitRun(Run run, int position) throws Exception {
    Run afterRun = (Run) run.deepClone(true);
    afterRun.setText(run.getText().substring(position));
    run.setText(run.getText().substring((0), (0) + (position)));
    run.getParentNode().insertAfter(afterRun, run);
    return afterRun;
}

@awais.hafeez

Thank you for your response. Its working fine.