下面的代码不适用于Apache poi 3.16。有人能提供正确的解决方案吗,在我的项目中有一些依赖只供使用
public void ConvertToPDF(String docPath, String pdfPath) {
try {
InputStream doc = new FileInputStream(new File(docPath));
XWPFDocument document = new XWPFDocument(doc);
PdfOptions options = PdfOptions.create();
OutputStream out = new FileOutputStream(new File(pdfPath));
PdfConverter.getInstance().convert(document, out, options);
System.out.println("Done");
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
字符串
例外情况:
Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: org.apache.poi.POIXMLDocumentPart.getPackageRelationship()Lorg/apache/poi/openxml4j/opc/PackageRelationship;
at org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument.getFontsDocument(XWPFStylesDocument.java:1479)
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 recall.wordEditor.converter(recall_word.java:395)
at recall.wordEditor.process(recall_word.java:379)
at recall.wordEditor$5.actionPerformed(recall_word.java:194)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
型
4条答案
按热度按时间vu8f3i0k1#
这样做的主要问题是那些
PdfOptions
和PdfConverter
不是apache poi
项目的一部分。它们是由opensagres
开发的,第一个版本被命名为org.apache.poi.xwpf.converter.pdf.PdfOptions
和org.apache.poi.xwpf.converter.pdf.PdfConverter
。这些旧类自2014年以来没有更新,需要使用apache poi
的3.9
版本。请使用最新的fr.opensagres.poi.xwpf.converter.pdf,它可以使用最新的稳定版本
apache poi 3.17
。那就做吧
字符串
2018年10月:此代码使用
apache poi 3.17
工作。由于apache poi
中的更改,它无法使用apache poi 4.0.0
工作,直到现在fr.opensagres.poi.xwpf.converter
还没有考虑到这些更改。2019年2月:我现在使用最新的
apache poi
版本4.0.1
和最新版本2.0.2
的fr.opensagres.poi.xwpf.converter.pdf和配偶。2021年6月:使用
apache poi
版本4.1.2
和fr.opensagres.poi.xwpf.converter.pdf和consorts的最新版本2.0.2
工作。无法使用apache poi
版本5.0.0
工作,因为XDocReport
需要ooxml-schemas
,而apache poi 5
不再支持。2022年4月:使用
apache poi
版本5.2.2
和fr.opensagres.poi.xwpf.converter.pdf和consorts的最新版本2.0.3
工作。2023年7月:使用
apache poi
版本5.2.3
和fr.opensagres.poi.xwpf.converter.pdf和consorts的最新版本2.0.4
工作。qzlgjiam2#
fr.opensagres.poi.xwpf.converter.core的新版本2.0.2可以与apachepoi4.0.1和itext2.17一起运行您只需要在Maven中添加以下依赖项,然后Maven将自动下载所有依赖项。(更新了您的Maven项目,因此它下载了所有这些库及其所有依赖项)
字符串
bakd9h0s3#
2021年6月:使用apache poi版本4.1.2和最新版本2.0.2的fr.opensagres.poi.xwpf.converter.core和consorts。无法使用apache poi版本5.0.0工作,因为XDocReport需要ooxml-schemas,而apache poi 5不再支持。
ooxml-schemas已替换为poi-ooxml-full。
https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-full/5.0.0
但它不适用于fr.opensagres.poi.xwpf.converter.core 2.0.2,因为它与apache-poi 5.0.0中包含的新版本CTStyle不兼容。
csbfibhn4#
刚刚添加到POX.xml
字符串