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.