A comment is a note or annotation that an author or reviewer can add to a document. In this article, I will illustrate how to add comment to specific text and get the marked text of a specific comment in a Word document using Java.
Add Dependencies
Free Spire.Doc for Java library are used to achieve the functionality. There are two ways to include Free Spire.Doc for Java in your Java project:
For maven projects, add the following dependencies to your project’s pom.xml file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>http: //repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId> e-iceblue </groupId> <artifactId>spire.doc.free</artifactId> <version> 3.9 . 0 </version> </dependency> </dependencies> |
The latest version of Free Spire.Doc for Java is 3.9.0 (at the time of writing this article).
For non-maven projects, download Free Spire.Doc for Java pack from here: Download- Free Spire.Doc for Java, extract the zip file, then add Spire.Doc.jar in the lib folder into your project as a dependency.
Add comment to specific text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | import com.spire.doc.*; import com.spire.doc.documents.*; import com.spire.doc.fields.*; public class AddCommentToSpecificText { public static void main(String[] args){ //Create word document Document document = new Document(); //Load the document from disk document.loadFromFile( "Template.docx" ); //Call insertComments method to add comment to the specific text "Dragon Boat Festival" insertComments(document, "Dragon Boat Festival" ); //Save the document document.saveToFile( "addCommentToSpecificText.docx" , FileFormat.Docx_2013); } private static void insertComments(Document doc, String stringToComment) { //Find the string TextSelection find = doc.findString(stringToComment, false , true ); //Create comment start mark and comment end mark CommentMark commentMarkStart = new CommentMark(doc); commentMarkStart.setType(CommentMarkType.Comment_Start); CommentMark commentMarkEnd = new CommentMark(doc); commentMarkEnd.setType(CommentMarkType.Comment_End); //Add content for comment Comment comment = new Comment(doc); comment.getBody().addParagraph().setText( "Also known as Duanwu festival" ); //Set the author of comment comment.getFormat().setAuthor( "Sierra" ); //Get the textRange TextRange range = find.getAsOneRange(); //Get its paragraph Paragraph para = range.getOwnerParagraph(); //Get the index of textRange int index = para.getChildObjects().indexOf(range); //Add comment to the paragraph para.getChildObjects().add(comment); //Insert the comment start mark and comment end mark to the paragraph para.getChildObjects().insert(index, commentMarkStart); para.getChildObjects().insert(index + 2 , commentMarkEnd); } } |
Output:
Get the marked text of a specific comment
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import com.spire.doc.Document; import com.spire.doc.documents.CommentMark; import com.spire.doc.documents.Paragraph; import com.spire.doc.fields.Comment; import com.spire.doc.fields.TextRange; public class GetMarkedTextOfSpecificComment { public static void main(String[] args){ //Load the Word document Document doc = new Document(); doc.loadFromFile( "addCommentToSpecificText.docx" ); //Get the first comment in the document Comment comment = doc.getComments().get( 0 ); //get the start mark and end mark of the comment Paragraph para = comment.getOwnerParagraph(); CommentMark start = comment.getCommentMarkStart(); CommentMark end = comment.getCommentMarkEnd(); int indexOfStart = para.getChildObjects().indexOf(start); int indexOfEnd = para.getChildObjects().indexOf(end); String markedText = "" ; //Get the marked text between the comment start mark and comment end mark for ( int i = indexOfStart + 1 ; i < indexOfEnd; i++) { if (para.getChildObjects().get(i) instanceof TextRange) { TextRange range = (TextRange) para.getChildObjects().get(i); markedText += range.getText(); } } //Print out the marked text System.out.println(markedText); } } |
Output:
Comments
Post a Comment