实现任意格式的office转pdf:
首先需要安装openoffice
地址:http://download.openoffice.org/other.html
百度很多 这个自己找找
一、配置maven依赖
<!--office转换pdf -->
<dependency>
<groupId>com.artofsolving</groupId>
<artifactId>jodconverter</artifactId>
<version>2.2.1</version>
</dependency>
<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>
使用maven只能下载到jodconverter-2.2.1.jar
二、代码实现
public static void Office2Pdf(String srcPath, String desPath) throws IOException {
// 源文件目录
File inputFile = new File(srcPath);
if (!inputFile.exists()) {
System.out.println("源文件不存在!");
return;
}
// 输出文件目录
File outputFile = new File(desPath);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().exists();
}
// 调用openoffice服务线程
String command = "C:/Program Files (x86)/OpenOffice 4/program/soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";
Process p = Runtime.getRuntime().exec(command);
// 连接openoffice服务
OpenOfficeConnection connection = new SocketOpenOfficeConnection(
"127.0.0.1", 8100);
connection.connect();
// 转换word到pdf
DocumentConverter converter = new OpenOfficeDocumentConverter(
connection);
converter.convert(inputFile, outputFile);
// 关闭连接
connection.disconnect();
// 关闭进程
p.destroy();
System.out.println("转换完成!");
}
测试代码
public static void main(String[] args) throws IOException {
String sourceFile = "D:/1.doc";
String destFile = "D://1.pdf";
Office2Pdf.Office2Pdf(sourceFile, destFile);
}
这个只能实现xls、doc、ppt转pdf 看个人需求
我这边是需要任意格式的office转pdf。 所以,很显然这个效果达不到要求
这边只需要把jodconverter-2.2.1.jar包 替换成jodconverter-2.2.2.jar就可以实现转换效果,但是maven是下在不到这个jar包的
jar地址:jodconverter/jodconverter-2.2.2.jar.zip( 27 k)
这个地址呢没有自己测试过,如果不行就自己百度一个,很多的
这个是maven项目,导入jar包可能不太方便,我这边也是导入不了这个jar包,只能放在lib项目中引用
其实很简单 新建一个lib文件夹 存放jar包
然后 biudpath 引入jar包
即可实现任意格式的openoffice转换pdf
有什么不懂的问题可以留言咨询,也可以加我微信:18601979899