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

查看:239
本文介绍了无法在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天全站免登陆