/**
* convert to pdf using xdocreport, apache poi and itext extension
* note: not compatible with RTL languages
*/
public static void ConvertToPDFUsingXDocAndPoiAndItext(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);
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
// convert docx to pdf using apache poi
// note: RTL Languages are not supported
public byte[] getPDFByXWPF() {
try {
XWPFDocument document = new XWPFDocument(new ByteArrayInputStream(file.toByteArray()));
PdfOptions options = PdfOptions.create();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PdfConverter.getInstance().convert(document, outputStream, options);
return outputStream.toByteArray();
}catch (IOException e){
return null;
}
}
<!-- Note: not sure if these are all of the dependencies -->
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.itext.extension</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.core</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
<version>1.0.6</version>
</dependency>