首页 > 解决方案 > 将 .docx 转换为 .pdf

问题描述

我正在尝试使用 Apache POI 将 docx 转换为 pdf。我看到了很多关于这个问题的问题,但即使是最好的答案也不起作用。我已经尝试将版本更改为最旧的版本 - 没有结果。我使用 Kotlin 和 Gradle。主类代码如下:

import org.apache.poi.xwpf.converter.pdf.PdfConverter
import org.apache.poi.xwpf.converter.pdf.PdfOptions
import org.apache.poi.xwpf.usermodel.XWPFDocument
import java.io.*


class o {
    fun ConvertToPDF(docPath: String?, pdfPath: String?) {
            val doc = FileInputStream(File(docPath!!))
            val document = XWPFDocument(doc)
            val options = PdfOptions.create()
            val out = FileOutputStream(File(pdfPath!!))
            PdfConverter.getInstance().convert(document, out, options)
    }

    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            val cwoWord = o()
            println("Start")
            cwoWord.ConvertToPDF("C:\\simple.docx",
                "123.pdf")
        }
    }
}

带有依赖项的 build.gradle

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"

    implementation "org.apache.poi:poi-ooxml-schemas:4.1.0"
    implementation "fr.opensagres.xdocreport:org.apache.poi.xwpf.converter.core:1.0.6"
    implementation "fr.opensagres.xdocreport:org.apache.poi.xwpf.converter.pdf:1.0.6"
    implementation "fr.opensagres.xdocreport:fr.opensagres.xdocreport.itext.extension:2.0.0"
    implementation "com.lowagie:itext:2.1.7"
    implementation "org.apache.poi:ooxml-schemas:1.4"
    implementation "org.apache.xmlbeans:xmlbeans:3.1.0"
    implementation "org.dom4j:dom4j:2.1.1"
    implementation "org.apache.poi:poi-ooxml:4.1.0"
    implementation "org.apache.poi:poi:4.1.0"   

    testCompile group: 'junit', name: 'junit', version: '4.12'
}

错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/POIXMLDocumentPart
    at org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument.getFontsDocument(XWPFStylesDocument.java:1477)
    at org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument.<init>(XWPFStylesDocument.java:190)
    at org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument.<init>(XWPFStylesDocument.java:184)
    at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.createStylesDocument(XWPFDocumentVisitor.java:166)
    at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.<init>(XWPFDocumentVisitor.java:159)
    at org.apache.poi.xwpf.converter.pdf.internal.PdfMapper.<init>(PdfMapper.java:149)
    at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:55)
    at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:38)
    at org.apache.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:45)
    at o.ConvertToPDF(o.kt:13)
    at o$Companion.main(o.kt:21)
    at o.main(o.kt)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.POIXMLDocumentPart
Caused by: java.lang.ClassNotFoundException: org.apache.poi.POIXMLDocumentPart

    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 12 more

有人可以解释什么是错的吗?

标签: javapdfkotlinapache-poiconverters

解决方案


推荐阅读