专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

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

本文将介绍在Java程序中如何给Word文档中的指定字符串添加批注。前文中,主要介绍的是针对某个段落来添加批注,以及回复、编辑、删除批注的方法,如果需要针对特定关键词或指定字符串来设置批注,可以参考本文的方法。

使用工具:Free Spire.Doc for Java (免费版)

获取方法****1****:通过官网下载,并导入Spire.Doc.jar文件至java程序。

获取方法****2****:通过maven仓库安装导入。可参考方法教程

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;

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();
    }
}

批注添加效果:

77_1.png

(本文完)

文章永久链接:https://tech.souyunku.com/48191

未经允许不得转载:搜云库技术团队 » Java 给Word指定字符串添加批注

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们