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.
Before getting started, please download Free Spire.Doc for Java package through this link, 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.
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);
}
}
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.
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);
}
}
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.
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.
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);
}
}
Top comments (0)