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

标签 java image pdf-generation ms-office

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

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

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

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

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

对于第 2 步(将 pdf 转换为图像),有许多免费的 java 库,例如PDF盒子。它适用于我的虚拟 pdf 文件

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

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

Sample image from first page of word document

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

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

JOD转换器 - 输出看起来不错,但它需要 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();
}

输出看起来不错,但它在生成的 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);

我找不到适合该任务的库。
  • 你有什么建议吗?
  • 我可以直接将文档(docx、doc、xlsx、xls)转换为图像吗?
  • docx4j 的转换功能真的免费吗?
  • 如何从生成的pdf(通过docx4j)中删除“##评估仅使用##”?
  • docx4j 可以与非 ooxml 文档一起使用吗?
  • 我可以只将第一页转换为 pdf 吗?
  • 我可以设置 pdf 的大小以适应转换后的文档内容吗?
  • 是否有任何库和示例代码可以将文档转换为 pdf 或将文档转换为图像?
  • 最佳答案

    如果您负担得起安装 LibreOffice(或 Apache OpenOffice)的费用,JODConverter 应该可以很好地解决问题(而且是免费的)。

    请注意 the latest version of JODConverter在 Maven 中央存储库中提供了一个功能,称为 Filters这将允许您轻松地仅转换第一页,并且它支持开箱即用地转换为 PNG。这是一个关于如何执行此操作的快速示例:

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

    至于你的问题

    Can I set size of pdf to fit with converted document content



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

    我不会在这里详细介绍,因为您可以选择其他方式,但如果您需要进一步的帮助,请联系 Gitter Community的项目。

    关于java - 如何使用 java 从 Microsoft 文档创建预览图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47509676/

    相关文章:

    java - 充气城堡 : Detached Enveloped Signature Changes at Every Run

    Java字符串替换

    java - 类型转换 "with"接口(interface)

    c++ - 如何将灰度图像复制到 OpenCV 中的 RGB 图像红色 channel ?

    python - 如何使用 Python OpenCV 查找表格图像中的行数和列数?

    python - 在python中将excel转换为pdf

    java - 如果 NVIDIA 控制面板设置计算优化,cuCtxCreate 会失败

    java - 使用 PaintComponent Java 绘制图像

    html - 是@page :last really something?

    java - 为什么当通过 Web 服务同时创建文档时 libreoffice sdk 会崩溃?