第一步:jar包依赖:
<!--openoffice 依赖jar包 -->
<dependency>
<groupId>com.artofsolving</groupId>
<artifactId>jodconverter</artifactId>
<version>2.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.livesense/jodconverter-core -->
<dependency>
<groupId>com.github.livesense</groupId>
<artifactId>jodconverter-core</artifactId>
<version>1.0.5</version>
</dependency>
第二步:功能代码
package com.digipower.ucas.openoffice.util;
import java.io.File;
import java.io.IOException;
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 com.digipower.ucas.common.exception.OpenOfficeException;
/**
*
* @ClassName: OpentOfficeUtil
* @Description: openoffice pdf 转换工具
* @author: **** -zzg
* @date: 2019年4月26日 上午9:39:11
*
* @Copyright: 2019 www.digipower.cn
* 注意:本内容仅限于深圳市****限公司内部使用,禁止用于其他的商业目的
*/
public class OpentOfficeUtil {
// 日志记录功能
private final static Logger logger = LoggerFactory.getLogger(OpentOfficeUtil.class);
private static OfficeManager officeManager;
private static String OFFICE_HONE = "C:/Program Files (x86)/OpenOffice 4/";
private static int port[] = { 8100 };
// set 和 get 方法
public static String getOFFICE_HONE() {
return OFFICE_HONE;
}
public static void setOFFICE_HONE(String oFFICE_HONE) {
OFFICE_HONE = oFFICE_HONE;
}
public static int[] getPort() {
return port;
}
public static void setPort(int[] port) {
OpentOfficeUtil.port = port;
}
// 打开服务器
public static void startService() {
DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
try {
System.out.println("准备启动服务....");
configuration.setOfficeHome(OFFICE_HONE);// 设置OpenOffice.org安装目录
configuration.setPortNumbers(port); // 设置转换端口,默认为8100
configuration.setTaskExecutionTimeout(1000 * 60 * 5L);// 设置任务执行超时为5分钟
configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);// 设置任务队列超时为24小时
officeManager = configuration.buildOfficeManager();
officeManager.start(); // 启动服务
System.out.println("office转换服务启动成功!");
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new OpenOfficeException(e.getMessage(), e);
}
}
// 关闭服务器
public static void stopService() {
System.out.println("关闭office转换服务....");
if (officeManager != null) {
officeManager.stop();
}
System.out.println("关闭office转换成功!");
}
public void convertToPDF(String inputPath, String outputPath){
File inputFile = new File(inputPath);
File outputFile = new File(outputPath);
convertToPDF(inputFile,outputFile);
}
public void convertToPDF(File inputFile, File outputFile){
// 判断inputFile文件存在
if(inputFile.exists()){
if(!outputFile.exists()){
try {
outputFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
logger.error(e.getMessage(), e);
throw new OpenOfficeException(e.getMessage(),e);
}
}
// 开始文件转换
startService();
System.out.println("进行文档转换转换:" + inputFile + " --> " + outputFile);
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
converter.convert(inputFile, outputFile);
stopService();
System.out.println();
} else {
// inputFile文件不存在
throw new OpenOfficeException("inputFile 文件不存在");
}
}
}
测试代码:
package com.digipower.ucas.openoffice.test;
import com.digipower.ucas.openoffice.util.OpentOfficeUtil;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
OpentOfficeUtil util = new OpentOfficeUtil();
String inputPath = "C:/Users/zzg/Desktop/1.png";
String outputPath = "C:/Users/zzg/Desktop/1.pdf";
util.convertToPDF(inputPath, outputPath);
}
}