DEV Community

CodeSharing
CodeSharing

Posted on

1 1

【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>
Enter fullscreen mode Exit fullscreen mode

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Add Comment

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Reply

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);
        }
}
Enter fullscreen mode Exit fullscreen mode

Delete

API Trace View

Struggling with slow API calls? 🕒

Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more →

Top comments (0)

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook