支持linux和windowx的转pdf的java程序,当然服务器端需要安装不同版本的OpenOffice;
private static OpenOfficeConnection connection = null;
public static void officeToPdf(File srcFile, File destFile) {
try {
// 这里是OpenOffice的安装目录,
String sysIp ="127.0.0.1";//获取系统IP
String OOPath = "D:\\OpenOffice.org 3\\program\\soffice.exe -headless -accept=\"socket,host\=127.0.0.1,port\=8100;urp;\"";// 获取系统OpenOffice的安装目录,linux下:/opt/OpenOffice.org3/program/soffice -headless -accept\="socket,host\=127.0.0.1,port\=8100;urp;
// 启动OpenOffice的服务
Process pro = null;
try {
pro = Runtime.getRuntime().exec(OOPath);
} catch (IOException e) {
e.printStackTrace();
}
OpenOfficeConnection connection = getConnection(sysIp);
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
String fileExt = "";
String fileName = srcFile.getName();
int i = fileName.indexOf(".");
if (i != -1) {
fileExt = fileName.substring(i + 1);
}
//不同版本的office文档
if ("wps".equalsIgnoreCase(fileExt)) {
DocumentFormat df = new DocumentFormat("Kingsoft wps", DocumentFamily.TEXT, "application/wps", "wps");
DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();
DocumentFormat pdf = formatReg.getFormatByFileExtension("pdf");
converter.convert(srcFile, df, destFile, pdf);
} else if ("et".equalsIgnoreCase(fileExt)) {
DocumentFormat df = new DocumentFormat("Kingsoft et", DocumentFamily.TEXT, "application/et", "et");
DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();
DocumentFormat pdf = formatReg.getFormatByFileExtension("pdf");
converter.convert(srcFile, df, destFile, pdf);
} else if ("dps".equalsIgnoreCase(fileExt)) {
DocumentFormat df = new DocumentFormat("Kingsoft dps", DocumentFamily.TEXT, "application/dps", "dps");
DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();
DocumentFormat pdf = formatReg.getFormatByFileExtension("pdf");
converter.convert(srcFile, df, destFile, pdf);
} else {
converter.convert(srcFile, destFile);
}
String osname = System.getProperty("os.name");
if (osname.indexOf("Windows") > -1) {
connection.disconnect();
} else {
connection.connect();
}
pro.destroy();
} catch (ConnectException e) {
logger.error("officeToPdf error:", e);
} catch (Exception e) {
logger.error("officeToPdf error:", e);
}
}
private static OpenOfficeConnection getConnection(String sysIp) throws ConnectException {
if (connection == null || !connection.isConnected()) {
connection = new SocketOpenOfficeConnection(sysIp, 8100);
connection.connect();
}
return connection;
}
public static void main(String[] s) {
File office = new File("D:\\test.doc");
File pdf = new File("D:\\test.pdf");
ConvertX2PDFHelper.officeToPdf(office, pdf);
}
分享到:
相关推荐
office文档通过openoffice或者microsoft多线程转换成pdf文档,并通过pdf.js显示
OpenOffice转换Office文档为PDF、HTML,将word,ppt转成html,Excel转为Html
NULL 博文链接:https://titanseason.iteye.com/blog/1471606
Word、Excel等常用文档格式通过OpenOffice转换为pdf及图片,亲测,可用。Word、Excel等常用文档格式通过OpenOffice转换为pdf及图片,亲测,可用。Word、Excel等常用文档格式通过OpenOffice转换为pdf及图片,亲测,可用...
本篇文章主要介绍了java使用openoffice将office系列文档转换为PDF的示例方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
因为在Android项目中要用到在线查看Office文档,但是缺没有合适的SDK可以用,只能从图片着手。下载一个Openoffice软件下载到电脑然后安装,具体如何安装请百度一下。分析一下代码的功能;Entry.java这个类的原理是先...
Java利用OpenOffice将word等office文档转换成PDF
使用OpenOffice将文档转换为PDF,将PDF转换为Swf
java实现附件预览(openoffice+PDF.js),将office文档,通过openoffice工具转换为PDF文件,使用PDF.js进行前端展示 是对openoffice+swftools+flexpaper的升级版,减少一次swf文件转换,及flexpaper只能预览十页内容...
将word文档转化为PDF是项目中常见的需求之一,目前主流的方法可以分为两大类,一类是利用各种Office应用进行转换,譬如Microsoft Office、WPS以及LiberOffice,另一种是利用各种语言提供的对于Office文档读取的接口...
用java可以将office文档转化为pdf并再转为swf文件播放,期间要用 到openoffice的转化服务,亲测可用,里面内容详细请看readme.txt,效果不错
JodConverter一个Java的OpenDocument 文件转换器,可以进行...本资源包含了所有的jodconverter及相关jar包以及java代码和openoffice服务器文档,供大家下载使用。 OpenOffice的下载地址:http://www.openoffice.org/
根据网上的资料总结的一个springboot 转换pdf Word文档大小最好2m以下 需要安装OpenOffice.org 3.3 链接:https://pan.baidu.com/s/1onrkhBCNlGLEmf3hPwzXWw 密码:8h5a
它的工作原理是先用openOffice软件把ppt、word、excel、txt等ms office格式文档转换成pdf,然后用swftools软件转换成swf,最后通过FlexPaper实现在线播放;而这个过程并无需在客户端安装WORD、PDF等office办公软件...
linux下转换office文档成为pdf,,实现在线预览。文档有详细说明,openoffice太大无法上传。这里有文档说明和需要的jar包,,还有字体。文档有路径进行直接下载。
提供window上OpenOffice4安装文件下载,用于做office文件转换PDF,进行在线预览。
Java程序中可以利用OpenOffice,将办公文档(包括doc、docx、xls、xlsx等)转换为PDF格式的文档。由于PDF格式的文档可以达到在线预览的效果,而Office办公文档实现在线预览相对来说会比较麻烦,但曲线救国,通过文档...
解决通过OpenOffice如何将word、excel、ppt、html、txt转换成pdf 解决如何将pdf转换成图片 解决如何将word、excel、ppt、html、txt转换成图片,之前有人传过,但不能运行,现在传个自己的,可以完美转换,谁下谁知道...
JODConverter,是一个Java的OpenDocument文件转换器,依赖于OpenOffice.org或者LibreOffice提供的服务来进行转换,它能将Microsoft Office文档(Word,Excel,PowerPoint)转换为PDF格式。
1、该代码使用SpringBoot框架实现了...2、主要包含两个接口,一个是office文档转化成pdf文档;一个是pdf文档的在线预览 3、由于调用了OpenOffice的接口进行文档转化,所以需要安装OpenOfiice,这里用的版本是4.1.3。