簡體   English   中英

如何使用Apache HWPF從DOC文件中提取文本和圖像

[英]How to use Apache HWPF to extract text and images out of a DOC file

我下載了Apache HWPF 我想用它來讀取doc文件並將其文本寫入純文本文件。 我不太了解HWPF。

我非常簡單的程序在這里:

我現在有3個問題:

  1. 一些包有錯誤(他們找不到apache hdf)。 我怎么能解決它們?

  2. 我如何使用HWDF的方法來查找和提取圖像?

  3. 我的某些程序不完整且不正確。 所以請幫我完成它。

我必須在2天內完成這個程序。

我再說一遍請請幫助我完成這個。

非常感謝你們的幫助!

這是我的基本代碼:

public class test {
  public void m1 (){
    String filesname = "Hello.doc";
    POIFSFileSystem fs = null;
    fs = new POIFSFileSystem(new FileInputStream(filesname ); 
    HWPFDocument doc = new HWPFDocument(fs);
    WordExtractor we = new WordExtractor(doc);
    String str = we.getText() ;
    String[] paragraphs = we.getParagraphText();
    Picture pic = new Picture(. . .) ;
    pic.writeImageContent( . . . ) ;
    PicturesTable picTable = new PicturesTable( . . . ) ;
    if ( picTable.hasPicture( . . . ) ){
      picTable.extractPicture(..., ...);
      picTable.getAllPictures() ;
    }
}
    //you can use the org.apache.poi.hwpf.extractor.WordExtractor to get the text
    String fileName = "example.doc";
    HWPFDocument wordDoc = new HWPFDocument(new FileInputStream(fileName));
    WordExtractor extractor = new WordExtractor(wordDoc);
    String[] text = extractor.getParagraphText();
    int lineCounter = text.length;
    String articleStr = ""; // This string object use to store text from the word document.
    for(int index = 0;index < lineCounter;++ index){
        String paragraphStr = text[index].replaceAll("\r\n","").replaceAll("\n","").trim();
        int paragraphLength = paragraphStr.length();
        if(paragraphLength != 0){
            articleStr.concat(paragraphStr);
        }
    }
    //you can use the org.apache.poi.hwpf.usermodel.Picture to get the image
    List<Picture> picturesList = wordDoc.getPicturesTable().getAllPictures();
    for(int i = 0;i < picturesList.size();++i){
        BufferedImage image = null;
        Picture pic = picturesList.get(i);
        image = ImageIO.read(new ByteArrayInputStream(pic.getContent()));
        if(image != null){
            System.out.println("Image["+i+"]"+" ImageWidth:"+image.getWidth()+" ImageHeight:"+image.getHeight()+" Suggest Image Format:"+pic.suggestFileExtension());
        }
    }

Apache Tika將為您完成此任務。 它處理與POI交談以執行HWPF操作,並為您提供XHTML或純文本以獲取文件內容。 如果您注冊一個遞歸解析器,那么您也將獲得所有嵌入的圖像。

我知道這很久以后,但我在谷歌代碼上發現了TextMining,更准確,更易於使用。 然而,它幾乎被遺棄的代碼。

如果你只是想這樣做,而你不關心編碼,你可以使用Antiword

$ antiword file.doc> out.txt

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM