1

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

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Geeeee
  • 31
  • 1
  • 5
  • 1
    The `fr.opensagres.poi.xwpf.converter.` version `2.0.1` works using `apache poi 3.x`. Using `apache poi 4.x`, `fr.opensagres.poi.xwpf.converter.` version `2.0.2` is needed. See https://stackoverflow.com/questions/51440312/docx-to-pdf-converter-in-java/51440649#51440649. – Axel Richter Feb 01 '20 at 10:46
  • thank you . Upgrading to version 2.0.2 of maven dependency solved the problem but now I am getting this error when opening the pdf. image attached above – Geeeee Feb 01 '20 at 11:01
  • According the error "Unable to open the file. A sharing violation has occurred.", the file is opened already using exclusive locking. Something locks the created `PDF` file. Make sure you closed the `FileOutputStream out`. – Axel Richter Feb 01 '20 at 11:11
  • yes . outputstream and other xwpf documents are closed. when I closed the IDE to test the file, it became invalid. – Geeeee Feb 01 '20 at 11:24
  • upon building . I got the error above . updated image – Geeeee Feb 01 '20 at 11:25
  • 2
    This is a total different question now. I recommend deletion this question and asking a new one about the new issue. But please do not provide code or stack traces as pictures. And please provide all necessary code. What is `MainApp.mergeDocument` doing? I suspect it simply concatenates multiple `Word` document bodies. This is not correct according the specs of `Office Open XML`. `MS Word` will tolerate this but other libraries which are following the specs of `Office Open XML` might not tolerate this. – Axel Richter Feb 01 '20 at 11:43
  • the main goal of the application is to merge all documents from a specific folder and convert it to pdf file. that is the main function of mergeDocument – Geeeee Feb 01 '20 at 11:52
  • but for now. I am trying to convert a single word document to a pdf file for testing purposes before leaping to next step – Geeeee Feb 01 '20 at 11:53
  • 1
    Please provide [a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Without that nobody will be able reproducing your issue. In general the code of https://stackoverflow.com/questions/51440312/docx-to-pdf-converter-in-java/51440649#51440649 works. – Axel Richter Feb 01 '20 at 11:55
  • please see this - I tried to make it as clear as I could hoping I could deliver the point. - https://stackoverflow.com/questions/60017210/merging-two-document-and-convert-to-pdf-apache-poi – Geeeee Feb 01 '20 at 13:16
  • @Axel Richter: thank you, it works! – hieunt89 May 18 '21 at 02:31

0 Answers0