I am trying to insert comments to a text in a docx file.
But my comments will be like paragraphs which I am storing in array and then passing those array elements one by one in an iteration.
Below is my code snippet
- Code: Select all
protected void createGrammarCheckedDocument(ArrayList<String> textToBeProcessed, S3ObjectInputStream objectInputStream, ArrayList<String> textToBeReplaced, Job job) {
System.out.println("Text to be processed ===> " + textToBeProcessed.size());
System.out.println("Text to be replaced ==> " + textToBeReplaced);
Document documentToBeCreated = new Document(objectInputStream);
try {
for(int i = 0 ; i < textToBeProcessed.size(); i++) {
for (int j = 0; j < textToBeReplaced.size(); j++) {
//Find the specific string to add comment
TextSelection textSelection = documentToBeCreated.findString(textToBeProcessed.get(i), false, true);
System.out.println("String in line ==> " + textSelection);
//Create a start mark
CommentMark commentMarkStart = new CommentMark(documentToBeCreated);
commentMarkStart.setType(CommentMarkType.Comment_Start);
//Create an end mark
CommentMark commentMarkEnd = new CommentMark(documentToBeCreated);
commentMarkEnd.setType(CommentMarkType.Comment_End);
//Create a comment
Comment comment = new Comment(documentToBeCreated);
comment.getBody().addParagraph().setText(textToBeReplaced.get(j));
comment.getFormat().setAuthor("PaperTrue");
//Find the paragraph where the string is located
Paragraph paragraph = textSelection.getAsOneRange().getOwnerParagraph();
//Get the index of the string in the paragraph
int index = paragraph.getChildObjects().indexOf(textSelection.getAsOneRange());
//Add the comment to the paragraph
paragraph.getChildObjects().add(comment);
paragraph.getChildObjects().insert(index, commentMarkStart);
paragraph.getChildObjects().insert(index + 2, commentMarkEnd);
}
}
} catch (Exception e) {
e.printStackTrace();
}
documentToBeCreated.saveToFile(commonFileSavingProcess("#" + job.getJobNo() + " GrammarChecked.docx", job));
}
The comments are generated, but the created file is not in proper format
below are the snapshots of my output file,