java pdf版本转换工具,Java中的Docx至Pdf转换器

The below code is not working with Apache poi 3.16.

Can someone provide with the correct solution, in my project there are some dependency for using only

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:

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.(XWPFStylesDocument.java:190)

at org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument.(XWPFStylesDocument.java:184)

at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.createStylesDocument(XWPFDocumentVisitor.java:166)

at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.(XWPFDocumentVisitor.java:159)

at org.apache.poi.xwpf.converter.pdf.internal.PdfMapper.(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)

解决方案

The main problem with this is that those PdfOptions and PdfConverter are not part of the apache poi project. They are developed by opensagres and first versions were badly named org.apache.poi.xwpf.converter.pdf.PdfOptions and org.apache.poi.xwpf.converter.pdf.PdfConverter. Those old classes were not updated since 2014 and needs version 3.9 of apache poi to be used.

Do using the much more current fr.opensagres.poi.xwpf.converter.pdf, which works using the latest stable release apache poi 3.17.

Then do

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();

}

}

October 2018:

This code works using apache poi 3.17. It cannot work using apache poi 4.0.0 due to changings in apache poi which were not taken in account in fr.opensagres.poi.xwpf.converter until now.

February 2019:

Works for me now using the newest apache poi version 4.0.1 and the newest version 2.0.2 of fr.opensagres.poi.xwpf.converter.core and consorts.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值