我正在使用 docx4j 2.8.1 并尝试转换几个不同的 docx 文件,但我总是遇到同样的问题。问题可能来自库的版本或缺少某些依赖项。
代码:
package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.docx4j.convert.out.pdf.PdfConversion;
import org.docx4j.convert.out.pdf.viaXSLFO.PdfSettings;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
public class pdfConverter {
public static void main(String[] args) {
createPDF();
}
private static void createPDF() {
try {
// 1) Load DOCX into WordprocessingMLPackage
InputStream is = new FileInputStream(
new File("D:/TestDoc/Res.docx"));
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
.load(is);
// 2) Prepare Pdf settings
PdfSettings pdfSettings = new PdfSettings();
// 3) Convert WordprocessingMLPackage to Pdf
OutputStream out = new FileOutputStream(new File(
"D:/TestDoc/Res.pdf"));
PdfConversion converter = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(
wordMLPackage);
converter.output(out, pdfSettings);
} catch (Throwable e) {
e.printStackTrace();
}
}
}
错误:
org.docx4j.openpackaging.exceptions.Docx4JException: FOP issues
at org.docx4j.convert.out.pdf.viaXSLFO.Conversion.output(Conversion.java:374)
at test.pdfConverter.createPDF(pdfConverter.java:42)
at test.pdfConverter.main(pdfConverter.java:21)
Caused by: java.lang.NullPointerException
at org.docx4j.XmlUtils.transform(XmlUtils.java:842)
at org.docx4j.XmlUtils.transform(XmlUtils.java:802)
at org.docx4j.convert.out.pdf.viaXSLFO.Conversion.output(Conversion.java:349)
... 2 more