在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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值