Java怎么获取Word批注的文字和图片
这篇文章将为大家详细讲解有关Java获取Word批注的文字和图片的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
创新互联建站主要从事网站制作、成都网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务滨海,十载网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108
工具使用:Word类库(Free Spire.Doc for Java 免费版)
Jar文件获取:可通过官网下载,下载后解压文件,并将lib文件夹下的Spire.Doc.jar文件导入java程序。
测试文档如下:批注中包含文本和图片

【示例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");
//实例化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();
}
}
}
}
//输入文本内容
System.out.println(text);
}
}批注文本读取结果:

【示例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");
//创建ArrayList数组对象
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);
}
}
}批注图片读取结果:

关于Java获取Word批注的文字和图片的方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
新闻标题:Java怎么获取Word批注的文字和图片
网站链接:http://www.jxjierui.cn/article/peghig.html


咨询
建站咨询
