Add, Reply to and Delete Comments in Word in Java
Tags: java, worddocument, comment
Published: 2020-02-04 09:32:12

---
title: Add, Reply to and Delete Comments in Word in Java
published: true
description: This article demonstrates how to add, reply to and delete comments in Word document in Java.
tags: Java, Word document, Comment
---

A comment is a note or annotation that an author or reviewer can add to a document. In this article, we will show you how to add, reply to and delete comments in Word document in Java using [Free Spire.Doc for Java library](https://www.e-iceblue.com/Introduce/free-doc-for-java.html).

Before getting started, please download Free Spire.Doc for Java package through [this link](https://www.e-iceblue.com/Download/doc-for-java-free.html), unzip the package and then import Spire.Doc.jar from lib folder into our application.

##Add a comment##
We can add a comment to a Word document by using the **appendComment** method of **Paragraph** class.

The following code shows how to add a comment to a Word document.
```java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.Comment;

public class AddComments {
    public static void main(String[] args){
        //Load a Word document
        Document document= new Document();
        document.loadFromFile("input.docx");

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

        //Add a comment to the paragraph
        Comment comment = paragraph.appendComment("This is a comment");
        //Specify the author of the comment
        comment.getFormat().setAuthor("Shaun");
        //Specify the initial of the author
        comment.getFormat().setInitial("St");

        //Save the document
        document.saveToFile("AddComments.docx", FileFormat.Docx);
    }
}
```
Output:
![Add Comments](https://dev-to-uploads.s3.amazonaws.com/i/vzf926fv5xdw9k5jjbh1.png)
 
##Reply to a comment##
We can reply to an existing comment in Word document by using the **replyToComment** method of **Comment** class.

The following code shows how to rely to an existing comment in a Word document.
```java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.fields.Comment;

public class Comments {
    public static void main(String[] args){
        //Load a Word document
        Document document= new Document();
        document.loadFromFile("AddComments.docx");

        //Get the first comment
        Comment comment1 = document.getComments().get(0);

        //Create a new comment
        Comment replyComment1 = new Comment(document);
        replyComment1.getFormat().setAuthor("Chris");
        replyComment1.getBody().addParagraph().appendText("Hi, thank you for this information!");
        //Add the new comment as a reply to the first comment
        comment1.replyToComment(replyComment1);

        //Save the document
        document.saveToFile("ReplyToComments.docx", FileFormat.Docx);
    }
}
```
Output:
![Reply To Comments](https://dev-to-uploads.s3.amazonaws.com/i/chn3yl2dk25wzq8hcg9d.png)
 
##Delete comments##
We can either delete all the comments or a specified comment from a Word document.

The following code shows how to delete all the comments from a Word document by using the **clear** method of **CommentsCollection** class.
```java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class DeleteComments {
    public static void main(String[] args){

        //Load a Word document
        Document document= new Document();
        document.loadFromFile("AddComments.docx");


        //Delete all the comments
        document.getComments().clear();


        //Save the document
        document.saveToFile("DeleteAllComments.docx", FileFormat.Docx);
    }
}
```

The following code shows how to delete a specified comment from a Word document by using the **removeAt** method of **CommentsCollection** class.
```java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class DeleteComments {
    public static void main(String[] args){
        //Load a Word document
        Document document= new Document();
        document.loadFromFile("AddComments.docx");

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

        //Save the document
        document.saveToFile("DeleteSpecifiedComment.docx", FileFormat.Docx);
    }
}
```