6

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.<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)
Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
Shelly
  • 183
  • 1
  • 1
  • 11
  • 1
    *is not working* : what do you mean? It throws exceptions? Can you add it to your question, please? – Leviand Jul 20 '18 at 10:17
  • See https://stackoverflow.com/questions/51330192/trying-to-make-simple-pdf-document-with-apache-poi/51337157#51337157 for complete examples which are tested and are working. – Axel Richter Jul 20 '18 at 10:21

4 Answers4

29

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.pdf and consorts.


June 2021: Works using apache poi version 4.1.2 and the newest version 2.0.2 of fr.opensagres.poi.xwpf.converter.pdf and consorts. Cannot work using apache poi version 5.0.0 because XDocReport needs ooxml-schemas which apache poi 5 does not support anymore.


April 2022: Works using apache poi version 5.2.2 and the newest version 2.0.3 of fr.opensagres.poi.xwpf.converter.pdf and consorts.


July 2023: Works using apache poi version 5.2.3 and the newest version 2.0.4 of fr.opensagres.poi.xwpf.converter.pdf and consorts.

Axel Richter
  • 56,077
  • 6
  • 60
  • 87
  • @Alex Richter Hi can you plz provide a working example if not possible library links i have been trying to make it work for weeks in android studio but i cant get it to work either there duplicate error or exception or no class found or sometimes its ok but class not found in runtime not matter what i do i just cant get it to work i use android studio : arctic fox – Ahsan raza Sep 09 '21 at 06:24
  • @Ahsan raza: The above is a working example using the default `Java` runtime environments. But `android` is **not** the same as `Java` but only an excerpt of `java`. And there are multiple known problems using `apache poi` on `android`, missing `AWT` classes for example. So no, I don't have a solution for `android`. – Axel Richter Sep 09 '21 at 06:39
  • April 2022 version still not working as we need "org.apache.poi.xwpf.converter.pdf" and not "fr.opensagres.poi.xwpf.converter.core ". Using the former one is still giving class not found error (java.lang.NoClassDefFoundError: org/apache/poi/POIXMLDocumentPart) – Anmol Jain Apr 07 '22 at 20:23
  • @Anmol Jain: "as we need `org.apache.poi.xwpf.converter.pdf`": This old version will not work using `apache poi` versions greater than `apache poi 3.9`. And that will never change. How should it? I've clearly pointed in my answer what `opensagres` version work together with what `apache poi` versions. – Axel Richter Apr 08 '22 at 05:20
  • No offense, I was just saying that "fr.opensagres.poi.xwpf.converter.core" does not convert doc to pdf so it is irrelevant to the question posted here. – Anmol Jain Apr 11 '22 at 05:54
  • April 2022: Works using apache poi version 5.2.2 and the newest version 2.0.3 of fr.opensagres.poi.xwpf.converter.core and consorts. it is not working. any help? – Kumaresan Perumal May 24 '22 at 12:21
  • Getting error org.apache.xmlbeans.XmlException: error: The 'namespace-prefix' feature is not supported while the 'namespaces' feature is enabled. @AxelRichter – Mujahid Khan Nov 06 '22 at 07:48
  • A very nice post. However, this POI converter does not handle any non-trivial document. I get failures like `Cannot invoke "org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGrid.getGridColList()" because "grid" is null` or `Cannot invoke "org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageSz.xgetW()" because "pageSize" is null`. Meanwhile, `docx4j` does the export, but lacks header, footer, common styles, failed on some custom fonts, etc. So, at the end of the day, the best solution is still the manual conversion with MS Word. – Dmitriy Popov Nov 28 '22 at 20:15
2

New version 2.0.2 of fr.opensagres.poi.xwpf.converter.core runs with apache poi 4.0.1 and itext 2.17. You just need to add below dependency in Maven and then maven will auto download all dependent dependencies. (Updated your Maven project, so it downloaded all these libraries and all of its dependencies)

<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.poi.xwpf.converter.pdf</artifactId>
    <version>2.0.2</version>
</dependency>
avijitshaw
  • 171
  • 1
  • 3
2

June 2021: Works using apache poi version 4.1.2 and the newest version 2.0.2 of fr.opensagres.poi.xwpf.converter.core and consorts. Cannot work using apache poi version 5.0.0 because XDocReport needs ooxml-schemas which apache poi 5 does not support anymore.

ooxml-schemas has been replaced with poi-ooxml-full.

https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-full/5.0.0

but it doesn't work with fr.opensagres.poi.xwpf.converter.core 2.0.2, because is not compatible with new version of CTStyle which is included in apache-poi 5.0.0.

0

Just addded in 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>
David Buck
  • 3,752
  • 35
  • 31
  • 35
Sagar R G
  • 29
  • 1