Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

java - 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. 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.

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...