如何使用java从Microsoft文档创建预览图像

nil*_*lez 5 java pdf-generation image ms-office

目前,我正在处理 Microsoft 文档:Word(doc、docx)、Powerpoint(ppt、pptx)和 Excel(xls、xlsx)

我想从第一页创建预览图像。

Apache-poi 库只能完成 PowerPoint 文档。

但我找不到其他类型的解决方案。

我有一个想法将文档转换为 pdf (1) 并转换为图像 (2) 。

对于步骤 2(将 pdf 转换为图像),有许多免费的 java 库,例如 PDFBox。它与我的虚拟 pdf 文件配合得很好

但是,我在步骤 1 中遇到了问题

在我的文档中,它可能包含具有多种样式的文本、表格、图像或对象。Word 文档第一页的示例图像:

Word 文档第一页的示例图像

哪个开源java库可以完成这个任务?

我尝试使用以下库来实现:

JODConverter - 输出看起来不错,但它需要 OpenOffice。

docx4j - 我不确定它是否可以使用非 ooxml 格式(doc、xls、ppt)并且它真的免费吗?以下是示例代码:

String inputWordPath = "C:\\Users\\test\\Desktop\\TestPDF\\Docx.docx";
String outputPDFPath = "C:\\Users\\test\\Desktop\\TestPDF\\OutDocx4j.pdf";
try {
    InputStream is = new FileInputStream(new File(inputWordPath));
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(is);
    Mapper fontMapper = new IdentityPlusMapper();
    wordMLPackage.setFontMapper(fontMapper);
    Docx4J.toPDF(wordMLPackage, new FileOutputStream(new File(outputPDFPath)));
} catch (Exception e) {
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

输出看起来不错,但在生成的 pdf 中包含“ ##评估仅使用##”。

xdocreport - 生成的 pdf 不包含图像。

String inputWordPath = "C:\\Users\\test\\Desktop\\TestPDF\\Docx.docx";
String outputPDFPath = "C:\\Users\\test\\Desktop\\TestPDF\\OutXDOCReport.pdf";
InputStream is = new FileInputStream(new File(inputWordPath));
XWPFDocument document = new XWPFDocument(is);
PdfOptions options = PdfOptions.create();
OutputStream out = new FileOutputStream(new File(outputPDFPath));
PdfConverter.getInstance().convert(document, out, options);
Run Code Online (Sandbox Code Playgroud)

我找不到适合该任务的库。

  • 您有什么建议吗?

  • 我可以直接将文档(docx、doc、xlsx、xls)转换为图像吗?

  • docx4j 的转换功能真的免费吗?

  • 如何从生成的pdf(由docx4j)中删除“ ##评估仅使用##”?

  • docx4j 可以处理非 ooxml 文档吗?

  • 我可以只将第一页转换为 pdf 吗?

  • 我可以设置 pdf 的大小以适应转换后的文档内容吗?

  • 是否有任何库和示例代码可以将文档转换为 pdf 或将文档转换为图像?

sbr*_*ier 4

如果您有能力安装 LibreOffice(或 Apache OpenOffice),JODConverter 应该可以很好地解决问题(而且是免费的)。

\n\n

请注意,Maven 中央存储库中提供的最新版本的 JODConverter提供了一项称为过滤器的功能 that would allow you to convert only the first page easily, and it supports conversion to PNG out of the box. Here\'s a quick example on how to do so:

\n\n
// Create an office manager using the default configuration.\n// The default port is 2002. Note that when an office manager\n// is installed, it will be the one used by default when\n// a converter is created.\nfinal LocalOfficeManager officeManager = LocalOfficeManager.install(); \ntry {\n\n    // Start an office process and connect to the started instance (on port 2002).\n    officeManager.start();\n\n    final File inputFile = new File("document.docx");\n    final File outputFile = new File("document.png");\n\n    // Create a page selector filter in order to\n    // convert only the first page.\n    final PageSelectorFilter selectorFilter = new PageSelectorFilter(1);\n\n    LocalConverter\n      .builder()\n      .filterChain(selectorFilter)\n      .build()\n      .convert(inputFile)\n      .to(outputFile)\n      .execute();\n}\xc2\xa0finally {\n    // Stop the office process\n    LocalOfficeUtils.stopQuietly(officeManager);\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

至于你的问题

\n\n
\n

我可以设置 pdf 的大小以适应转换后的文档内容吗

\n
\n\n

如果您可以在不使用 JODConverter 的情况下使用 LibreOffice 或 Apache OpenOffice 来完成此操作,那么您也可以使用 JODConverter 来完成此操作。您只需了解如何以编程方式完成此操作,然后创建一个与 JODConverter 一起使用的过滤器。

\n\n

我不会在这里详细介绍,因为您可以选择其他方式,但如果您需要进一步的帮助,只需在Gitter 社区上询问即可 of the project.

\n