Java: Apache POI: Can I get clean text from MS Word (.doc) files?

后端 未结 3 1719
野趣味
野趣味 2020-12-31 10:24

The strings I\'m (programmatically) getting from MS Word files when using Apache POI are not the same text I can look at when I open the files with MS Word.

When usi

3条回答
  •  说谎
    说谎 (楼主)
    2020-12-31 10:55

    There are two options, one provided directly in Apache POI, the other via Apache Tika (which uses Apache POI internally).

    The first option is to use WordExtractor, but wrap it in a call to stripFields(String) when calling it. That will remove the text based fields included in the text, things like HYPERLINK that you've seen. Your code would become:

    NPOIFSFileSystem fs = new NPOIFSFileSytem(file);
    WordExtractor extractor = new WordExtractor(fs.getRoot());
    
    for(String rawText : extractor.getParagraphText()) {
    String text = extractor.stripFields(rawText);
    System.out.println(text);
    }
    

    The other option is to use Apache Tika. Tika provides text extraction, and metadata, for a wide variety of files, so the same code will work for .doc, .docx, .pdf and many others too. To get clean, plain text of your word document (you can also get XHTML if you'd rather), you'd do something like:

    TikaConfig tika = TikaConfig.getDefaultConfig();
    TikaInputStream stream = TikaInputStream.get(file);
    ContentHandler handler = new BodyContentHandler();
    Metadata metadata = new Metadata();
    tika.getParser().parse(input, handler, metadata, new ParseContext());
    String text = handler.toString();
    

提交回复
热议问题