Word 文档中的批注功能允许使用者对文档的内容进行批注,而不对其产生影响。文档作者可以使用此功能对内容进行注释,而读者也可以使用此功能对内容进行评论或提出疑问。批注功能还允许对已添加的批注进行回复,可以方便使用者对文档进行讨论。本文将介绍如何使用 Spire.Doc for Java 在 Word 文档中添加、回复或删除批注。
安装 Spire.Doc for Java
首先,您需要在 Java 程序中添加 Spire.Doc.jar 文件作为依赖项。您可以从此链接下载 JAR 文件;如果您使用 Maven,则可以通过在 pom.xml 文件中添加以下代码导入 JAR 文件。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>13.3.0</version>
</dependency>
</dependencies>
添加批注到 Word 文档中的段落
添加批注到 Word 文档中的指定段落时,可以直接使用 Paragraph.appendComment() 方法,然后再设置批注者。以下是添加评论到段落的操作步骤:
- 创建 Document 的对象。
- 使用 Document.loadFromFile() 方法载入 Word 文档。
- 使用 Document.getSections().get() 方法获取文档第一节。
- 使用 Section.getParagraphs().get() 方法获取该节第一个段落。
- 使用 Paragraph.appendComment() 方法添加批注到该段落。
- 使用 Comment.getFormat().setAuthor() 方法设置批注者。
- 使用 Document.saveToFile() 方法保存文档。
- Java
import com.spire.doc.Document;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.Comment;
public class addCommentParagraph {
public static void main(String[] args) {
//创建Document的对象
Document document = new Document();
//载入Word文档
document.loadFromFile("生而如是.docx");
//获取文档第一节
Section section = document.getSections().get(0);
//获取第一节的第二个段落
Paragraph paragraph = section.getParagraphs().get(1);
//添加批注到该段落
Comment comment = paragraph.appendComment("这是文章的引入部分。");
comment.getFormat().setAuthor("杨义");
//保存文档
document.saveToFile("添加批注到段落.docx");
}
}
添加批注到 Word 文档中的文本
Spire.Doc for Java 提供的 Comment 类代表 Word 文档中的批注,CommentMark 代表被批注文本的开始或结束标记。在添加批注到 Word 文档中的指定文本时,我们可以创建一个自定义的 insertComment() 方法来添加批注并设置被批注文本的起始位置。以下是操作步骤:
- 创建 Document 的对象。
- 使用 Document.loadFromFile() 方法载入 Word 文档。
- 使用自定义的 insertComment() 方法添加批注到文档中。
- 使用 Document.saveToFile() 方法保存文档。
- Java
import com.spire.doc.*;
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;
import com.spire.doc.fields.TextRange;
public class addComment {
public static void main(String[] args) {
//创建一个Document类的对象
Document document= new Document();
//载入Word文档
document.loadFromFile("生而如是.docx");
//使用自定义的方法添加批注到Word文档
insertComments(document, "气质");
//保存文档
document.saveToFile("添加批注.docx", FileFormat.Docx);
}
private static void insertComments(Document doc, String keystring) {
//查找要批注的文本
TextSelection find = doc.findString(keystring, false, true);
//创建一个Comment类的对象
Comment comment = new Comment(doc);
//设置批注文本
comment.getBody().addParagraph().setText("气质指的是一个人心理活动动力的总和。");
//设置批注者
comment.getFormat().setAuthor("李立");
//获取要批注文本所在的段落
TextRange range = find.getAsOneRange();
Paragraph para = range.getOwnerParagraph();
//添加批注到该段落
para.getChildObjects().add(comment);
//创建批注的开始标记和结束标记
CommentMark commentMarkStart = new CommentMark(doc, comment.getFormat().getCommentId(), CommentMarkType.Comment_Start);
CommentMark commentMarkEnd = new CommentMark(doc, comment.getFormat().getCommentId(), CommentMarkType.Comment_End);
//将开始与结束标记插入到该段落以设置批注的开始与结束位置
int index = para.getChildObjects().indexOf(range);
para.getChildObjects().insert(index, commentMarkStart);
para.getChildObjects().insert(index + 2, commentMarkEnd);
}
}
在 Word 文档中回复批注
回复批注时,我们可以使用 Spire.Doc for Java 提供的 Comment.replyToComment() 来将一个批注设置为另一个批注的回复。以下是详细操作步骤:
- 创建 Document 的对象。
- 使用 Document.loadFromFile() 方法载入 Word 文档。
- 使用 Document.getComments().get() 方法获取文档中的第一个批注。
- 创建一个 Comment 的对象并设置批注者、添加批注文本。
- 使用 Comment.replyToComment() 方法将创建的批注设置为文档第一个批注的回复。
- 使用 Document.saveToFile() 方法保存文档。
- Java
import com.spire.doc.*;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.*;
public class replyToComment {
public static void main(String[] args) {
//创建Document的对象
Document document = new Document();
//载入Word文档
document.loadFromFile("添加批注.docx");
//获取第一个评论
Comment comment = document.getComments().get(0);
//创建Comment的对象并设置批注者、添加批注文本
Comment replyComment = new Comment(document);
replyComment.getFormat().setAuthor("冉林");
Paragraph paragraph = replyComment.getBody().addParagraph();
paragraph.appendText("这是心理学中的概念。");
//将该批注设置为第一个批注的回复
comment.replyToComment(replyComment);
//保存文档
document.saveToFile("回复批注.docx", FileFormat.Docx);
}
}
删除 Word 文档中的批注
我们可以用 Document.getComments().removeAt() 方法来删除指定批注,或使用 Document.getComments().clear() 方法来删除所有批注。以下是删除批注的操作步骤:
- 创建 Document 的对象。
- 使用 Document.loadFromFile() 方法载入 Word 文档。
- 使用 Document.getComments().clear() 方法来删除所有批注或使用 Document.getComments().removeAt() 方法来删除指定批注。
- 使用 Document.saveToFile() 方法保存文档。
- Java
import com.spire.doc.*;
import com.spire.doc.FileFormat;
public class deleteComment {
public static void main(String[] args) {
//创建Document的对象
Document document= new Document();
//载入Word文档
document.loadFromFile("回复批注.docx");
//删除所有批注
document.getComments().clear();
//删除指定批注
//document.getComments().removeAt(1);
//保存文档
document.saveToFile("删除批注.docx", FileFormat.Docx);
}
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。