无法按 Apache POI 中的 Word 文档 (docx) 的顺序阅读所有内容 [英] Unable to read all content in order of a word document (docx) in Apache POI

查看:25
本文介绍了无法按 Apache POI 中的 Word 文档 (docx) 的顺序阅读所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试阅读 Word 文档中的所有内容(包括表格、图片、段落).我可以使用 getBodyElementsIterator() 读取表格和段落,但它无法读取文档中的图片.虽然我可以使用 getAllPictures() 单独阅读图片,但我需要按顺序阅读所有内容.

I've been trying to read all content (including tables, pictures, paragraphs) from a word document. I'm able to read tables and paragraphs using getBodyElementsIterator() but it doesn't read pictures present inside the document. Although I'm able to read pictures seperately using getAllPictures() but I need to read everything in order.

我尝试在 getBodyElementsIterator() 中循环时寻找 XWPFPicture 实例,但我找不到任何图像实例.

I've tried looking for XWPFPicture instance while looping inside getBodyElementsIterator() but I'm not able to find any image instance.

Iterator<IBodyElement> iter = xdoc.getBodyElementsIterator();
           while (iter.hasNext()) {
               IBodyElement elem = iter.next();
               if (elem instanceof XWPFParagraph) {
                  System.out.println("para - "+elem.getClass());
               } else if (elem instanceof XWPFTable) {
                  System.out.println("table - "+elem);
               } else if (elem instanceof XWPFPictureData){
                  System.out.println("picture - "+elem);
               } else {
                  System.out.println("else - "+elem);
               }  
            }

这是我得到的输出.

paraorg.apache.poi.xwpf.usermodel.XWPFParagraph@4d3167f4
paraorg.apache.poi.xwpf.usermodel.XWPFParagraph@ed9d034
tableorg.apache.poi.xwpf.usermodel.XWPFTable@6121c9d6
paraorg.apache.poi.xwpf.usermodel.XWPFParagraph@87f383f
paraorg.apache.poi.xwpf.usermodel.XWPFParagraph@4eb7f003

它包含段落和表格,但不包含任何图片

It contains paragraphs and tables but not any pictures

推荐答案

如评论中所述,apache poi 中如何按 word 文档 (docx) 的顺序阅读所有内容的问题很多太广泛了,无法在这里回答.*.docxOffice Open XML 文件格式的 ZIP 存档.它包含文档正文的 document.xml.这是需要遍历的非常复杂的 XML.但是 document.xml 可能包含对 *.docx ZIP 存档中其他资源的引用,然后也需要遍历这些资源.

As told in comments already the question how to read all content in order of a word document (docx) in apache poi is much too broad to be answerable here. A *.docx is a ZIP archive in Office Open XML file format. It contains the document.xml for the document body. This is very complex XML which needs to be traversed. But that document.xml might contain references to other resources in the *.docx ZIP archive which then also needs to be traversed.

我能提供的是这个遍历过程的模板.它从 XWPFDocument 开始,在首先遍历所有 IBodyElement在里面.然后根据找到的IBodyElement类型做进一步的遍历处理.

What I can provide is a template of how this traversing process could look like. It starts at XWPFDocument and at first traverses all the IBodyElements in it. According to the found type of IBodyElement it does further traversing processes then.

import java.io.FileInputStream;

import org.apache.poi.xwpf.usermodel.*;

import java.util.List;

public class WordReadAllContent {

 static void traversePictures(List<XWPFPicture> pictures) throws Exception {
  for (XWPFPicture picture : pictures) {
   System.out.println(picture);
   XWPFPictureData pictureData = picture.getPictureData();
   System.out.println(pictureData);
  }
 }

 static void traverseRunElements(List<IRunElement> runElements) throws Exception {
  for (IRunElement runElement : runElements) {
   if (runElement instanceof XWPFFieldRun) {
    XWPFFieldRun fieldRun = (XWPFFieldRun)runElement;
    System.out.println(fieldRun.getClass().getName());
    System.out.println(fieldRun);
    traversePictures(fieldRun.getEmbeddedPictures());
   } else if (runElement instanceof XWPFHyperlinkRun) {
    XWPFHyperlinkRun hyperlinkRun = (XWPFHyperlinkRun)runElement;
    System.out.println(hyperlinkRun.getClass().getName());
    System.out.println(hyperlinkRun);
    traversePictures(hyperlinkRun.getEmbeddedPictures());
   } else if (runElement instanceof XWPFRun) {
    XWPFRun run = (XWPFRun)runElement;
    System.out.println(run.getClass().getName());
    System.out.println(run);
    traversePictures(run.getEmbeddedPictures());
   } else if (runElement instanceof XWPFSDT) {
    XWPFSDT sDT = (XWPFSDT)runElement;
    System.out.println(sDT);
    System.out.println(sDT.getContent());
    //ToDo: The SDT may have traversable content too.
   }
  }
 }

 static void traverseTableCells(List<ICell> tableICells) throws Exception {
  for (ICell tableICell : tableICells) {
   if (tableICell instanceof XWPFSDTCell) {
    XWPFSDTCell sDTCell = (XWPFSDTCell)tableICell;
    System.out.println(sDTCell);
    //ToDo: The SDTCell may have traversable content too.
   } else if (tableICell instanceof XWPFTableCell) {
    XWPFTableCell tableCell = (XWPFTableCell)tableICell;
    System.out.println(tableCell);
    traverseBodyElements(tableCell.getBodyElements());
   }
  }
 }

 static void traverseTableRows(List<XWPFTableRow> tableRows) throws Exception {
  for (XWPFTableRow tableRow : tableRows) {
   System.out.println(tableRow);
   traverseTableCells(tableRow.getTableICells());
  }
 }

 static void traverseBodyElements(List<IBodyElement> bodyElements) throws Exception {
  for (IBodyElement bodyElement : bodyElements) {
   if (bodyElement instanceof XWPFParagraph) {
    XWPFParagraph paragraph = (XWPFParagraph)bodyElement;
    System.out.println(paragraph);
    traverseRunElements(paragraph.getIRuns());
   } else if (bodyElement instanceof XWPFSDT) {
    XWPFSDT sDT = (XWPFSDT)bodyElement;
    System.out.println(sDT);
    System.out.println(sDT.getContent());
    //ToDo: The SDT may have traversable content too.
   } else if (bodyElement instanceof XWPFTable) {
    XWPFTable table = (XWPFTable)bodyElement;
    System.out.println(table);
    traverseTableRows(table.getRows());
   }
  }
 }

 public static void main(String[] args) throws Exception {

  String inFilePath = "./WordDocument.docx";

  XWPFDocument document = new XWPFDocument(new FileInputStream(inFilePath));
  traverseBodyElements(document.getBodyElements());

  document.close();
 }

}

这是一个工作草案.我确定,我忘记了什么.

This is a working draft. I am sure, I forgot something.

这篇关于无法按 Apache POI 中的 Word 文档 (docx) 的顺序阅读所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆