Java怎么添加、回复、修改、删除Word批注

发布时间:2021-09-17 16:44:21 作者:chen
来源:亿速云 阅读:175

这篇文章主要讲解了“Java怎么添加、回复、修改、删除Word批注”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Java怎么添加、回复、修改、删除Word批注”吧!

批注是一种常用于对特定文档内容进行注解的工具或方法,起到解释说明、标记指正的作用。在本篇文章中,将介绍如何操作Word批注的方法,包括:

1. 添加批注:添加文本到批注、插入图片到批注;

  1.1 给指定段落添加批注

  1.2 给指定字符串添加批注

2. 回复批注;

3. 修改或替换批注:用文本替换批注中的文本内容、用文本替换批注中的图片、用图片替换批注中的图片;

4. 删除批注:删除指定批注中的所有内容、删除指定批注中的指定内容

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

Jar文件获取及导入:

方法1通过官网获取jar包,并解压。解压后,将lib文件夹下的Spire.Doc.jar文件导入java程序。

方法2:通过添加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);
     }
 }

批注添加效果:

Java怎么添加、回复、修改、删除Word批注

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

批注添加效果:

Java怎么添加、回复、修改、删除Word批注

【示例3】回复批注

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

批注回复效果:

Java怎么添加、回复、修改、删除Word批注

【示例4】修改或替换批注

 import com.spire.doc.*;
 
 public class ModifyComment {
     public static void main(String[] args){
         //加载含有批注的测试文档
         Document doc = new Document("sample.docx");
 
         //获取第一个批注中的第一段,用文本替换原有批注中的文本
         doc.getComments().get(0).getBody().getParagraphs().get(0).replace("请在试验中将包含以下特征的实验样本记录在册,并整理好周记录报表,供后续观察取样。","参照以下实验方法!",false,false);
         //获取第一个批注中的第二段,用文本替换原有批注中的图片
         doc.getComments().get(0).getBody().getParagraphs().get(1).setText("请上报管理科!");
 
         //获取第一个批注中的第三段,删除原有图片,再调用方法添加新图片(用图片替换图片)
         doc.getComments().get(0).getBody().getParagraphs().get(2).getChildObjects().removeAt(0);
         doc.getComments().get(0).getBody().getParagraphs().get(2).appendPicture("2.png");
 
         //保存文档
         doc.saveToFile("ModifyComment.docx",FileFormat.Docx_2010);
     }
 }

修改或替换结果:

Java怎么添加、回复、修改、删除Word批注

【示例5】删除批注

 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.getComments().get(0).getBody().getParagraphs().get(1).getChildObjects().removeAt(0);
 
         //保存文档
         doc.saveToFile("DeleteComment", FileFormat.Docx_2010);
     }
 }

批注删除效果:

Java怎么添加、回复、修改、删除Word批注

感谢各位的阅读,以上就是“Java怎么添加、回复、修改、删除Word批注”的内容了,经过本文的学习后,相信大家对Java怎么添加、回复、修改、删除Word批注这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. Java 添加、替换、删除Word中的图片
  2. Java 添加、读取、修改、删除Word文档属性

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:如何解决IE针对Ajax请求结果的缓存问题

下一篇:在IE浏览器下出发CSS的haslayout属性是怎样的

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》