Read Microsoft Word Documents into Plain Text (DOC, DOCX) in Java

cfans picture cfans · Feb 15, 2010 · Viewed 9.3k times · Source

I'm looking for something in Java to read in Word documents to process their text.. all I need is there text, nothing fancy. I know about Apache POI, however it doesn't include support for DOCX right now, anything out there?

Answer

Joseph Kulandai picture Joseph Kulandai · Mar 1, 2010

If you don't require formatting information, images and all other fancy stuff, then the job is lot easier. Just some 5 to 10 lines of code will do.

  1. Treat DOCX as a zip file. It consists a bunch of files which includes 'document.xml'. Use ZipInputStream and extract that file alone. (you may use your favorite zip utility and open docx and see for yourself!)
  2. Use a SAX parser and read contents between node body/p/r/t - voila you got the text!

This is applicable only if you need the text only.