Java: Operate the Comment in Word Document (Add/Delete/Reply)

Comments in Word documents usually contain some suggestions or reviews. Comments are very useful, and adding comments will not affect or modify the original content of the document. This article is going to introduce how to use Free Spire.Doc for Java to add a comment to Word document, insert a comment as a reply to a selected comment, and delete the existing comment from Word document.

Installation
Method 1: Download the Free Spire.Doc for Java and unzip it. Then add the Spire.Doc.jar file to your Java application as dependency.

Method 2: Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.

<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>

Add a Comment to Word Document

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.Comment;


public class WordComment {
public static void main(String[] args) throws Exception{

String inputFile="C:\\Users\\Administrator\\Desktop\\Moon.docx";
String outputFile="Comment.docx";

//Load a word document
Document document= new Document(inputFile);

//Get the second paragraph
Section section = document.getSections().get(0);
Paragraph paragraph = section.getParagraphs().get(1);

//Insert a new comment
Comment comment = paragraph.appendComment("This sentence should be removed");
comment.getFormat().setAuthor("Paul");
comment.getFormat().setInitial("CM");

//Save the file
document.saveToFile(outputFile, FileFormat.Docx);
}
}

Reply to a Comment

import com.spire.doc.*;
import com.spire.doc.FileFormat;
import com.spire.doc.fields.*;

public class ReplyComment {
public static void main(String[] args) throws Exception{

String inputFile="Comment.docx";
String outputFile="ReplaytoComment.docx";

//Load a word document
Document document= new Document(inputFile);

//Get the first comment and reply to it
Comment comment1 = document.getComments().get(0);
Comment replyComment1 = new Comment(document);

replyComment1.getFormat().setAuthor("Steve");
replyComment1.getBody().addParagraph().appendText("Yes, the content will be updated as suggested.");

//Add the new comment as a reply to the selected comment.
comment1.replyToComment(replyComment1);
//Save the file
document.saveToFile(outputFile, FileFormat.Docx);
}
}

Delete Comment

import com.spire.doc.*;
import com.spire.doc.FileFormat;

public class DeleteComment{
public static void main(String[] args) throws Exception{

String inputFile="Comment.docx";
String outputFile="DeleteComment.docx";

//Load a word document
Document document= new Document(inputFile);

//Remove the first comment
document.getComments().removeAt(0);

//Save the file.
document.saveToFile(outputFile, FileFormat.Docx);
}
}

Sharing Java Code