【问题标题】:how to get a whole content of file using apache-poi?如何使用 apache-poi 获取文件的全部内容?
【发布时间】:2016-09-16 09:29:05
【问题描述】:

我尝试使用帮助 java api Apache POI 读取文件 .docx。我用:

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

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

【问题讨论】:

  • 看我的回答,它会起作用并帮助你..
  • “文件的全部内容”是什么意思?例如,我看不到如何在文本字符串中获取图片....
  • 这个答案应该可以帮助stackoverflow.com/a/28304463/1997376

标签: java ms-word apache-poi docx


【解决方案1】:
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 中,您可以获得文件的全部内容。

【讨论】:

  • 这是一个 docx 不是 pdf
  • 它不提供完整的内容(包括图片、表格......),而只提供文本内容
  • @NicolasFilotto , 提取图片请参考stackoverflow.com/questions/7063324/…
  • 变量document在哪里? PdfWriter parser = PdfWriter.getInstance(document, file);parser.parse(); 找不到方法parse()
  • @Oleg1n 它的doc
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-25
相关资源
最近更新 更多