如何使用Apache POI从.docx文档获取图片和表格? [英] How to get pictures and tables from .docx document using apache poi?

查看:169
本文介绍了如何使用Apache POI从.docx文档获取图片和表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,亲爱的,我试图将整个文档从.docx文件提取到Java中的文本区域,而我唯一收到的是没有图像或表格的文本,所以有什么建议吗?预先感谢.

Dears, kindly i tried to extract whole document from .docx file to a text area in java, and What i only receive is text without images or tables, so any advice? Thanks in advance.

我的代码是:

try{
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
XWPFDocument doc = new XWPFDocument(new 
FileInputStream(chooser.getSelectedFile()));
XWPFWordExtractor extract = new XWPFWordExtractor(doc);
content.setText(extract.getText());
content.setFont(new Font("Serif", Font.ITALIC, 16));
content.setLineWrap(true);
content.setWrapStyleWord(true);
content.setBackground(Color.white);

} catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
} 

推荐答案

要提取表,请使用List<XWPFTable> table = doc.getTables()

下面的例子

public static void readWordDocument() { 
try { 
        String fileName = "C:\\sample.docx"; 

        if(!(fileName.endsWith(".doc") || fileName.endsWith(".docx"))) { 
                throw new FileFormatException(); 
        } else { 

        XWPFDocument doc = new XWPFDocument(new FileInputStream(fileName)); 

                List<XWPFTable> table = doc.getTables();         

                for (XWPFTable xwpfTable : table) { 
                                                    List<XWPFTableRow> row = xwpfTable.getRows(); 
                                                    for (XWPFTableRow xwpfTableRow : row) { 
                                                            List<XWPFTableCell> cell = xwpfTableRow.getTableCells(); 
                                                            for (XWPFTableCell xwpfTableCell : cell) { 
                                                                    if(xwpfTableCell!=null) 
                                                                    { 
                                                                            System.out.println(xwpfTableCell.getText()); 
                                                                            List<XWPFTable> itable = xwpfTableCell.getTables(); 
                                                                            if(itable.size()!=0) 
                                                                            { 
                                                                                    for (XWPFTable xwpfiTable : itable) { 
                                                                                            List<XWPFTableRow> irow = xwpfiTable.getRows(); 
                                                                                            for (XWPFTableRow xwpfiTableRow : irow) { 
                                                                                                    List<XWPFTableCell> icell = xwpfiTableRow.getTableCells(); 
                                                                                                    for (XWPFTableCell xwpfiTableCell : icell) { 
                                                                                                            if(xwpfiTableCell!=null) 
                                                                                                            {   
                                                                                                                    System.out.println(xwpfiTableCell.getText()); 
                                                                                                            } 
                                                                                                    } 
                                                                                            } 
                                                                                    } 
                                                                            } 
                                                                    } 
                                                            } 
                                                    } 
                } 
        } 
} catch(FileFormatException e) { 
        e.printStackTrace(); 
} catch (FileNotFoundException e) { 
        e.printStackTrace(); 
} catch (IOException e) { 
        e.printStackTrace(); 
} 

}

要凸出图像,请使用List<XWPFPictureData> piclist=docx.getAllPictures()

请参见下面的示例

    public static void extractImages(String src){
  try{

  //create file inputstream to read from a binary file
  FileInputStream fs=new FileInputStream(src);
  //create office word 2007+ document object to wrap the word file
  XWPFDocument docx=new XWPFDocument(fs);
  //get all images from the document and store them in the list piclist
  List<XWPFPictureData> piclist=docx.getAllPictures();
  //traverse through the list and write each image to a file
  Iterator<XWPFPictureData> iterator=piclist.iterator();
  int i=0;
  while(iterator.hasNext()){
   XWPFPictureData pic=iterator.next();
   byte[] bytepic=pic.getData();
   BufferedImage imag=ImageIO.read(new ByteArrayInputStream(bytepic));
          ImageIO.write(imag, "jpg", new File("D:/imagefromword"+i+".jpg"));
          i++;
  }

  }catch(Exception e){System.exit(-1);}

 }

这篇关于如何使用Apache POI从.docx文档获取图片和表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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