最近在电子合同等项目中需要把word或者pdf转换成image,用到了openOffice把word转换pdf,以及把pdf转换成图片
感谢小伙伴张国清花费了三天时间来实现了此功能。下面我将把具体的步骤和注意事项说明。防止重复造轮子,最后我会把我的demo工程,以及对应的jar等发送到百度云。提供各位下载
一、首先,列出maven依赖以及jar包
<!--PDF转图片--> <dependency> <groupId>org.icepdf.os</groupId> <artifactId>icepdf-core</artifactId> <version>6.2.2</version> <exclusions> <exclusion> <groupId>javax.media</groupId> <artifactId>jai_core</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.icepdf.os</groupId> <artifactId>icepdf-viewer</artifactId> <version>6.2.2</version> </dependency> <!--word转pdf--> <dependency> <groupId>org.openoffice</groupId> <artifactId>jurt</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.openoffice</groupId> <artifactId>ridl</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.openoffice</groupId> <artifactId>juh</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.openoffice</groupId> <artifactId>unoil</artifactId> <version>3.0.1</version> </dependency> <!--需要手动添加到本地仓库 word 转 pdf--> <dependency> <groupId>org.artofsolving.jodconverter</groupId> <artifactId>jodconverter-core</artifactId> <version>3.0-beta-4</version> </dependency>
这里注意:你还一个需要把一个jar的文件夹(文章最下边有相应的百度云下载地址)你找到自己的maven仓库把对应的jar手动放入到 【repository/org/】目录下,在idea右边导入后会有有红色波浪线警告。整个可忽略,不影响使用
下图是对应怎么找到自己的maven repository路径
  
二、这里贴出详细的代码和配置,注意如果有的地方不正确或者不理解,欢迎评论或者在文章末尾下载我的demo工程来实际运行。以进一步详细熟悉
word转换pdf工具类
import org.apache.commons.lang3.StringUtils; import org.artofsolving.jodconverter.OfficeDocumentConverter; import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration; import org.artofsolving.jodconverter.office.OfficeManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import java.io.File; import java.util.List; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import java.util.regex.Pattern; /** * FileName: OfficeToPdfUtil * author: zhangguoqing * Date: 2018/9/19 9:18 * 说明: word文件转换成pdf文件(必须安装Openoffice) */ @Component public class OfficeToPdfUtil { static OfficeManager officeManager; private static ReentrantLock OfficeLock = new ReentrantLock(); @PostConstruct public void init() { DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration(); // 设置OpenOffice.org 3的安装目录 config.setOfficeHome(getOfficeHome()); // 启动OpenOffice的服务 OfficeManager getOfficeManager = config.buildOfficeManager(); if (getOfficeManager == null) { getOfficeManager.start(); } officeManager = getOfficeManager; } private static Logger logger = LoggerFactory.getLogger(OfficeToPdfUtil.class); /** * 锁 */ private static Lock lock = new R

