将word转换为pdf java [英] Convert word to pdf java
问题描述
我正在尝试将word转换为pdf,我的代码是:
I'm trying convert word to pdf, my code is:
public static void main(String[] args) {
try {
XWPFDocument document = new XWPFDocument();
document.createStyles();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun title = paragraph.createRun();
title.setText("gLETS GO");
PdfOptions options = PdfOptions.create();
OutputStream out = new FileOutputStream(new File("C:/Users/pepe/Desktop/DocxToPdf1.pdf"));
PdfConverter.getInstance().convert(document, out, options);
System.out.println("Done");
} catch (Exception e) {
e.printStackTrace();
}
}
我遇到错误:
fr.opensagres.poi.xwpf.converter.core.XWPFConverterException: org.apache.xmlbeans.XmlException: error: Unexpected end of file after null
at fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:71)
at fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:39)
Caused by: org.apache.xmlbeans.XmlException: error: Unexpected end of file
我尝试了其他解决方案,但没有用.如果有人可以帮助我或其他方式,我会创建一个Java项目
I have tried other solutions but doesnt works. I create a java project, if someone can help me or other way to do
推荐答案
This is probably a duplicate of Trying to make simple PDF document with Apache poi. But let's have a complete example again to show how to create a new XWPFDocument
from scratch using the latest apache poi 4.1.2
which then can be converted to PDF
using PdfConverter
of fr.opensagres.poi.xwpf.converter
version 2.0.2
and iText
.
如所告知的,由apache poi
创建的默认*.docx
文档缺少某些PdfConverter
所需的内容.
As told the default *.docx
documents created by apache poi
lacks some content which PdfConverter
needs.
必须有一个样式文档,即使该文档为空.
There must be a styles document, even if it is empty.
并且必须至少具有设置页面大小的页面部分属性.为了实现这一点,我们必须在程序中另外添加一些代码.不幸的是,这需要所有架构ooxml-schemas-1.4.jar
的完整jar,如常见问题解答N10025 .
And there must be section properties for the page having at least the page size set. To fulfilling this we must add some code additionally in our program. Unfortunately this then needs the full jar of all of the schemas ooxml-schemas-1.4.jar
as mentioned in Faq-N10025.
由于我们需要更改底层的底层对象,因此必须编写文档,以便提交底层的对象.否则,我们交给PdfConverter
的XWPFDocument
将是不完整的.
And because we need changing the underlying low level objects, the document must be written so underlying objects will be committed. Else the XWPFDocument
which we hand over the PdfConverter
will be incomplete.
最小的完整工作示例:
import java.io.*;
import java.math.BigInteger;
//needed jars: fr.opensagres.poi.xwpf.converter.core-2.0.2.jar,
// fr.opensagres.poi.xwpf.converter.pdf-2.0.2.jar,
// fr.opensagres.xdocreport.itext.extension-2.0.2.jar,
// itext-4.2.1.jar
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
//needed jars: apache poi and it's dependencies
// and additionally: ooxml-schemas-1.4.jar
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.util.Units;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
public class XWPFToPDFConverterSampleMin {
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
// there must be a styles document, even if it is empty
XWPFStyles styles = document.createStyles();
// there must be section properties for the page having at least the page size set
CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
CTPageSz pageSz = sectPr.addNewPgSz();
pageSz.setW(BigInteger.valueOf(12240)); //12240 Twips = 12240/20 = 612 pt = 612/72 = 8.5"
pageSz.setH(BigInteger.valueOf(15840)); //15840 Twips = 15840/20 = 792 pt = 792/72 = 11"
// filling the body
XWPFParagraph paragraph = document.createParagraph();
XWPFRun title = paragraph.createRun();
title.setText("gLETS GO");
//document must be written so underlaaying objects will be committed
ByteArrayOutputStream out = new ByteArrayOutputStream();
document.write(out);
document.close();
document = new XWPFDocument(new ByteArrayInputStream(out.toByteArray()));
PdfOptions options = PdfOptions.create();
PdfConverter converter = (PdfConverter)PdfConverter.getInstance();
converter.convert(document, new FileOutputStream("XWPFToPDFConverterSampleMin.pdf"), options);
document.close();
}
}
这篇关于将word转换为pdf java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!