docx-to-pdf转换器

9vw9lbht  于 2021-08-20  发布在  Java
关注(0)|答案(3)|浏览(579)

下面的代码不适用于apache poi 3.16。有人能提供正确的解决方案吗?在我的项目中,有一些依赖项只能使用

  1. public void ConvertToPDF(String docPath, String pdfPath) {
  2. try {
  3. InputStream doc = new FileInputStream(new File(docPath));
  4. XWPFDocument document = new XWPFDocument(doc);
  5. PdfOptions options = PdfOptions.create();
  6. OutputStream out = new FileOutputStream(new File(pdfPath));
  7. PdfConverter.getInstance().convert(document, out, options);
  8. System.out.println("Done");
  9. } catch (FileNotFoundException ex) {
  10. System.out.println(ex.getMessage());
  11. } catch (IOException ex) {
  12. System.out.println(ex.getMessage());
  13. }
  14. }

例外情况:

  1. Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: org.apache.poi.POIXMLDocumentPart.getPackageRelationship()Lorg/apache/poi/openxml4j/opc/PackageRelationship;
  2. at org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument.getFontsDocument(XWPFStylesDocument.java:1479)
  3. at org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument.<init>(XWPFStylesDocument.java:190)
  4. at org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument.<init>(XWPFStylesDocument.java:184)
  5. at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.createStylesDocument(XWPFDocumentVisitor.java:166)
  6. at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.<init>(XWPFDocumentVisitor.java:159)
  7. at org.apache.poi.xwpf.converter.pdf.internal.PdfMapper.<init>(PdfMapper.java:149)
  8. at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:55)
  9. at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:38)
  10. at org.apache.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:45)
  11. at recall.wordEditor.converter(recall_word.java:395)
  12. at recall.wordEditor.process(recall_word.java:379)
  13. at recall.wordEditor$5.actionPerformed(recall_word.java:194)
  14. at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
  15. at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
  16. at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
  17. at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
  18. at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
  19. at java.awt.Component.processMouseEvent(Unknown Source)
  20. at javax.swing.JComponent.processMouseEvent(Unknown Source)
  21. at java.awt.Component.processEvent(Unknown Source)
  22. at java.awt.Container.processEvent(Unknown Source)
  23. at java.awt.Component.dispatchEventImpl(Unknown Source)
  24. at java.awt.Container.dispatchEventImpl(Unknown Source)
  25. at java.awt.Component.dispatchEvent(Unknown Source)
  26. at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
  27. at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
  28. at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
  29. at java.awt.Container.dispatchEventImpl(Unknown Source)
  30. at java.awt.Window.dispatchEventImpl(Unknown Source)
  31. at java.awt.Component.dispatchEvent(Unknown Source)
  32. at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
  33. at java.awt.EventQueue.access$500(Unknown Source)
  34. at java.awt.EventQueue$3.run(Unknown Source)
  35. at java.awt.EventQueue$3.run(Unknown Source)
  36. at java.security.AccessController.doPrivileged(Native Method)
  37. at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
  38. at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
  39. at java.awt.EventQueue$4.run(Unknown Source)
  40. at java.awt.EventQueue$4.run(Unknown Source)
  41. at java.security.AccessController.doPrivileged(Native Method)
  42. at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
  43. at java.awt.EventQueue.dispatchEvent(Unknown Source)
  44. at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
  45. at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
  46. at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
  47. at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  48. at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  49. at java.awt.EventDispatchThread.run(Unknown Source)
9cbw7uwe

9cbw7uwe1#

这方面的主要问题是 PdfOptionsPdfConverter 不属于 apache poi 项目它们是由 opensagres 第一个版本的名字很糟糕 org.apache.poi.xwpf.converter.pdf.PdfOptionsorg.apache.poi.xwpf.converter.pdf.PdfConverter . 这些旧类自2014年以来未更新,需要更新版本 3.9 属于 apache poi 待使用。
请使用更流行的fr.opensagres.poi.xwpf.converter.pdf,它使用最新的稳定版本 apache poi 3.17 .
那就做吧

  1. import java.io.InputStream;
  2. import java.io.OutputStream;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.File;
  6. //needed jars: fr.opensagres.poi.xwpf.converter.core-2.0.1.jar,
  7. // fr.opensagres.poi.xwpf.converter.pdf-2.0.1.jar,
  8. // fr.opensagres.xdocreport.itext.extension-2.0.1.jar,
  9. // itext-2.1.7.jar
  10. import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
  11. import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
  12. //needed jars: apache poi and it's dependencies
  13. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  14. public class DOCXToPDFConverterSampleMin {
  15. public static void main(String[] args) throws Exception {
  16. String docPath = "./WordDocument.docx";
  17. String pdfPath = "./WordDocument.pdf";
  18. InputStream in = new FileInputStream(new File(docPath));
  19. XWPFDocument document = new XWPFDocument(in);
  20. PdfOptions options = PdfOptions.create();
  21. OutputStream out = new FileOutputStream(new File(pdfPath));
  22. PdfConverter.getInstance().convert(document, out, options);
  23. document.close();
  24. out.close();
  25. }
  26. }

2018年10月:此代码使用 apache poi 3.17 . 它无法使用 apache poi 4.0.0 由于环境的变化 apache poi 这些都没有被考虑在内 fr.opensagres.poi.xwpf.converter 直到现在。
2019年2月:使用最新的 apache poi 版本 4.0.1 还有最新的版本 2.0.2 由fr.opensagres.poi.xwpf.converter.core和consorts组成。
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 不再支持。

展开查看全部
dgsult0t

dgsult0t2#

fr.opensagres.poi.xwpf.converter.core的新版本2.0.2使用apache poi 4.0.1和itext 2.17运行。您只需要在maven中添加以下依赖项,然后maven将自动下载所有依赖项(更新了maven项目,因此它下载了所有这些库及其所有依赖项)

  1. <dependency>
  2. <groupId>fr.opensagres.xdocreport</groupId>
  3. <artifactId>fr.opensagres.poi.xwpf.converter.pdf</artifactId>
  4. <version>2.0.2</version>
  5. </dependency>
wxclj1h5

wxclj1h53#

刚刚添加到pox.xml中

  1. <dependency>
  2. <groupId>org.apache.poi</groupId>
  3. <artifactId>poi-ooxml</artifactId>
  4. <version>4.0.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>fr.opensagres.xdocreport</groupId>
  8. <artifactId>fr.opensagres.poi.xwpf.converter.pdf</artifactId>
  9. <version>2.0.2</version>
  10. </dependency>

相关问题