简体   繁体   English

无法按 Apache POI 中的 Word 文档 (docx) 的顺序阅读所有内容

[英]Unable to read all content in order of a word document (docx) in Apache POI

I've been trying to read all content (including tables, pictures, paragraphs) from a word document.我一直在尝试从 Word 文档中读取所有内容(包括表格、图片、段落)。 I'm able to read tables and paragraphs using getBodyElementsIterator() but it doesn't read pictures present inside the document.我可以使用 getBodyElementsIterator() 读取表格和段落,但它无法读取文档中的图片。 Although I'm able to read pictures seperately using getAllPictures() but I need to read everything in order.虽然我可以使用 getAllPictures() 单独阅读图片,但我需要按顺序阅读所有内容。

I've tried looking for XWPFPicture instance while looping inside getBodyElementsIterator() but I'm not able to find any image instance.我尝试在 getBodyElementsIterator() 中循环时寻找 XWPFPicture 实例,但我找不到任何图像实例。

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);
               }  
            }

This is the output I'm getting.这是我得到的输出。

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它包含段落和表格,但不包含任何图片

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.正如评论中已经说过的,如何在apache poi中按照 word 文档 (docx) 的顺序阅读所有内容的问题太广泛了,无法在这里回答。 A *.docx is a ZIP archive in Office Open XML file format. *.docxOffice Open XML文件格式的ZIP存档。 It contains the document.xml for the document body.它包含document.xml的文档主体。 This is very complex XML which needs to be traversed.这是需要遍历的非常复杂的XML But that document.xml might contain references to other resources in the *.docx ZIP archive which then also needs to be traversed.但是该document.xml可能包含对*.docx ZIP存档中其他资源的引用,然后也需要遍历这些资源。

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 IBodyElement s in it.它从XWPFDocument开始,首先遍历其中的所有IBodyElement According to the found type of IBodyElement it does further traversing processes then.然后根据找到的IBodyElement类型进行进一步的遍历过程。

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.我确定,我忘记了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM