使用 docx4j 的 docx 到 png 转换器

docx to png converter using docx4j

使用 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 不支持转换成任何图像 format.Any 建议。?谢谢!

    settings.setApacheFopMime(MimeConstants.MIME_PNG);

适合我。

我想问题是你有 "images/png",而不是 "image/png"