class="MsoNormal">在word中,批注是一种常用于对特定文档内容进行注解的方法,起到解释说明、标记指正的作用。本篇文章中,将通过Java程序来演示如何来操作批注,内容要点包括:
1. 添加批注(添加文本、图片到批注)
2. 回复批注
3. 删除批注
?
使用工具:Free Spire.Doc for Java 2.0.0(免费版)
Jar文件导入:
方法1:从官网下载jar包。
导入步骤: 在程序下新建一个directory目录,并命名(本示例中命名为lib);将控件包lib文件夹下的jar(如下图1)复制到程序中新建的目录下。复制jar文件后,鼠标右键点击jar文件,选择”Add as Library”。完成导入(如下图2)。
?
?
图1:
?
图2:
?
方法2:通过maven导入。参考导入方法。
?
Java代码示例
?
【示例1】添加Word批注
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.Comment;
public class AddComment {
public static void main(String[] args) {
//加载测试文档
Document doc = new Document("test.docx");
//获取指定段落
Section sec = doc.getSections().get(0);
Paragraph para= sec.getParagraphs().get(3);
//插入文本到批注
Comment comment = para.appendComment("请在试验中将包含以下特征的实验样本记录在册,并整理好周记录报表,供后续观察取样。");
comment.getFormat().setAuthor("审校组");
//插入图片到批注
comment.getBody().addParagraph().appendPicture("tp.png");
//保存文档
doc.saveToFile("AddComment.docx", FileFormat.Docx_2010);
}
}
?
批注添加效果:
?
【示例2】回复Word批注
import com.spire.doc.*;
import com.spire.doc.fields.Comment;
public class ReplyComment {
public static void main(String[] args) throws Exception{
//加载测试文档
Document doc = new Document("AddComment.docx");
//获取指定批注
Comment comment = doc.getComments().get(0);
//回复批注
Comment relyC= new Comment(doc);
relyC.getFormat().setAuthor("实验组");
relyC.getBody().addParagraph().appendText("已完成。");
comment.replyToComment(relyC);
//保存文档
doc.saveToFile("ReplyComment.docx",FileFormat.Docx_2010);
}
}
?
批注回复结果:
【示例3】删除Word批注
import com.spire.doc.*;
import com.spire.doc.FileFormat;
public class DeleteComment{
public static void main(String[] args) {
//加载测试文档
Document doc = new Document("AddComment.docx");
//调用方法删除指定批注
doc.getComments().removeAt(0);
//保存文档
doc.saveToFile("DeleteComment",FileFormat.Docx_2010);
}
}
?
运行程序后,批注已被删除。
?
?
(本文完)
大小: 56.3 KB
大小: 31.1 KB
大小: 151.4 KB
大小: 154 KB
查看图片附件