问题
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