This article describes how to annotate a specified string in a Word document in a Java program. Preceding text In, it mainly introduces the methods of adding comments for a paragraph, replying, editing and deleting comments. If you need to set comments for specific keywords or specified strings, you can refer to the methods in this article.
Using tool: free flame.doc for Java (free version)
Access method 1: through the official website download , and import the Spire.Doc.jar file to the java program.
Get method 2: Pass maven Warehouse installation import. May refer to Method course.
Java code example
import com.spire.doc.*; import com.spire.doc.documents.CommentMark; import com.spire.doc.documents.CommentMarkType; import com.spire.doc.documents.Paragraph; import com.spire.doc.documents.TextSelection; import com.spire.doc.fields.Comment; public class AddCommentToCharacters { public static void main(String[] args) { //Load test document Document doc = new Document(); doc.loadFromFile("test.docx"); //Find the specified string TextSelection[] selections = doc.findAllString("Thick thick film", true, false); //Get the paragraph of the key string Paragraph para = selections[0].getAsOneRange().getOwnerParagraph(); int index = para.getChildObjects().indexOf(selections[0].getAsOneRange()); //Add annotations ID CommentMark start = new CommentMark(doc); start.setCommentId(1); start.setType(CommentMarkType.Comment_Start); CommentMark end = new CommentMark(doc); end.setType(CommentMarkType.Comment_End); end.setCommentId(1); //Add comments String str = "Annotate the specified string"; Comment comment = new Comment(doc); comment.getFormat().setCommentId(1); comment.getBody().addParagraph().appendText(str); comment.getFormat().setAuthor("Author:"); comment.getFormat().setInitial("CM"); para.getChildObjects().insert(index, start); para.getChildObjects().insert(index + 1, selections[0].getAsOneRange()); para.getChildObjects().insert(index + 2,end); para.getChildObjects().insert(index + 3, comment); //Save document doc.saveToFile("String annotation.docx",FileFormat.Docx_2013); doc.dispose(); } }
Comment adding effect:
(end of this paper)