Comment (4)
Comments in Word documents often hold valuable information, such as feedback, suggestions, and notes. Unfortunately, editors like Microsoft Word lack a built-in feature for batch-extracting comments, leaving users to rely on cumbersome methods like copying and pasting or using VBA macros. To simplify this process, this article demonstrates how to use Java to extract comments from Word documents with Spire.Doc for Java. With a streamlined approach, you can easily retrieve all comment text and images in a single operation—quickly, efficiently, and error-free. Let's explore how it’s done.
- Extract Comments Text from Word Documents in Java
- Extract Comment Images from Word Documents in 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>13.11.2</version>
</dependency>
</dependencies>
Extract Comments Text from Word Documents in Java
Using Java to extract all comment text is easy and quick. Firstly, loop through all comments in the Word file and get the current comment using the Document.getComments().get() method offered by Spire.Doc for Java. Then iterate through all paragraphs in the comment body and get the current paragraph. Finally, text from comment paragraphs will be extracted using the Paragraph.getText() method. Let's dive into the detailed steps.
Steps to extract comment text from Word files:
- Create an object of Document class.
- Load a Word document from files using Document.loadFromFile() method.
- Iterate through all comments in the Word file.
- Get the current comment with Document.getComments().get() method.
- Loop through paragraphs in the comment and access the current paragraph through Comment.getBody().getParagraphs().get() method.
- Extract the text of the paragraphs in comments by calling Paragraph.getText() method.
- Get the current comment with Document.getComments().get() method.
- Save the extracted comments.
The code example below demonstrates how to extract all comment text from a Word document:
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.io.*;
public class ExtractComments {
public static void main(String[] args) throws IOException {
// Create a new Document instance
Document doc = new Document();
// Load the document from the specified input file
doc.loadFromFile("/comments.docx");
// Iterate over each comment in the document
for (int i = 0; i < doc.getComments().getCount(); i++) {
// Get the comment at the current index
Comment comment = doc.getComments().get(i);
// Iterate over each paragraph in the comment's body
for (int j = 0; j < comment.getBody().getParagraphs().getCount(); j++) {
// Get the paragraph at the current index
Paragraph para = comment.getBody().getParagraphs().get(j);
// Get the text of the paragraph and append a line break
String result = para.getText() + "\r\n";
// Write the extracted comment a text file
writeStringToTxt(result, "/commenttext.txt");
}
}
// Dispose of the document resources
doc.dispose();
}
// Custom method to write a string to a text file
public static void writeStringToTxt(String content, String txtFileName) throws IOException {
FileWriter fWriter = new FileWriter(txtFileName, true);
try {
// Write the content to the text file
fWriter.write(content);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
// Flush and close the FileWriter
fWriter.flush();
fWriter.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}

Extract Comments Images from Word Documents with Java
Sometimes, comments in a document may contain not only text but also images. With the methods provided by Spire.Doc for Java, you can easily extract all images from comments in bulk. The process is similar to extracting text: you need to iterate through each comment, the paragraphs in the comment body, and the child objects of each paragraph. Then, check if the object is a DocPicture. If it is, use the DocPicture.getImageBytes() method to extract the image.
Steps to extract comment images from Word documents:
- Create an instance of Document class.
- Specify the file path to load a source Word file through Document.loadFromFile() method.
- Create a list to store extracted data.
- Loop through comments in the Word file and get the current comment using Document.getComments().get() method.
- Loop through all paragraphs in a comment, and get the current paragraph with Comment.getBody().getParagraphs().get() method.
- Iterate through each child object of a paragraph, and access a child object through Paragraph.getChildObjects().get() method.
- Check if the child object is DocPicture, if it is, get the image data using DocPicture.getImageBytes() method.
- Loop through all paragraphs in a comment, and get the current paragraph with Comment.getBody().getParagraphs().get() method.
- Add the image data to the list and save it as image files.
Here is the code example of extracting all comment images from a Word file:
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.io.*;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;
public class ExtractCommentImages {
public static void main(String[] args) {
// Create an object of the Document class
Document document = new Document();
// Load a Word document with comments
document.loadFromFile("/comments.docx");
// Create a list to store the extracted image data
List<byte[]> images = new ArrayList<>();
// Loop through the comments in the document
for (int i = 0; i < document.getComments().getCount(); i++) {
Comment comment = document.getComments().get(i);
// Iterate through the paragraphs in the comment body
for (int j = 0; j < comment.getBody().getParagraphs().getCount(); j++) {
Paragraph paragraph = comment.getBody().getParagraphs().get(j);
// Loop through the child objects in the paragraph
for (int k = 0; k < paragraph.getChildObjects().getCount(); k++) {
DocumentObject obj = paragraph.getChildObjects().get(k);
// Check if it is a picture
if (obj instanceof DocPicture) {
DocPicture picture = (DocPicture) obj;
// Get the image date and add it to the list
images.add(picture.getImageBytes());
}
}
}
}
// Specify the output file path
String outputDir = "/comment_images/";
new File(outputDir).mkdirs();
// Save the image data as image files
for (int i = 0; i < images.size(); i++) {
String fileName = String.format("comment-image-%d.png", i);
Path filePath = Paths.get(outputDir, fileName);
try (FileOutputStream fos = new FileOutputStream(filePath.toFile())) {
fos.write(images.get(i));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

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 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>13.11.2</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>13.11.2</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>13.11.2</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.