批注,是做者或审阅者给文档添加的注释或注解。经过查看批注,能够更加详细地了解某些文字的背景。除添加文本信息到批注外,还能够经过添加图片的方式来使其内容更具丰富性和直观性。本文将经过使用Java程序来详细介绍如何添加、修改和删除Word文档中的批注(含文本和图片)。html
方法1:经过官网下载获取jar包。解压后将lib文件夹下的Spire.Doc.jar文件导入Java程序。(以下图)java
方法2:经过maven仓库安装导入。具体安装详解参见此网页。app
import com.spire.doc.*; import com.spire.doc.documents.Paragraph; import com.spire.doc.fields.Comment; public class InsertComment { public static void main(String[] args) { //加载测试文档 Document doc = new Document("C:\\Users\\Test1\\Desktop\\Sample.docx"); //获取指定段落 Section sec = doc.getSections().get(0); Paragraph para= sec.getParagraphs().get(1); //插入文本到批注 Comment comment = para.appendComment("徐志摩,现代新月派表明诗人,原名章垿,字槱森,留学英国时更名志摩。"); //插入图片到批注 comment.getBody().addParagraph().appendPicture("C:\\Users\\Test1\\Desktop\\Image.jpg"); //保存文档 doc.saveToFile("output/InsertComment.docx", FileFormat.Docx_2010); } }
批注添加效果:maven
import com.spire.doc.*; public class ReplaceComment { public static void main(String[] args) { //加载含有批注的测试文档 Document doc = new Document("C:\\Users\\Test1\\Desktop\\InsertComment.docx"); //获取第一个批注中的第一段,用新的文本替换原有批注中的文本 doc.getComments().get(0).getBody().getParagraphs().get(0).replace("徐志摩,现代新月派表明诗人,原名章垿,字槱森,留学英国时更名志摩。", "徐志摩是一位在中国文坛上曾经活跃一时并有必定影响的做家。",false,false); //获取第一个批注中的第二段,删除原有图片,再调用方法添加新图片(用图片替换图片) doc.getComments().get(0).getBody().getParagraphs().get(1).getChildObjects().removeAt(0); doc.getComments().get(0).getBody().getParagraphs().get(1).appendPicture("C:\\Users\\Test1\\Desktop\\Image2.jpg"); //保存文档 doc.saveToFile("output/ReplaceComment.docx",FileFormat.Docx_2013); } }
批注修改效果:工具
import com.spire.doc.*; import com.spire.doc.FileFormat; public class DeleteComment { public static void main(String[] args) { String inputFile="C:\\Users\\Test1\\Desktop\\InsertComment.docx"; String outputFile="output/DeleteComment.docx"; //加载示例文档 Document doc= new Document(inputFile); //删除批注 doc.getComments().removeAt(0); //保存文档 doc.saveToFile(outputFile, FileFormat.Docx); } }
批注删除效果:测试
(本文完)spa