zoukankan      html  css  js  c++  java
  • 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();
        }
    }

    批注添加效果:

    (本文完)

  • 相关阅读:
    三行代码搞定微信登录集成
    iptables命令
    Linux(centos)系统各个目录的作用详解 推荐
    Linux下Apache服务的查看和启动
    Linux使用退格键时出现^H ^?解决方法
    小程序:最难点For的wx:key
    linux 通过wol远程开机【转】
    linux wake on lan功能通过ethtool配置【转】
    设计模式小议:state【转】
    TCP/IP详解学习笔记(4)-ICMP协议,ping和Traceroute【转】
  • 原文地址:https://www.cnblogs.com/Yesi/p/11805744.html
Copyright © 2011-2022 走看看