我的任务是从 docx 文件中提取所有图像。我正在使用下面的代码片段来实现相同的目的。我正在使用 Apache POI api 来实现相同的目的。
`File file = new File(InputFileString);
FileInputStream fs = new FileInputStream(file.getAbsolutePath());
//FileInputStream fs=new FileInputStream(src);
//create office word 2007+ document object to wrap the word file
XWPFDocument doc1x=new XWPFDocument(fs);
//get all images from the document and store them in the list piclist
List<XWPFPictureData> piclist=doc1x.getAllPictures();
//traverse through the list and write each image to a file
Iterator<XWPFPictureData> iterator=piclist.iterator();
int i=0;
while(iterator.hasNext()){
XWPFPictureData pic=iterator.next();
byte[] bytepic=pic.getData();
BufferedImage imag=ImageIO.read(new ByteArrayInputStream(bytepic));
ImageIO.write(imag, "jpg", new File("C:/imagefromword"+i+".jpg"));
i++;
}`
但是,此代码无法检测文档页脚或页眉部分中的任何图像。
我广泛使用了我的谷歌技能,但无法想出任何有用的东西。
Is there anyway to capture the image file in the footer section of the docx file?
最佳答案
我不是 Apache POI
问题方面的专家,但简单的搜索得出了 this代码:
package com.concretepage;
import java.io.FileInputStream;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFFooter;
import org.apache.poi.xwpf.usermodel.XWPFHeader;
public class ReadDOCXHeaderFooter {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("D:/docx/read-test.docx");
XWPFDocument xdoc=new XWPFDocument(OPCPackage.open(fis));
XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(xdoc);
//read header
XWPFHeader header = policy.getDefaultHeader();
System.out.println(header.getText());
//read footer
XWPFFooter footer = policy.getDefaultFooter();
System.out.println(footer.getText());
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
以及 XWPFHeaderFooter 的文档页面(这是上面示例中 XWPFFooter 类的直接父类...)显示相同的 getAllPictures您用来迭代文档正文中所有图片的方法。
我在移动设备上,所以我还没有真正测试过任何东西 - 但它看起来足够简单,可以工作。
祝你好运!
关于JAVA:从 docx 文档中提取页脚图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42667929/