使用XWPFDocument将Word转换为PDF

问题描述

我正在尝试使用XWPFDocument将docx文件转换为PDF文件。 正在创建文件,但格式不正确。

我的word文档包含带有图像和表格的标题,带有表格的正文部分以及带有表格的页脚部分。

下面是我的代码

InputStream doc = new FileInputStream(new File(inputFile));
XWPFDocument document = new XWPFDocument(doc);
PdfOptions options = PdfOptions.create();
OutputStream out = new FileOutputStream(new File(outputpdfFile));
PdfConverter.getInstance().convert(document,out,options); 

使用该代码,我得到以下错误

Aug 20,2020 5:37:18 PM fr.opensagres.poi.xwpf.converter.pdf.internal.PdfMapper visitPicture SEVERE: The byte array is not a recognized imageformat.
    
Aug 20,2020 5:37:19 PM fr.opensagres.poi.xwpf.converter.pdf.internal.PdfMapper visitPicture SEVERE: The byte array is not a recognized imageformat.
    
fr.opensagres.poi.xwpf.converter.core.XWPFConverterException: java.lang.RuntimeException: Not all annotations Could be added to the document (the document doesn't have enough pages).  at fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:71)    at fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:39)    at fr.opensagres.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:46)   at pdf.convertpdf.ConvertIntoPDF.main(ConvertIntoPDF.java:49)
    
Caused by: java.lang.RuntimeException: Not all annotations Could be added to the document (the document doesn't have enough pages).     at com.lowagie.text.pdf.PdfDocument.close(UnkNown Source)   at com.lowagie.text.Document.close(UnkNown Source)  at  fr.opensagres.poi.xwpf.converter.pdf.internal.elements.StylableDocument.close(StylableDocument.java:180)    at fr.opensagres.poi.xwpf.converter.pdf.internal.PdfMapper.endVisitDocument(PdfMapper.java:177)     at fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.start(XWPFDocumentVisitor.java:217)    at fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:57)    ... 3 more

请给我建议解决方案。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)