批注是一种常用于对特定文档内容进行注解的工具或方法,起到解释说明、标记指正的作用。在本篇文章中,将介绍如何操作Word批注的方法,包括:
1. 添加批注:添加文本到批注、插入图片到批注;
1.1 给指定段落添加批注
1.2 给指定字符串添加批注
2. 回复批注;
3. 修改或替换批注:用文本替换批注中的文本内容、用文本替换批注中的图片、用图片替换批注中的图片;
4. 删除批注:删除指定批注中的所有内容、删除指定批注中的指定内容
5. 读取批注
5.1 读取批注中的文本
5.2 读取批注中的图片
使用工具:Free Spire.Doc for Java (免费版)
Jar文件获取及导入:
方法1:通过官网获取jar包,并解压。解压后,将lib文件夹下的Spire.Doc.jar文件导入java程序。
方法2:通过添加maven依赖导入到maven项目,参考导入步骤。
Java示例代码
【示例1】给段落添加批注(文本、图片)
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);
}
}
批注添加效果:
![5c9e108909755dd23565245d11c05b7f.png](https://img-blog.csdnimg.cn/img_convert/5c9e108909755dd23565245d11c05b7f.png)
【示例2】给指定字符串添加批注
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;
public class AddComment