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



Question:

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?


Solution:1

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.


Solution:2

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!


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »