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.
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;
}