在Java中的Word中添加,回复和删除注释

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

我们可以使用来向Word文档添加注释appendComment的方法段类。

下面的代码显示如何向Word文档添加注释。

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

Reply to a comment

我们可以使用以下命令回复Word文档中的现有注释:ReplyTo评论的方法评论类。

下面的代码显示如何答复Word文档中的现有注释。

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

Delete comments

我们可以从Word文档中删除所有注释或指定注释。

以下代码显示了如何通过使用以下命令从Word文档中删除所有注释:明确的方法评论集类。

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);
    }
}

以下代码显示了如何通过使用以下命令从Word文档中删除指定的注释:removeAt的方法评论集类。

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);
    }
}

from: https://dev.to//eiceblue/add-reply-to-and-delete-comments-in-word-in-java-1ndo

发布了0 篇原创文章 · 获赞 0 · 访问量 629

猜你喜欢

转载自blog.csdn.net/cunxiedian8614/article/details/105691270