? ? ? ?批注,是作者或審閱者給文檔添加的注釋或注解乔遮。通過查看批注扮超,可以更加詳細地了解某些文字的背景。除添加文本信息到批注外蹋肮,還可以通過添加圖片的方式來使其內容更具豐富性和直觀性出刷。本文將通過使用Java程序來詳細介紹如何添加、修改和刪除Word文檔中的批注(含文本和圖片)坯辩。
使用工具:Free Spire.Doc for Java (免費版)
Jar文件獲取及導入:
方法1:通過官網(wǎng)下載獲取jar包馁龟。解壓后將lib文件夾下的Spire.Doc.jar文件導入Java程序。(如下圖)
方法2:通過maven倉庫安裝導入漆魔。具體安裝詳情參見此網(wǎng)頁坷檩。
【示例1】添加Word批注
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("徐志摩,現(xiàn)代新月派代表詩人改抡,原名章垿矢炼,字槱森,留學英國時改名志摩阿纤。");
//插入圖片到批注comment.getBody().addParagraph().appendPicture("C:\\Users\\Test1\\Desktop\\Image.jpg");
//保存文檔
doc.saveToFile("output/InsertComment.docx", FileFormat.Docx_2010);
??? }
}
批注添加效果:
【示例2】修改Word批注
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("徐志摩,現(xiàn)代新月派代表詩人欠拾,原名章垿胰锌,字槱森,留學英國時改名志摩藐窄。","徐志摩是一位在中國文壇上曾經(jīng)活躍一時并有一定影響的作家资昧。",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);
??? }
}
批注修改效果:
【示例3】刪除Word批注
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);
?}
}
批注刪除效果:
(本文完)