I am having an issue when using Apache POI to create a DOC file, then convert to PDF.
Using Apache POI to create a DOC file works well, it outputs a DOC with text, tables, imagery and shaded headers without problems. If I use IText to create the PDF this also works.
I am trying to reduce development and change time by creating a DOC then converting this to PDF so that I do not have two code bases to manage.
When I convert the DOC to PDF using Apache POI converter, I got the Style Error.
org.apache.poi.xwpf.converter.core.XWPFConverterException: java.lang.IllegalStateException: Expecting one Styles document part, but found 0
I add the style in the code. but then get this error:
I also get a null pointer exception relating to the headers, error below: ERROR CODE
org.apache.poi.xwpf.converter.core.XWPFConverterException: java.lang.NullPointerException
at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:70) ~[org.apache.poi.xwpf.converter.pdf-1.0.6.jar:1.0.6]
at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:38) ~[org.apache.poi.xwpf.converter.pdf-1.0.6.jar:1.0.6]
at org.apache.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:45) ~[org.apache.poi.xwpf.converter.core-1.0.6.jar:1.0.6]
at com.imviewer.report.DocxReportBuilder311CUSTOM.ConvertToPDF(DocxReportBuilder311CUSTOM.java:878) [classes/:na]
at com.imviewer.report.DocxReportBuilder311CUSTOM.doInBackground(DocxReportBuilder311CUSTOM.java:833) [classes/:na]
at com.imviewer.report.DocxReportBuilder311CUSTOM.doInBackground(DocxReportBuilder311CUSTOM.java:1) [classes/:na]
at javax.swing.SwingWorker$1.call(Unknown Source) [na:1.8.0_161]
at java.util.concurrent.FutureTask.run(Unknown Source) [na:1.8.0_161]
at javax.swing.SwingWorker.run(Unknown Source) [na:1.8.0_161]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_161]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_161]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_161]
Caused by: java.lang.NullPointerException: null
at org.apache.poi.xwpf.converter.core.utils.XWPFTableUtil.getGridColList(XWPFTableUtil.java:183) ~[org.apache.poi.xwpf.converter.core-1.0.6.jar:1.0.6]
at org.apache.poi.xwpf.converter.core.utils.XWPFTableUtil.computeColWidths(XWPFTableUtil.java:116) ~[org.apache.poi.xwpf.converter.core-1.0.6.jar:1.0.6]
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitTable(XWPFDocumentVisitor.java:867) ~[org.apache.poi.xwpf.converter.core-1.0.6.jar:1.0.6]
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitBodyElements(XWPFDocumentVisitor.java:251) ~[org.apache.poi.xwpf.converter.core-1.0.6.jar:1.0.6]
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.start(XWPFDocumentVisitor.java:199) ~[org.apache.poi.xwpf.converter.core-1.0.6.jar:1.0.6]
at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:56) ~[org.apache.poi.xwpf.converter.pdf-1.0.6.jar:1.0.6]
... 11 common frames omitted
Then I have tried manually converting the DOC to PDF using Apache POI in a separate piece of code the text is output and tables/imagery appear to be in the correct place but no table lines show (although the spacing appears correct). However, if I open the created DOC, remove the table formatting, then re-apply, then convert the DOC to PDF, the lines appear.
It seems that the lines which show in the DOC created by Apache POI will not convert or show in a PDF converted by Apache - but, if we manually add table formatting it does show.
Can anyone help please?
public static void ConvertToPDF(File docPath, File pdfPath) {
try {
InputStream doc = new FileInputStream(docPath);
XWPFDocument document = new XWPFDocument(doc);
PdfOptions options = PdfOptions.create();
OutputStream out = new FileOutputStream(pdfPath);
PdfConverter.getInstance().convert(document, out, options);
System.out.println("Completed");
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}