我正在尝试使用docx4j 3.0加载现有的docx文件,进行一些简单的文本更改并另存为PDF。我的代码如下所示:
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load (new File (template));
MainDocumentPart doc = wordMLPackage.getMainDocumentPart ();
try
{
VariablePrepare.prepare (wordMLPackage);
}
catch (Exception e)
{
System.err.println ("Warning: VariablePrepare failed: " + e);
}
(here I enumerate all Text nodes and make changes as required)
PdfConversion c = new Conversion (wordMLPackage);
try (FileOutputStream out = new FileOutputStream (outputFile)) {
c.output (out, new PdfSettings ());
}
这将产生以下异常:
org.docx4j.openpackaging.exceptions.Docx4JException: Exception exporting package
at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:79)
at org.docx4j.Docx4J.toFO(Docx4J.java:467)
at org.docx4j.Docx4J.toPDF(Docx4J.java:477)
at org.docx4j.convert.out.pdf.viaXSLFO.Conversion.output(Conversion.java:70)
at net.meridiandigital.binco.invoicegen.InvoiceGenerator.generateFile(InvoiceGenerator.java:80)
at net.meridiandigital.binco.invoicegen.Test.main(Test.java:46)
Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: Exception loading default template "org/docx4j/convert/out/fo/docx2fo.xslt", Cannot convert argument/return type in call to method 'org.docx4j.convert.out.common.XsltCommonFunctions.notImplemented(reference, node-type, string)'
at org.docx4j.convert.out.common.AbstractXsltExporterDelegate.loadDefaultTemplates(AbstractXsltExporterDelegate.java:92)
at org.docx4j.convert.out.common.AbstractXsltExporterDelegate.getDefaultTemplate(AbstractXsltExporterDelegate.java:75)
at org.docx4j.convert.out.common.AbstractXsltExporterDelegate.getTemplates(AbstractXsltExporterDelegate.java:66)
at org.docx4j.convert.out.common.AbstractXsltExporterDelegate.process(AbstractXsltExporterDelegate.java:57)
at org.docx4j.convert.out.common.AbstractWmlExporter.process(AbstractWmlExporter.java:63)
at org.docx4j.convert.out.common.AbstractWmlExporter.process(AbstractWmlExporter.java:32)
at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:71)
... 5 more
Caused by: javax.xml.transform.TransformerConfigurationException: Cannot convert argument/return type in call to method 'org.docx4j.convert.out.common.XsltCommonFunctions.notImplemented(reference, node-type, string)'
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:945)
at org.docx4j.XmlUtils.getTransformerTemplate(XmlUtils.java:842)
at org.docx4j.convert.out.common.AbstractXsltExporterDelegate.loadDefaultTemplates(AbstractXsltExporterDelegate.java:88)
... 11 more
任何想法出什么事了吗?
最佳答案
它不应该使用com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl
反而org.apache.xalan.processor.TransformerFactoryImpl
(您需要在类路径上使用真正的Xalan)
另请参见https://github.com/plutext/docx4j/blob/master/src/main/java/org/docx4j/XmlUtils.java的第224行的InstantiateTransformerFactory()