如何在使用apache poiparsingjava中的MS Word文档时知道图像或图片位置

HWPFDocument wordDoc = new HWPFDocument(new FileInputStream(fileName));

List picturesList = wordDoc.getPicturesTable()。getAllPictures();

上面的语句给出了文档中所有图片的列表。 我想知道文档中哪些文字/位置图像将位于?

你错误地看图片,这就是为什么你找不到任何位置!

你需要做的是依次处理文档的每个CharacterRun 。 将它传递给PicturesTable ,并检查字符是否有图片。如果有,从表中取回图片,并且知道它在文档中的位置,因为它具有来自它的运行

最简单的就是这样的:

PicturesSource pictures = new PicturesSource(document); PicturesTable pictureTable = document.getPicturesTable(); Range r = document.getRange(); for(int i=0; i<r.numParagraphs(); i++) { Paragraph p = r.getParagraph(i); for(int j=0; j<p.numCharacterRuns(); j++) { CharacterRun cr = p.getCharacterRun(j); if (pictureTable.hasPicture(cr)) { Picture picture = pictures.getFor(cr); // Do something useful with the picture } } } 

你可以在Apache Tika解析器中为Microsoft Word .doc找到一个很好的例子,它由Apache POI