2016-09-16 64 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 –

相關問題