java - 将 .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
有人可以解释什么是错的吗?
解决方案
推荐阅读
- google-cloud-platform - 服务帐户具有“计算管理员”权限,但错误提示需要 compute.instances.list
- javascript - 将文件上传到 laravel 服务器错误
- apache-kafka - 删除未使用的 Kafka 分区
- c# - 如何正确使用 ResidentArrays?
- windows - 使用主机:端口(端口映射)(从 Windows 到 Windows)通过 Socket 从主机连接到容器
- apache-spark - 什么是随机分区?
- c++ - Qt 信号槽与接收器和 lambda 的一次性连接
- ansible - ansible的wait_for和wait_for_connection的区别
- c# - 像这样的参数的等效参数匹配器是什么 - “表达式
> 谓词)”在 NSubsitute 中? - url - 在地址栏中手动写入 URL 时,页面中出现错误 404