How to extract Docx (Word 2007 above) using Apache POI

Hi, I am using Apache POI 3.6. I have already generated the code.

XWPFDocument doc = new XWPFDocument(new FileInputStream(file));
         wordxExtractor = new XWPFWordExtractor(doc);
         text = wordxExtractor.getText();

         System.out.println("adding docx " + file);
         d.add(new Field("content", text, Field.Store.NO, Field.Index.ANALYZED));

Unfortunately, he generated an error.

Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentException
at org.apache.poi.openxml4j.opc.OPCPackage.init(OPCPackage.java:149)
at org.apache.poi.openxml4j.opc.OPCPackage.<init>(OPCPackage.java:136)
at org.apache.poi.openxml4j.opc.Package.<init>(Package.java:54)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:98)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:199)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:178)
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:53)
at org.apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.java:98)
at org.apache.lucene.demo.Indexer.indexDocs(Indexer.java:153)
at org.apache.lucene.demo.Indexer.main(Indexer.java:88)

It seemed like he was using a constructor

XWPFWordExtractor (OPCPackage container)

but not this →

XWPFWordExtractor (XWPFDocument document)

Any wonder why ??? Or any idea how I can extract .docx and then convert it to a string?

+3
source share
3 answers

You need to add the dom4j library to your project library or project libraries

+3
source

It looks like you don't have all the dependencies on your class path.

http://poi.apache.org/overview.html, , dom4j OOXML. , , , ... POI, ooxml-libs.

+2

Source: https://habr.com/ru/post/1762369/


All Articles