1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > java_poi教程.pdf 如何使用POI转换.DOC / .DOCX为PDF在Java ..?

java_poi教程.pdf 如何使用POI转换.DOC / .DOCX为PDF在Java ..?

时间:2020-05-19 08:23:58

相关推荐

java_poi教程.pdf 如何使用POI转换.DOC / .DOCX为PDF在Java ..?

how to convert ms-document to PDF, is there any example pls share

with me.. thanks.

解决方案

If you are requiered to use POI i guess you should take a look at org.apache.poi.hwpf.converter

I never tried this, but i guess it´s worth a try atleast.

It seems like you can use WordToFoConverterto convert your XWPFDocument to a FO-file (example here).

From there you can use apaches FOP to transform the FO-file to a PDF like this:

// Step 1: Construct a FopFactory

// (reuse if you plan to render multiple documents!)

FopFactory fopFactory = FopFactory.newInstance();

// Step 2: Set up output stream.

// Note: Using BufferedOutputStream for performance reasons (helpful with FileOutputStreams).

OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("C:/Temp/myfile.pdf")));

try {

// Step 3: Construct fop with desired output format

Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

// Step 4: Setup JAXP using identity transformer

TransformerFactory factory = TransformerFactory.newInstance();

Transformer transformer = factory.newTransformer(); // identity transformer

// Step 5: Setup input and output for XSLT transformation

// Setup input stream

Source src = new StreamSource(new File("C:/Temp/myfile.fo"));

// Resulting SAX events (the generated FO) must be piped through to FOP

Result res = new SAXResult(fop.getDefaultHandler());

// Step 6: Start XSLT transformation and FOP processing

transformer.transform(src, res);

} finally {

//Clean-up

out.close();

}

This Code was taken from /fop/0.95/embedding.html incase you want to read more on this topic.

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。