Java中的Docx至Pdf转换器

时间:2018-07-20 10:17:07

标签: java apache pdf apache-poi converter

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

3 个答案:

答案 0 :(得分:4)

与此相关的主要问题是,那些PdfOptionsPdfConverter不是apache poi项目的一部分。它们是由opensagres开发的,第一个版本的命名分别为org.apache.poi.xwpf.converter.pdf.PdfOptionsorg.apache.poi.xwpf.converter.pdf.PdfConverter。这些旧类自2014年以来就没有更新,需要使用3.9的{​​{1}}版本。

使用最新得多的fr.opensagres.poi.xwpf.converter.pdf,它可以使用最新的稳定发行版apache poi

然后做

apache poi 3.17

2018年10月: 此代码使用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(); } } 起作用。由于apache poi 3.17中的更改(到目前为止尚未在apache poi 4.0.0中考虑到),因此无法使用apache poi


2019年2月: 现在,我可以使用fr.opensagres.poi.xwpf.converter.core的最新fr.opensagres.poi.xwpf.converter版本apache poi和最新的4.0.1版本以及配套软件为我工作。

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

只需添加到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>