使用docx4j将docx转换为pdf的问题

时间:2013-07-22 04:59:52

标签: docx4j pdf-conversion

我正在使用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

1 个答案:

答案 0 :(得分:0)

通过改为罐子解决。我现在使用2.8.0及其罚款。