POI - null 后文件意外结束

POI - Unexpected end of file after null

我在将使用 poi 创建的 docx 文件转换为 pdf 时遇到问题。 我用下一个代码创建文件(只是空文件,但我尝试用 text/images 填充):

   private static void createEmpty() throws IOException{
         XWPFDocument doc = new XWPFDocument();
         FileOutputStream out = new FileOutputStream("empty.docx");
         doc.write(out);
         out.close();
         doc.close();
   }

我得到的文件可以用 Word 2010 打开,看起来不错,但是当我尝试用 POI pdfConverter 将它转换为 pdf 时,出现错误:

org.apache.poi.xwpf.converter.core.XWPFConverterException: org.apache.xmlbeans.XmlException: error: Unexpected end of file after null
   at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:59)
   at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:37)
   at org.apache.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:45)
   at sbt.oot.metrics.pdf.DocxToPdfConverter.create(DocxToPdfConverter.java:44)
   at sbt.oot.metrics.pdf.DocxToPdfConverter.main(DocxToPdfConverter.java:23)
Caused by: org.apache.xmlbeans.XmlException: error: Unexpected end of file after null
   at org.apache.xmlbeans.impl.store.Locale$SaxLoader.load(Locale.java:3471)
   at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1270)
   at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1257)
   at org.apache.xmlbeans.impl.schema.SchemaTypeLoaderBase.parse(SchemaTypeLoaderBase.java:345)
   at org.openxmlformats.schemas.wordprocessingml.x2006.main.StylesDocument$Factory.parse(Unknown Source)
   at org.apache.poi.xwpf.usermodel.XWPFDocument.getStyle(XWPFDocument.java:446)
   at org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument.<init>(XWPFStylesDocument.java:192)
   at org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument.<init>(XWPFStylesDocument.java:186)
   at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.createStylesDocument(XWPFDocumentVisitor.java:161)
   at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.<init>(XWPFDocumentVisitor.java:154)
   at org.apache.poi.xwpf.converter.pdf.internal.PdfMapper.<init>(PdfMapper.java:149)
   at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:54)
   ... 4 more
Caused by: org.xml.sax.SAXParseException; systemId: file:; lineNumber: 1; columnNumber: 1; Unexpected end of file after null
   at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.reportFatalError(Piccolo.java:1038)
   at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.parse(Piccolo.java:723)
   at org.apache.xmlbeans.impl.store.Locale$SaxLoader.load(Locale.java:3439)
   ... 15 more

这是我如何转换它的代码:

// 1) Load docx with POI XWPFDocument
XWPFDocument document = new XWPFDocument(new FileInputStream(new File(docxFile)));
XWPFDocument template = new XWPFDocument(new FileInputStream(new File("Template.dotx")));
XWPFStyles newStyles = document.createStyles();
newStyles.setStyles(template.getStyle());

// 2) Convert POI XWPFDocument 2 PDF with iText
File outFile = new File(pdfFile);
OutputStream out = new FileOutputStream( outFile );
PdfOptions options = null;
PdfConverter.getInstance().convert( document, out, options );

但是,如果我在 word 中打开文件并再次保存 - 我可以毫无问题地进行转换。 有什么建议吗?

我在创建 docx 文件时使用模板解决了这个问题:

XWPFDocument document = new XWPFDocument();
XWPFDocument template = new XWPFDocument(new FileInputStream(new File("Template.dotx")));
XWPFStyles newStyles = document.createStyles();
newStyles.setStyles(template.getStyle());