首页 技术 正文
技术 2022年11月11日
0 收藏 409 点赞 4,763 浏览 1909 个字

—恢复内容开始—

最近在做项目时需要写一个功能,将doc,ppt,xsl等文档做在线预览。网上查了很多资料,开始适用poi将文档转成pdf没成功,后来使用了OpenOffice4 + jodconverte2.2.2  + slf4j-jdk14-1.7.21,

jarbao版本一定要对,不然会报错,

首先安装Open Office4,导入jia包,下面直接上代码;

 import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.ConnectException; import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.thinkgem.jeesite.common.config.Global; public class FileTransferUtils { /**
* inputFile : 源文件路径<br>
* outputFile : 目标文件路径<br>
* suffix : 目标文件后缀 ".pdf" , ".html" etc
* */
/*public static void main(String []args){
office2PDF("D:\\test\\out.docx", "D:\\test\\", ".pdf");
}*/
public static void office2PDF(String inputFile,String outputFile,String suffix){
try{
File input = new File(inputFile);
if(!input.exists()){
}
String filename = input.getName();
String file = filename.substring(0, filename.lastIndexOf("."));
System.out.println(file);
File output = new File(outputFile+file+suffix);
System.out.println(outputFile+file+suffix);
if(output.exists()){
output.delete();
} String open_office_home = Global.getConfig("openoffice.path"); //这是我在项目中配置的
//String open_office_home ="你的openoffie安装路径";
String command = open_office_home+
"program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\""; //这条命令是通用的; Process process = Runtime.getRuntime().exec(command); OpenOfficeConnection connection =new SocketOpenOfficeConnection();
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(input, output);
connection.disconnect();
process.destroy();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch (ConnectException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}

以上就是我做office文档转pdf的经验,初次发博,欢迎指教

—恢复内容结束—

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,995
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,509
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,352
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,136
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,769
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,847