在使用docx4j将docx转换为png时,获取UnsupportedOperationException。下面是示例代码。
public class DocToPng {
public static void main(String[] args) throws Docx4JException, IOException {
String inPath="D:\\pdfconverterfolder\\test.docx";
String outPath="D:\\pdfconverterfolder\\pngOutput.png";
File theFile = new File(inPath);
File outile=new File(outPath);
WordprocessingMLPackage wordMLPckg = Docx4J.load(theFile);
OutputStream os = new FileOutputStream(outile);
FOSettings settings = Docx4J.createFOSettings();
settings.setWmlPackage(wordMLPckg);
settings.setApacheFopMime("images/png");
Docx4J.toFO(settings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
os.close();
System.out.println("DOne");
}
}
及以下是控制台输出:
[main] INFO org.docx4j.org.apache.xml.serializer.ToXMLStream - Using repackaged ToXMLStream
[main] INFO org.docx4j.org.apache.xml.serializer.ToXMLStream - Using repackaged ToXMLStream
[main] ERROR org.docx4j.convert.out.common.AbstractExporter - Exception exporting package
java.lang.UnsupportedOperationException: Don't know how to handle "images/png" as an output format. Neither an FOEventHandler, nor a Renderer could be found for this output format.
at org.apache.fop.render.RendererFactory.createFOEventHandler(RendererFactory.java:365)
at org.apache.fop.fo.FOTreeBuilder.<init>(FOTreeBuilder.java:107)
at org.apache.fop.apps.Fop.createDefaultHandler(Fop.java:104)
at org.apache.fop.apps.Fop.<init>(Fop.java:78)
at org.apache.fop.apps.FOUserAgent.newFop(FOUserAgent.java:182)
at org.apache.fop.apps.FopFactory.newFop(FopFactory.java:219)
at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:240)
at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:187)
at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:168)
at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:47)
at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:82)
at org.docx4j.Docx4J.toFO(Docx4J.java:689)
at toImage.DocToPng.main(DocToPng.java:26)
Exception in thread "main" org.docx4j.openpackaging.exceptions.Docx4JException: Exception exporting package
at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:109)
at org.docx4j.Docx4J.toFO(Docx4J.java:689)
at toImage.DocToPng.main(DocToPng.java:26)
Caused by: java.lang.UnsupportedOperationException: Don't know how to handle "images/png" as an output format. Neither an FOEventHandler, nor a Renderer could be found for this output format.
at org.apache.fop.render.RendererFactory.createFOEventHandler(RendererFactory.java:365)
at org.apache.fop.fo.FOTreeBuilder.<init>(FOTreeBuilder.java:107)
at org.apache.fop.apps.Fop.createDefaultHandler(Fop.java:104)
at org.apache.fop.apps.Fop.<init>(Fop.java:78)
at org.apache.fop.apps.FOUserAgent.newFop(FOUserAgent.java:182)
at org.apache.fop.apps.FopFactory.newFop(FopFactory.java:219)
at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:240)
at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:187)
at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:168)
at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:47)
at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:82)
... 2 more
似乎docx4j不支持转换为任何图像格式。有任何建议吗?谢谢!!
答案 0 :(得分:0)
settings.setApacheFopMime(MimeConstants.MIME_PNG);
为我工作。
我想问题是您有“ images / png”,而不是“ image / png”