2016-09-16 67 views
1

我尝试使用帮助java api Apache POI读取文件.docx。我使用:如何使用apache-poi获取文件的全部内容?

public static String view(String nameDoc){ 
    String text = null; 
    try{ 
     XWPFDocument docx = new XWPFDocument(
       new FileInputStream(nameDoc)); 
     XWPFWordExtractor we = new XWPFWordExtractor(docx); 
     text = we.getText(); 
     we.close(); 
     docx.close(); 
    }catch (Exception e){ 
     e.printStackTrace(); 
    } 
    return text; 
} 

在这种情况下,我得到的只是文件的文本,但我的文件包括文本,表格,图片...我怎样才能得到文件的全部内容?

+0

看到我的答案,它会工作,并帮助你.. –

+2

你是什么意思“文件的全部内容”?例如,我看不出如何在文本字符串中获取图片.... – Gagravarr

+0

此答案应该有所帮助http://stackoverflow.com/a/28304463/1997376 –

回答

0
String contents = ""; 

    try { 
     System.out.println("Starting the test"); 
     POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("D:/Resume.doc")); 
     HWPFDocument doc = new HWPFDocument(fs); 
     WordExtractor we = new WordExtractor(doc); 
     OutputStream file = new FileOutputStream(new File("D:/test.pdf")); 
     PdfWriter parser = PdfWriter.getInstance(doc, file); 
     parser.parse(); 
     PDDocument pdfDocument = parser.getPDDocument(); 
     PDFTextStripper stripper = new PDFTextStripper(); 
     contents = stripper.getText(pdfDocument); 
     pdfDocument.close(); 

    } catch (Exception e) { 
     logger.error(e.getMessage()); 
    } 

contents你会得到完整的文件内容。

+0

它是一个docx不是pdf –

+0

它doesn '提供完整的内容(图像,表..包括),但只有文本内容 –

+0

@NicolasFilotto,提取图像请参考http://stackoverflow.com/questions/7063324/extract-image-from-pdf-using- java –

相关问题