Java 给Word指定字符串添加批注

 时间:2026-02-16 02:55:00

1、方法1:通过e-iceblue官网下载。下载后,解压文件,并将lib文件夹下的Spire.Doc.jar文件导入到java程序。参考如下导入效果:

2、方法2:通过maven导入。可以参考导入教程https://www.e-iceblue.cn/licensing/install-spirepdf-for-java-from-maven-repository.html

1、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 AddCommentToCharacters {
    public static void main(String[] args) {
        //加载测试文档
        Document doc = new Document();
        doc.loadFromFile("test.docx");

        //查找指定字符串
        TextSelection[] selections = doc.findAllString("皱状厚膜", true, false);

        //获取关键字符串所在段落
        Paragraph para = selections[0].getAsOneRange().getOwnerParagraph();
        int index = para.getChildObjects().indexOf(selections[0].getAsOneRange());

        //设置批注ID
        CommentMark start = new CommentMark(doc);
        start.setCommentId(1);
        start.setType(CommentMarkType.Comment_Start);
        CommentMark end = new CommentMark(doc);
        end.setType(CommentMarkType.Comment_End);
        end.setCommentId(1);

        //添加批注内容
        String str = "给指定字符串添加批注";
        Comment comment = new Comment(doc);
        comment.getFormat().setCommentId(1);
        comment.getBody().addParagraph().appendText(str);
        comment.getFormat().setAuthor("作者:");
        comment.getFormat().setInitial("CM");
        para.getChildObjects().insert(index, start);
        para.getChildObjects().insert(index + 1, selections[0].getAsOneRange());
        para.getChildObjects().insert(index + 2,end);
        para.getChildObjects().insert(index + 3, comment);

        //保存文档
        doc.saveToFile("字符串批注.docx",FileFormat.Docx_2013);
        doc.dispose();
    }
}

2、批注添加效果:

Java 给Word指定字符串添加批注

  • RedHat上面搭建gcc
  • freeftpd安装及配置
  • 数据报表FineReport中分组报表按段分组
  • 填报报表FineReport中数据列过滤属性如何设置
  • 数据报表FineReport中分组报表自定义公式分组
  • 热门搜索
    素材大全 怎么清除手机内存 披萨怎么做视频 南阳旅游景点大全简介 头灯大全 怎么查手机通话记录 咖喱粉怎么用 东北大秧歌曲大全 宝宝小游戏大全 怎么练三角肌