将使用poi创建的docx文件转换为pdf时遇到问题.
我使用下一个代码创建文件(只是空文件,但我已尝试填充文本/图像):
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());