Adding comments in Word is a great way to mark up your documents without having to directly edit its content, and you can add comments to anything in Word, including text, images, charts, tables, etc. If there are comments on the document already, replying to them lets you have a discussion with others, even when you're not all in the document at the same time.
This article will introduce how to add a comment to the specified paragraph or text in Word, reply to the comment and then delete it using Java codes. It’s worth mentioning that the operation mentioned above need to be done with the help of a third-party library called Free Spire.Doc for Java.
BEFORE CODING
First of all, we need to create a
development environment by installing JDK and Intellij IDEA, and then add a Jar
file called Spire.Doc.jar which is in the library to IDEA. You can get the Jar
file through the link or directly reference it by using the following Maven
configuration.
<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.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
USING THE CODE
Add a comment to a specified paragraph in Word
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.Comment;
public class AddCommentToParagraph {
public static void main(String[] args) {
//load a word document
Document document= new Document("C:\\Users\\Test1\\Desktop\\Sample.docx");
//Get the fifth paragraph of first section
Section section = document.getSections().get(0);
Paragraph paragraph = section.getParagraphs().get(4);
//Insert a new comment to the paragraph
Comment comment = paragraph.appendComment("The definition of Cashless Society");
comment.getFormat().setAuthor("Tina");
comment.getFormat().setInitial("CM");
//save the file
document.saveToFile("output/AddCommentToParagraph.docx", FileFormat.Docx);
}
}Output
Add a comment to specified text in Wordimport 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 AddCommentToText {
public static void main(String[] args) {
//Load a Word document
Document doc = new Document();
doc.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.docx");
//Find the text string in the document
TextSelection[] finds = doc.findAllString("a cashless society is one in which financial transactions " +
"are handled through the transfer of digital information instead of physical money in the form of banknotes " +
"or coins", false, true);
for (TextSelection find : finds) {
//Create comment start mark and comment end mark
CommentMark commentmarkStart = new CommentMark(doc);
commentmarkStart.setType(CommentMarkType.Comment_Start);
CommentMark commentmarkEnd = new CommentMark(doc);
commentmarkEnd.setType(CommentMarkType.Comment_End);
//Create a comment
Comment comment = new Comment(doc);
comment.getBody().addParagraph().setText("The definition of Cashless Society");
comment.getFormat().setAuthor("Tina");
//Find the paragraph where the string is located in
Paragraph para = find.getAsOneRange().getOwnerParagraph();
//Get the index of the string in the paragraph
int index = para.getChildObjects().indexOf(find.getAsOneRange());
//Add the comment to the paragraph
para.getChildObjects().add(comment);
//Insert the comment start mark and comment end mark to the paragraph based on the index
para.getChildObjects().insert(index, commentmarkStart);
para.getChildObjects().insert(index + 2, commentmarkEnd);
}
//Save the resulting document
doc.saveToFile("output/AddCommentToText.docx",FileFormat.Docx_2013);
}
}Output
Reply to a comment in Word
import com.spire.doc.*;
import com.spire.doc.FileFormat;
import com.spire.doc.fields.*;
public class ReplyToComment {
public static void main(String[] args) {
//load a word document
Document document= new Document("C:\\Users\\Test1\\Desktop\\AddCommentToParagraph.docx");
//Get the first comment
Comment comment1 = document.getComments().get(0);
//add the new comment as a reply to the selected comment.
Comment replyComment1 = new Comment(document);
replyComment1.getFormat().setAuthor("Emma");
replyComment1.getBody().addParagraph().appendText("You never need to use paper bills or coins in a Cashless Society");
comment1.replyToComment(replyComment1);
//save the file
document.saveToFile("output/ReplyToComment.docx", FileFormat.Docx);
}
}Output
Delete a specified comment in Wordimport com.spire.doc.*;
import com.spire.doc.FileFormat;
public class DeleteComment {
public static void main(String[] args) {
//load a word document
Document document= new Document("C:\\Users\\Test1\\Desktop\\ReplyToComment.docx");
//remove the first comment
document.getComments().removeAt(0);
//save the file.
document.saveToFile("output/DeleteComment.docx", FileFormat.Docx);
}
}Output
No comments:
Post a Comment