0

I have been searching and testing codes for a whole week now and trying many libraries, and many more things. It seems like the current answers on google and on Stackoverflow are out of date due to the updates they do on apache poi, and other libraries.

I have been trying to convert DOCX to PDF, with images and also to maintain the same format and layout as displayed in the DOCX.

I have tried this following code, which was botained from here:

    String inputFile="Test.docx";
    String outputFile="Tester.pdf";

    System.out.println("inputFile:" + inputFile + ",outputFile:"+ outputFile);
    FileInputStream in=new FileInputStream(inputFile);
    XWPFDocument document=new XWPFDocument(in);
    File outFile=new File(outputFile);
    OutputStream out=new FileOutputStream(outFile);
    PdfOptions options=null;
    PdfConverter.getInstance().convert(document,out,options);

I am also using the following Libraries:

com.lowagie.text-.2.1.7.jar
commons-codec-1.10.jar
commons-collections4-4.2.jar
commons-compress-1.18.jar
curvesapi-1.04.jar
fr.opensagres.xdocreport.itext.extension-2.0.1.jar
org.apache.poi.xwpf.converter.core-1.0.6.jar
org.apache.poi.xwpf.converter.pdf-1.0.6.jar
poi-4.0.0.jar
poi-ooxml-4.0.0.jar
poi-ooxml-schemas-4.0.0.jar
xmlbeans-3.0.1.jar

Getting the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/POIXMLDocumentPart
at org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument.getFontsDocument(XWPFStylesDocument.java:1477)
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)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.POIXMLDocumentPart

Either I the issue is in front of me and I am not seeing it.. otherwise, I am not sure what is going on. I have researched the above error and it claims to be apache poi different versions are trying to communicate with each other, but I have downloaded all the latest libraries for everything.

Tiger
  • 39
  • 2
  • 9
  • 1
    `com.lowagie.text-.2.1.7.jar` is definitely out of date. iText 2.1.7 was released on Jul 7, 2009, as can be seen on Github: https://github.com/itext/itextpdf/releases/2.1.7 and on the iText website: https://itextpdf.com/changelog/217. The latest version of iText is 7.1.3, and it is not backwards compatible with 2.1.7. – Amedee Van Gasse Oct 09 '18 at 11:14
  • Ok, so I have updated that to Itext 7.1.3 and included all of its dependencies. The error I am getting now is: Exception in thread "main" java.lang.NoClassDefFoundError: com/lowagie/text/Element. As I searched for it, Google will direct me to the com.lowagie.text-.2.1.7.jar – Tiger Oct 09 '18 at 11:30
  • There is nothing which works together with `apache poi 4.0.0` but the following both examples are working using `apache poi 3.17`: https://stackoverflow.com/questions/51440312/docx-to-pdf-converter-in-java/51440649#51440649, https://stackoverflow.com/questions/51330192/trying-to-make-simple-pdf-document-with-apache-poi/51337157#51337157 – Axel Richter Oct 09 '18 at 11:43
  • It still kept on asking for other libraries which I did till I reached the following error: Exception in thread "main" java.lang.NoSuchMethodError: com.lowagie.text.pdf.PdfPRow.getCells()[Lcom/lowagie/text/pdf/PdfPCell; – Tiger Oct 09 '18 at 14:19
  • My code in both the examples is complete and works using `apache poi 3.17` and only using the libraries I have mentioned in the code. – Axel Richter Oct 09 '18 at 16:11
  • Got it working finally!! this exmaple https://stackoverflow.com/questions/51440312/docx-to-pdf-converter-in-java/51440649#51440649 --- was missing libraries from this exmaple: https://stackoverflow.com/questions/51330192/trying-to-make-simple-pdf-document-with-apache-poi/51337157#51337157 , I might be wrong but that is how I managed to get it working. Thank you so much again, life saver!!!! – Tiger Oct 10 '18 at 09:42

0 Answers0