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;
publicclassExtractImgsInComment {
publicstaticvoid main(String[] args) throws IOException{
//加载测试文档
Document doc = newDocument();
doc.loadFromFile("sample.docx");
//创建ArrayList数组对象
ArrayList images = newArrayList();
//遍历所有批注
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 instanceofDocPicture){
DocPicture picture = (DocPicture) object;
images.add(picture.getImage());
}
}
}
}
//提取图片,并指定图片格式
for(int z = 0; z< images.size(); z++) {
File file = newFile(String.format("图片-%d.png", z));
ImageIO.write((RenderedImage) images.get(z), "PNG", file);
}
}
}