本文將介紹讀取Word批注的方法叠必,包括讀取Word批注中的文本及圖片膝藕。下面將通過Java代碼來演示如何讀取批注民泵。
工具使用:Word類庫(Free Spire.Doc for Java 免費(fèi)版)
Jar文件獲取:可通過官網(wǎng)下載鸿摇,下載后解壓文件滥酥,并將lib文件夾下的Spire.Doc.jar文件導(dǎo)入java程序更舞;也可以通過Maven倉庫安裝導(dǎo)入,具體路徑配置及導(dǎo)入方法可以參考教程
測試文檔如下:批注中包含文本和圖片
706090-20191119104440251-677711467.png
【示例1】讀取批注中的文本
import com.spire.doc.*; import com.spire.doc.documents.Paragraph; import com.spire.doc.fields.Comment; import com.spire.doc.fields.TextRange; public class ReadComment { public static void main(String[] args) { //加載測試文檔
Document doc = new Document();
doc.loadFromFile("sample.docx"); //實(shí)例化String類型變量
String text = ""; //遍歷所有批注
for(int i = 0;i< doc.getComments().getCount();i++){
Comment comment = doc.getComments().get(i); //遍歷所有批注中的段落
for(int j= 0;j < comment.getBody().getParagraphs().getCount();j++) {
Paragraph paragraph = comment.getBody().getParagraphs().get(j); //遍歷段落中的對象
for (Object object : paragraph.getChildObjects()) { //讀取文本
if (object instanceof TextRange) {
TextRange textRange = (TextRange) object;
text = text + textRange.getText();
}
}
}
} //輸入文本內(nèi)容
System.out.println(text);
}
}
批注文本讀取結(jié)果:
706090-20191119104321557-505768261.png
【示例2】讀取批注中的圖片
import com.spire.doc.*; import com.spire.doc.documents.Paragraph; import com.spire.doc.fields.Comment; import com.spire.doc.fields.DocPicture; import javax.imageio.ImageIO; import java.awt.image.RenderedImage; import java.io.File; import java.io.IOException; import java.util.ArrayList; public class ExtractImgsInComment { public static void main(String[] args) throws IOException{ //加載測試文檔
Document doc = new Document();
doc.loadFromFile("sample.docx"); //創(chuàng)建ArrayList數(shù)組對象
ArrayList images = new ArrayList(); //遍歷所有批注
for(int i = 0;i< doc.getComments().getCount();i++){
Comment comment = doc.getComments().get(i); //遍歷所有批注中的段落
for(int j= 0;j < comment.getBody().getParagraphs().getCount();j++) {
Paragraph paragraph = comment.getBody().getParagraphs().get(j); //遍歷段落中的對象
for (Object object : paragraph.getChildObjects()) { //獲取圖片對象
if(object instanceof DocPicture){
DocPicture picture = (DocPicture) object;
images.add(picture.getImage());
}
}
}
} //提取圖片坎吻,并指定圖片格式
for (int z = 0; z< images.size(); z++) {
File file = new File(String.format("圖片-%d.png", z));
ImageIO.write((RenderedImage) images.get(z), "PNG", file);
}
}
}
批注圖片讀取結(jié)果:
706090-20191119104414265-1964264329.png
愿與諸君共進(jìn)步缆蝉,大量的面試題及答案還有資深架構(gòu)師錄制的視頻錄像:有Spring,MyBatis,Netty源碼分析刊头,高并發(fā)黍瞧、高性能、分布式原杂、微服務(wù)架構(gòu)的原理印颤,JVM性能優(yōu)化、分布式架構(gòu)等這些成為架構(gòu)師必備的知識體系穿肄,可以微信搜索539413949獲取年局,最后祝大家都能拿到自己心儀的offer