使用docx4j将Word文档转换为PDF

时间:2014-02-21 08:58:51

标签: java pdf ms-word docx docx4j

我正在尝试将docx文件转换为pdf,docx是一个包含已通过docx4j编辑的标题,文本和图片的模板。这是转换代码......

private static void wordClientToPDF() throws FileNotFoundException, IOException
{
    try {

        // 1) Load DOCX into WordprocessingMLPackage
        InputStream is = new FileInputStream(new File(WORD_FILE_PATH_CLIENT));
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(is);

        // 2) Prepare Pdf settings
        PdfSettings pdfSettings = new PdfSettings();

        // 3) Convert WordprocessingMLPackage to Pdf
        OutputStream out = new FileOutputStream(new File(PDF_FILE_PATH_CLIENT));
        PdfConversion converter = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(wordMLPackage);
        converter.output(out, pdfSettings);

    } catch (Throwable e) {
        //YURY: print stack trace in message
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        sw.toString(); // stack trace as a string
        javax.swing.JOptionPane.showMessageDialog(panel, "PDF conversion error: " + sw.toString());
    }
}

但是,我收到以下错误DocxToPdfConversionError

感谢任何帮助。

0 个答案:

没有答案