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

标签 java apache-poi docx

我的任务是从 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/

相关文章:

java - 使用 @Query 'No converter found capable of converting from type XXX' 时出现错误 ("Select * from")

java - SQLITE_ERROR - SQL 错误或丢失数据库(接近 "AUTOINCREMENT": syntax error)

java - 将 Apache POI 中最大长度的文本写入单元格

java - 如何使用 Apache POI 读取 Excel 文件中特定行的数据?

java - 如何在 Apache POI 中调整图像环绕样式

Flutter - 尝试生成服务契约(Contract)PDF

java - 从 Sprite Sheet Java 读取图像

java - 每当执行应用程序时,它在 android 模拟器中显示为空白

android - 在错误的excel表中插入图像?

java - 以编程方式将字体嵌入到 docx4j 文档中(在 XHTML - DOCX 转换期间)