Comment (3)
Comments are used to provide additional information or draw attention to something in the document. Sometimes, the text being commented is also useful, and you may want to extract it for other purposes. In this article, you will learn how to extract the text between two comment marks in a Word document using Spire.Doc for Java.
Install Spire.Doc for Java
First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>12.4.6</version> </dependency> </dependencies>
Get Text Between Two Comment Marks in Word
To get the text between the start mark (represented by CommentMarkStart class) and the end mark (represented by CommentMarkEnd class), you’ll need to get the indexes of the comment marks. These indexes specify the position of the text being marked in a paragraph. The following are the detailed steps to get text inside two comment marks in a Word document.
- Create a Document object, and load a sample Word document using Document.loadFromFile() method.
- Get the first comment using Document.getComments().get() method.
- Get the start mark and end mark of the comment.
- Get the start mark's index and the end mark's index in the owner paragraph.
- Get the text range between the indexes, and then get the text of the text range using TextRage.getText() method.
- Java
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 GetTextInsideCommentMarkers { public static void main(String[] args) { //Create a Document object Document doc = new Document(); //Load the sample Word document doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.docx"); //Get the first comment 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(); //Get the start mark’s index and the end mark’s index respectively int indexOfStart = para.getChildObjects().indexOf(start); int indexOfEnd = para.getChildObjects().indexOf(end); //Declare a String variable String textMarked = ""; //Loop through the numbers between two indexes for (int i = indexOfStart + 1; i < indexOfEnd; i++) { if (para.getChildObjects().get(i) instanceof TextRange) { //Get the text range specified by the index TextRange range = (TextRange) para.getChildObjects().get(i); //Get text from the text range textMarked += range.getText(); } } //Print out the text being marked System.out.println(textMarked); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Comments can be added to almost every element in Word, such as text, images, charts, tables, etc. This article focuses on how to add comments to selected text (phrase) in a Word document using Spire.Doc for Java.
Install Spire.Doc for Java
First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project’s pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>12.4.6</version> </dependency> </dependencies>
Add a Comment to a Specific Phrase
The Paragraph.appendComment() method is used to add comments to an entire paragraph. By default, the comment mark will be placed at the end of the paragraph. To add a comment to a specific phrase, you need to place comment marks (represented by the CommentMark class) at the beginning and end of the text range. The following are the steps to add a comment to a specific phrase.
- Create a Document object, and load the sample Word file using Document.loadFromFile() method.
- Find the specific string from the document using Document.findAllString() method.
- Create a comment start mark and a comment end mark, which will be placed at the beginning and end of the selected phrase respectively.
- Create a Comment object, specifying content and author.
- Get the oner paragraph of the selected phrase, and add the comment to the paragraph as a child object.
- Save the document using Document.saveToFile() method.
- Java
import com.spire.doc.Document; import com.spire.doc.FileFormat; 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 AddCommentToSpecificText { public static void main(String[] args) { //Create a Document object Document doc = new Document(); //Load the sample Word file doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx"); //Find the specific string to add comment TextSelection[] finds = doc.findAllString("Spire.Doc for Java", false, true); TextSelection specificText = finds[0]; //Create a start mark CommentMark commentmarkStart = new CommentMark(doc); commentmarkStart.setType(CommentMarkType.Comment_Start); //Create an end mark CommentMark commentmarkEnd = new CommentMark(doc); commentmarkEnd.setType(CommentMarkType.Comment_End); //Create a comment Comment comment = new Comment(doc); comment.getBody().addParagraph().setText("Developed by E-iceblue"); comment.getFormat().setAuthor("Shaun"); //Find the paragraph where the string is located Paragraph para = specificText.getAsOneRange().getOwnerParagraph(); //Get the index of the string in the paragraph int index = para.getChildObjects().indexOf(specificText.getAsOneRange()); //Add the comment to the paragraph para.getChildObjects().add(comment); //Insert the start mark and end mark to the paragraph based on the index para.getChildObjects().insert(index, commentmarkStart); para.getChildObjects().insert(index + 2, commentmarkEnd); //Save to file doc.saveToFile("AddComment.docx", FileFormat.Docx_2013); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
MS Word Comments feature allows authors to discuss the content with the readers without having to directly edit the content. It is a good way to keep the document neat and clean. In this article, you will learn how to add, delete, or reply to comments in a Word document using Spire.Doc for Java.
Install Spire.Doc for Java
First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project’s pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>12.4.6</version> </dependency> </dependencies>
Add a Comment to Word
Spire.Doc offers the Paragraph.appendComment() method to a paragraph. The detailed steps are as follows.
- Load the sample Word document while initializing the Document object.
- Get the specific section using Document.getSection() method.
- Get the paragraphs collection using Section.getParagraphs() method, and then get the specific paragraph using ParagraphCollection.get() method.
- Add a comment to the paragraph using Paragraph.appendComment() method.
- Save the document using Document.saveToFile() method.
- Java
import com.spire.doc.*; import com.spire.doc.documents.Paragraph; import com.spire.doc.fields.Comment; public class AddComment { public static void main(String[] args) { //Load the sample Word document Document document= new Document("C:\\Users\\Administrator\\Desktop\\Sample.docx"); //Get the first section Section section = document.getSections().get(0); //Get the second paragraph Paragraph paragraph = section.getParagraphs().get(1); //Add a comment Comment comment = paragraph.appendComment("Spire.Doc for Java"); comment.getFormat().setAuthor("E-iceblue"); comment.getFormat().setInitial("CM"); //Save to file document.saveToFile("output/AddComment.docx", FileFormat.Docx); } }
Reply to a Comment
To add a reply to an existing comment, use Comment.replyToComment() method. The following are the detailed steps.
- Load the sample Word document while initializing the Document object.
- Get the comment collection using Document.getComments() method, and then get the specific comment using CommentsCollection.get() method.
- Create a new instance of Comment class, and specify its content and author.
- Add the comment as a reply to an existing comment using Comment.replyToComment() method.
- Save the document using Document.saveToFile() method.
- Java
import com.spire.doc.*; import com.spire.doc.FileFormat; import com.spire.doc.fields.*; public class ReplyToComment { public static void main(String[] args) { //Load the sample Word document Document document= new Document("C:\\Users\\Administrator\\Desktop\\Sample.docx"); //Get the first comment Comment comment1 = document.getComments().get(0); //Initialize a Comment object Comment replyComment = new Comment(document); //Set the author and content of the comment replyComment.getFormat().setAuthor("E-iceblue"); replyComment.getBody().addParagraph().appendText("Spire.Doc for Java is a professional Word Java library on operating Word documents."); //Add the new comment as a reply to the first comment comment1.replyToComment(replyComment); //Save to file document.saveToFile("output/ReplyToComment.docx", FileFormat.Docx); }
Delete a Comment from Word
After getting the comments collection through Document.getComments() method, you can use CommentsCollection.removeAt() method to delete the specific comment by its index. Here come the detailed steps.
- Load the sample Word document while initializing the Document object.
- Get the comments collection using Document.getComments() method.
- Remove the specific comment using CommentsCollection.removeAt() method.
- Save the document using Document.saveToFile() method.
- Java
import com.spire.doc.*; import com.spire.doc.FileFormat; public class DeleteComment { public static void main(String[] args) { //Load the sample Word document Document document= new Document("C:\\Users\\Administrator\\Desktop\\Sample.docx"); //Remove the comment by its index document.getComments().removeAt(1); //Save to file document.saveToFile("output/DeleteComment.docx", FileFormat.Docx); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.