Apache POI - DOCX 到 PDF 转换

Apache POI - DOCX To PDF Conversion

我正在尝试使用 POI 将 docx 文件转换为 pdf 文件。出现以下错误。 使用 poi-3.17 ,

  import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import org.apache.poi.xwpf.converter.pdf.PdfConverter;
    import org.apache.poi.xwpf.converter.pdf.PdfOptions;
    import org.apache.poi.xwpf.usermodel.XWPFDocument;
    
    public class WordToPDF {
    
        public static void main(String[] args) {
            WordToPDF cwoWord = new WordToPDF();
            System.out.println("Start");
            cwoWord.ConvertToPDF("D:\2067536.docx", "D:\2067536.pdf");
        }
    
        public void ConvertToPDF(String docPath, String pdfPath) {
            try {
                InputStream doc = new FileInputStream(new File(docPath));
                XWPFDocument document = new XWPFDocument(doc);
                document.createStyles();
                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 in thread "main" org.apache.poi.xwpf.converter.core.XWPFConverterException: java.lang.NullPointerException
        at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:70)
        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 WordToPDF.ConvertToPDF(WordToPDF.java:27)
        at WordToPDF.main(WordToPDF.java:17)
Caused by: java.lang.NullPointerException
        at org.apache.poi.xwpf.converter.pdf.internal.PdfMapper.visitHeader(PdfMapper.java:178)
        at org.apache.poi.xwpf.converter.pdf.internal.PdfMapper.visitHeader(PdfMapper.java:111)
        at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitHeaderRef(XWPFDocumentVisitor.java:1142)
        at org.apache.poi.xwpf.converter.core.MasterPageManager.visitHeadersFooters(MasterPageManager.java:213)
        at org.apache.poi.xwpf.converter.core.MasterPageManager.addSection(MasterPageManager.java:180)
        at org.apache.poi.xwpf.converter.core.MasterPageManager.compute(MasterPageManager.java:127)
        at org.apache.poi.xwpf.converter.core.MasterPageManager.initialize(MasterPageManager.java:90)
        at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitBodyElements(XWPFDocumentVisitor.java:232)
        at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.start(XWPFDocumentVisitor.java:199)
        at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:56)
        ... 4 more

由于这是一个空指针错误,我无法理解到底是什么问题,如有任何帮助,我们将不胜感激。谢谢。

Libre Office 救了我的命,一个简单的 docx 到 pdf 转换命令非常有效。

详细答案在这里