简体   繁体   中英

How do I use Apache POI to read a .DOC file in Java to separate images from text?

I need to read a Word .doc file from Java that has text and images. I need to recognize the images & text and separate them into 2 files.

I've recently heard about "Apache POI." How I can use Apache POI to read Word .doc files?

The examples and sample code on apache's site are pretty good. I recommend you start there.

http://poi.apache.org/hwpf/quick-guide.html

To get specific bits of text, first create a org.apache.poi.hwpf.HWPFDocument. Fetch the range with getRange(), then get paragraphs from that. You can then get text and other properties.

Here for an example of extracting an image. Here for the latest revision as of this writing.

And of course, the Javadocs

Note that, according to the POI site,

HWPF is still in early development.

It's not free (or even cheap!) but Aspose.Words should be able to do this. Their evaluation download will let you play with small files.

Do the destination files also have to be Docs? You could open the docs in Office and save them out as HTML. Then the separation becomes trivial. RTF is also a viable option, but I can't recommend a good RTF parser off the top of my head.

Edit to say: I just remembered another possible solution: Jacob , but you'll need an instance of Office running on the same machine. It's short for Java COM Bridge and it lets you make calls to the COM libraries in Office to manipulate the documents. I'm sure it's not as scary as it might sound!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM