JAVA: 从 docx 文档中提取页脚图像

JAVA: Extract Footer Images from a docx document

我的任务是从 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++;
  }`

但是,此代码无法检测文档页脚或页眉部分中的任何图像。

我已经广泛使用了我的 google 技能,但无法想出任何有用的东西。

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 (which is the direct father class of the XWPFFooter class in the above example...) shows the same getAllPictures 方法的文档页面,您用来遍历文档正文中的所有图片。

我在移动设备上,所以我还没有真正测试过任何东西 - 但它似乎足够简单。

祝你好运!