apache Java中的Docx到Pdf转换器

fcy6dtqo  于 2023-08-07  发布在  Apache
关注(0)|答案(4)|浏览(323)

下面的代码不适用于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)

vu8f3i0k

vu8f3i0k1#

这样做的主要问题是那些PdfOptionsPdfConverter不是apache poi项目的一部分。它们是由opensagres开发的,第一个版本被命名为org.apache.poi.xwpf.converter.pdf.PdfOptionsorg.apache.poi.xwpf.converter.pdf.PdfConverter。这些旧类自2014年以来没有更新,需要使用apache poi3.9版本。
请使用最新的fr.opensagres.poi.xwpf.converter.pdf,它可以使用最新的稳定版本apache poi 3.17
那就做吧

import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;

//needed jars: fr.opensagres.poi.xwpf.converter.core-2.0.1.jar, 
//             fr.opensagres.poi.xwpf.converter.pdf-2.0.1.jar,
//             fr.opensagres.xdocreport.itext.extension-2.0.1.jar,
//             itext-2.1.7.jar                                  
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;

//needed jars: apache poi and it's dependencies
import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class DOCXToPDFConverterSampleMin {

 public static void main(String[] args) throws Exception {

  String docPath = "./WordDocument.docx";
  String pdfPath = "./WordDocument.pdf";

  InputStream in = new FileInputStream(new File(docPath));
  XWPFDocument document = new XWPFDocument(in);
  PdfOptions options = PdfOptions.create();
  OutputStream out = new FileOutputStream(new File(pdfPath));
  PdfConverter.getInstance().convert(document, out, options);

  document.close();
  out.close();

 }
}

字符串
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.2fr.opensagres.poi.xwpf.converter.pdf和配偶。
2021年6月:使用apache poi版本4.1.2fr.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.2fr.opensagres.poi.xwpf.converter.pdf和consorts的最新版本2.0.3工作。
2023年7月:使用apache poi版本5.2.3fr.opensagres.poi.xwpf.converter.pdf和consorts的最新版本2.0.4工作。

qzlgjiam

qzlgjiam2#

fr.opensagres.poi.xwpf.converter.core的新版本2.0.2可以与apachepoi4.0.1和itext2.17一起运行您只需要在Maven中添加以下依赖项,然后Maven将自动下载所有依赖项。(更新了您的Maven项目,因此它下载了所有这些库及其所有依赖项)

<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.poi.xwpf.converter.pdf</artifactId>
    <version>2.0.2</version>
</dependency>

字符串

bakd9h0s

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不兼容。

csbfibhn

csbfibhn4#

刚刚添加到POX.xml

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.0.0</version>
</dependency>
<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.poi.xwpf.converter.pdf</artifactId>
    <version>2.0.2</version>
</dependency>

字符串

相关问题