0

我正在尝试将 word docx 文件转换为 pdf 文件,全部使用 java,并且没有任何用户交互。

到目前为止,这是我的代码,我正在使用 docx4j 库。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package richard.fileupload;
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;
/**
 *
 * @author Richard
 */
public class pdfConverter {
        public static void main(String[] args) {
        createPDF();
        createPDF();
    }

    private static void createPDF() {
        try {
            long start = System.currentTimeMillis();

            // 1) Load DOCX into WordprocessingMLPackage
            InputStream is = new FileInputStream(new File(
                    "D:/HelloWorld.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:/HelloWorld.pdf"));
            PdfConversion converter = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(
                    wordMLPackage);
            converter.output(out, pdfSettings);

            System.err.println("Generate pdf/HelloWorld.pdf with "
                    + (System.currentTimeMillis() - start) + "ms");

        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

}

但是,当我尝试运行时出现此错误,它编译得很好

run:
java.lang.NoClassDefFoundError: org/apache/log4j/Logger
    at org.docx4j.openpackaging.Base.<clinit>(Base.java:42)
    at richard.fileupload.pdfConverter.createPDF(pdfConverter.java:32)
    at richard.fileupload.pdfConverter.main(pdfConverter.java:21)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 3 more
java.lang.NoClassDefFoundError: Could not initialize class org.docx4j.openpackaging.packages.WordprocessingMLPackage
    at richard.fileupload.pdfConverter.createPDF(pdfConverter.java:32)
    at richard.fileupload.pdfConverter.main(pdfConverter.java:22)
BUILD SUCCESSFUL (total time: 0 seconds)

知道错误是什么,这是什么原因?

编辑我现在收到此错误

java.lang.NoClassDefFoundError: org/apache/fop/apps/FopFactory
    at org.docx4j.convert.out.pdf.viaXSLFO.Conversion.output(Conversion.java:231)
    at richard.fileupload.pdfConverter.createPDF(pdfConverter.java:43)
    at richard.fileupload.pdfConverter.main(pdfConverter.java:22)
4

0 回答 0