如何使用 POI.. 在 java 中将 .doc/.docx 转换为 pdf? [英] How to Convert .doc/.docx to pdf in java using POI..?

查看:57
本文介绍了如何使用 POI.. 在 java 中将 .doc/.docx 转换为 pdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将ms-document转为PDF,有没有例子请分享和我一起……谢谢.

how to convert ms-document to PDF, is there any example pls share with me.. thanks.

推荐答案

如果你需要使用 POI,我想你应该看看 org.apache.poi.hwpf.converter
我从未尝试过这个,但我想至少值得一试.似乎您可以使用 WordToFoConverter 将您的 XWPFDocument 转换为 FO 文件(示例
a.从那里您可以使用 apaches FOP 将 FO 文件转换为 PDF,如下所示:

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();
}

此代码取自 https://xmlgraphics.apache.org/fop/0.95/embedding.html 如果您想阅读有关此主题的更多信息.

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

这篇关于如何使用 POI.. 在 java 中将 .doc/.docx 转换为 pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆