How to export docx to png using docx4j?

2019-07-21 06:34发布

问题:

I'm using docx4j 3.3.0 and when I run the code below I get a ClassNotFoundException.

OutputStream os = new FileOutputStream(file);
FOSettings settings = Docx4J.createFOSettings();
settings.setWmlPackage(wmlPackage);
settings.setApacheFopMime("images/png");
Docx4J.toFO(settings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);

java.lang.ClassNotFoundException: org.docx4j.convert.out.fo.FOExporterXslt from [Module "org.docx4j-compat:main" from local module loader @19a1b0af (finder: local module finder @4656be4e (roots: C:\newton\jboss\modules,C:\newton\jboss\modules\system\layers\base))]

I'm sure I'm missing a dependency but I don't know what.

回答1:

In docx4j 3.3.0, export via FO moved to a separate project:

https://github.com/plutext/docx4j-export-FO

If you are using the docx4j zip distribution, add the jars in optional/export-fo

If you are using maven, add:

<groupId>org.docx4j</groupId>
<artifactId>docx4j-export-fo</artifactId>
<version>3.3.0</version>


回答2:

use this:-

settings.setApacheFopMime(MimeConstants.MIME_PNG);

I think the problem is you have "images/png", not "image/png"