Apache POI - convert word to pdf

不羁岁月 提交于 2020-07-22 22:10:26

问题


I am trying to convert XWPFDocument to PDF but getting an error. I am using - https://mvnrepository.com/artifact/fr.opensagres.xdocreport/fr.opensagres.poi.xwpf.converter.pdf/2.0.1.

<!-- https://mvnrepository.com/artifact/fr.opensagres.xdocreport     /fr.opensagres.poi.xwpf.converter.pdf -->
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.poi.xwpf.converter.pdf</artifactId>
<version>2.0.1</version>
</dependency>

Code:

   FileInputStream fis   = new FileInputStream("1.docx");

   XWPFDocument xdoc = new XWPFDocument(OPCPackage.open(fis));
        XWPFParagraph paragraph = xdoc.createParagraph();
        XWPFRun run = paragraph.createRun();
        run.setFontFamily("Arial");
        run.setText("this is a test");

   FileOutputStream out = new FileOutputStream( new File(dir + filename));          
        PdfOptions options = PdfOptions.create();
        // 3) Convert XWPFDocument to Pdf
        PdfConverter.getInstance().convert(xdoc, out, options);

I am getting this error. does anybody knows why?

fr.opensagres.poi.xwpf.converter.core.XWPFConverterException: java.lang.ClassCastException: org.apache.poi.ooxml.POIXMLDocumentPart cannot be cast to org.apache.poi.POIXMLDocumentPart
at fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:71)
at fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:39)
at fr.opensagres.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:46)

Upgrading to version 2.0.2 of maven dependency solved the problem but now I am getting this error when opening the pdf enter image description here

stacktrace

来源:https://stackoverflow.com/questions/60016101/apache-poi-convert-word-to-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!