word转图片 java_Java 利用LibreOffice将Office文档转换成 PDF,进而转图片,实现在线预览功能...

本文介绍了如何在Java项目中使用LibreOffice将Office文档转换为PDF,以便实现在线预览功能。通过异步或同步方式,结合JodConverter库,将Office文档转换为PDF,并探讨了转换过程中的优缺点。
摘要由CSDN通过智能技术生成

项目中需要将 Office 文档上传并实现在线预览,用到了 LibreOffice 将 Office 文档转换为 PDF 文档,然后再用 pdfbox 将 PDF 转为图片。

本文介绍借助 LibreOffice 将 Office 文当转换成 PDF 文档。

本文同样适用于 OpenOffice,以下仅以 LibreOffice 为例。

前言为了实现 Office 文档上传并实现在线预览功能,我试了 POI 和收费库 aspose,均不理想。

但是将 PDF 转换为图片有成熟的方案,于是,问题变成了:如何将 Office 文档转换成 PDF。

LibreOffice 可将 Office 文当转换成 PDF文件,并且效果非常棒,和通过微软的 Office 直接另存为 PDF 文件的效果几乎一样。针对在线预览的需求,效果是最好的,也许是最好的解决方案。

不过,前提是需要在服务器上安装 LibreOffice,为了测试,开发环境也需要安装,不过,好的是 LibreOffice 跨平台。 尝试通过 LibreOffice 将 Office 文档直接转换成图片,可惜只得到第一页的内容,查看帮助,没有找到直接转换为图片的方法。

Java 利用 LibreOffice 将 Office 文档转换成 PDF

有两种转换方式,各有优劣,请自行选择。

异步转换

通过调用操作系统命令的方式实现,这个转换是异步的,根据文件的大小需要的时间不确定,如果在上传之后就要立即预览,需要用同步方式。 - 优点:实现方式简单,不需要额外配置信息,不需要添加第三方依赖库(当然 LibreOffice 是必须要安装的)。 - 缺点:发送指令之后,转换是否成功,是否有异常,无法获知,也就是说,转换是否成功,是不确定的。当然,通过严格的测试,一般还是可以保证转换的可靠性的。

同步转换

用到了 JodConverter:https://github.com/sbraconnier/jodconverter - 优点:转换是同步的,转换成功与否是确定的。 - 缺点:代码运行期需要启动 LibreOffice 服务,需要占用操作系统资源,相对于异步转换方式,需要依赖第三方库,需要额外配置信息。

完整代码

添加依赖(仅同步方式需要)

org.jodconverter

jodconverter-local

4.2.4

在 resources 目录添加 libre.properties 文件(仅同步方式需要)

内容如下:

# LibreOffice主目录

libreOfficeHome=C:/dev/LibreOffice6.4

# 开启多个LibreOffice进程,每个端口对应一个进程

# portNumbers=2002,2003

portNumbers=2002

# 任务执行超时为5分钟

taskExecutionTimeoutMinutes=5

# 任务队列超时为1小时

taskQueueTimeoutHours=1

转换类 LibreOfficeUtil

package com.example.demo;

import com.example.factory.OfficeManagerInstance;

import org.jodconverter.JodConverter;

import java.io.File;

public class LibreOfficeUtil {

/*** 利用 JodConverter 将 Offfice 文档转换为 PDF(要依赖 LibreOffice),该转换为同步转换,返回时就已经转换完成*/

public static boolean convertOffice2PDFSyncIsSuccess(File sourceFile, File targetFile) {

try {

OfficeManagerInstance.start();

JodConverter.convert(sourceFile).to(targetFile).execute();

} catch (Exception e) {

e.printStackTrace();

return false;

}

return true;

}

/*** 利用 LibreOffice 将 Office 文档转换成 PDF,该转换是异步的,返回时,转换可能还在进行中,转换是否有异常也未可知* @param filePath 目标文件地址* @param targetFilePath 输出文件夹* @return 子线程执行完毕的返回值*/

public static int convertOffice2PDFAsync(String filePath, String fileName, String targetFilePath) throws Exception {

String command;

int exitStatus;

String osName = System.getProperty("os.name");

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用LibreOfficeJava API来实现LibreOffice文档换为PDF文档。以下是实现的步骤: 1. 首先需要确保LibreOffice已经安装在系统中,并且已经配置好了环境变量。同时需要下载并安装LibreOfficeJava API。 2. 在Java程序中引入LibreOfficeJava API所在的jar包。 3. 使用以下代码将文档换为PDF文档: ``` import com.sun.star.beans.PropertyValue; import com.sun.star.frame.XComponentLoader; import com.sun.star.lang.XComponent; import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.XComponentContext; public class ConvertToPDF { public static void main(String[] args) { XComponentContext xContext = null; try { //获取LibreOffice的上下文 xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); XComponentLoader xLoader = UnoRuntime.queryInterface(XComponentLoader.class, xContext.getServiceManager().createInstanceWithContext("com.sun.star.frame.Desktop", xContext)); //设置待文档的路径和文件名 String fileToConvert = "file:///C:/example.docx"; //设置换后的PDF文档的路径和文件名 String pdfFile = "file:///C:/example.pdf"; //设置换参数 PropertyValue[] conversionProperties = new PropertyValue[2]; conversionProperties[0] = new PropertyValue(); conversionProperties[0].Name = "Hidden"; conversionProperties[0].Value = Boolean.TRUE; conversionProperties[1] = new PropertyValue(); conversionProperties[1].Name = "FilterName"; conversionProperties[1].Value = "writer_pdf_Export"; //打开待文档 XComponent xComponent = xLoader.loadComponentFromURL(fileToConvert, "_blank", 0, conversionProperties); //将文档换为PDF格式 PropertyValue[] storeProperties = new PropertyValue[3]; storeProperties[0] = new PropertyValue(); storeProperties[0].Name = "FilterName"; storeProperties[0].Value = "writer_pdf_Export"; storeProperties[1] = new PropertyValue(); storeProperties[1].Name = "Overwrite"; storeProperties[1].Value = Boolean.TRUE; storeProperties[2] = new PropertyValue(); storeProperties[2].Name = "Hidden"; storeProperties[2].Value = Boolean.TRUE; UnoRuntime.queryInterface(XStorable.class, xComponent).storeToURL(pdfFile, storeProperties); //关闭文档 UnoRuntime.queryInterface(XCloseable.class, xComponent).close(Boolean.TRUE); } catch (Exception e) { e.printStackTrace(); } finally { if (xContext != null) { com.sun.star.uno.Runtime.getRuntime(xContext).freeUnusedLibraries(); } } } } ``` 4. 运行程序即可将LibreOffice文档换为PDF文档
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值