Comment obtenir des images et des tableaux à partir du document .docx en utilisant apache poi?

Dears, gentiment j’ai essayé d’extraire le document entier du fichier .docx dans une zone de texte en Java, et Ce que je ne reçois que du texte sans images ou des tableaux, donc des conseils? Merci d’avance.

Mon code est:

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

Pour extraire des tables, utilisez List table = doc.getTables()

L’exemple ci-dessous

 public static void readWordDocument() { try { Ssortingng fileName = "C:\\sample.docx"; if(!(fileName.endsWith(".doc") || fileName.endsWith(".docx"))) { throw new FileFormatException(); } else { XWPFDocument doc = new XWPFDocument(new FileInputStream(fileName)); List table = doc.getTables(); for (XWPFTable xwpfTable : table) { List row = xwpfTable.getRows(); for (XWPFTableRow xwpfTableRow : row) { List cell = xwpfTableRow.getTableCells(); for (XWPFTableCell xwpfTableCell : cell) { if(xwpfTableCell!=null) { System.out.println(xwpfTableCell.getText()); List itable = xwpfTableCell.getTables(); if(itable.size()!=0) { for (XWPFTable xwpfiTable : itable) { List irow = xwpfiTable.getRows(); for (XWPFTableRow xwpfiTableRow : irow) { List 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(); } 

}

Pour extraire des images, utilisez List piclist=docx.getAllPictures()

Voir exemple ci-dessous

  public static void extractImages(Ssortingng 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 piclist=docx.getAllPictures(); //traverse through the list and write each image to a file Iterator 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);} }