一:环境搭建

OpenOffice 下载地址http://www.openoffice.org/

JodConverter 下载地址http://sourceforge.net/projects/jodconverter/files/JODConverter/

解压后将目录下的所有jar包放在工程的lib下面或者采用引用的方式调用这些jar包。

下载后安装,我安装的路径为D:/openOffice/install/

二:启动服务

可以通过cmd调用服务, " cd D:/openOffice/install/program"

执行

soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

查看是否安装成功,查看端口对应的pid

netstat -ano|findstr  8100

查看pid对应的服务程序名

tasklist|findstr pid值

也可以把这一步省略,放到java程序中调用服务,因为启动服务占用内存比较大,在java中可以在使用

的时候调用,然后马上销毁。

三:程序代码

1:将word转换为pdf方法

 // 将word格式的文件转换为pdf格式
public void Word2Pdf(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 = "D:/openOffice/install/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("转换完成!");
}

2:调用方法

 @Test
public void testWord2Pdf() throws IOException {
String srcPath = "E:/test.docx";
String desPath = "E:/test.pdf";
Word2Pdf(srcPath, desPath);
}

以上代码经过验证,可以正常运行。

四:遇到问题

错误信息:

java.net.ConnectException: connection failed: socket,host=10.101.50.71,port=8100,tcpNoDelay=1: java.net.ConnectException: Connection refused: connect

at com.artofsolving.jodconverter.openoffice.connection.AbstractOpenOfficeConnection.connect(AbstractOpenOfficeConnection.java:79)

原因以及解决方法:第一次调用,soffice需要注册,所以到soffice.exe的安装路径下双击soffice.exe,注册即可。

java 调用OpenOffice将word格式文件转换为pdf格式的更多相关文章

  1. Java使用Openoffice将word、ppt转换为PDF

    最近项目中要实现WORD的文件预览功能,我们可以通过将WORD转换成PDF或者HTML,然后通过浏览器预览. OpenOffice OpenOffice.org 是一套跨平台的办公室软件套件,能在 W ...

  2. Java 使用 jacob 将 word 文档转换为 pdf 文件

    网上查询了许许多多的博客,说利用 poi.iText.Jsoup.jdoctopdf.使用 jodconverter 来调用 openOffice 的服务来转换等等,我尝试了很多种,但要么显示不完全, ...

  3. java使用jacob将office文档转换为PDF格式

    jacob 包下载地址: http://sourceforge.net/projects/jacob-project/ 下载后,将jacob 与 jacob-1.19-x64.dll放到安装jdk目录 ...

  4. 使用openoffice将word文件转换为pdf格式遇到问题:The type com.sun.star.lang.XEventListener cannot be resolved. It is indirectly referenced from required

    The type com.sun.star.lang.XEventListener cannot be resolved. It is indirectly referenced from requi ...

  5. 将dos格式文件转换为unix格式

    在windows下换行符是\r\n,表示回到行首并换到下一行 而unix系统中换行符是\n 这样就存在一个问题,在windows上的文档到了unix上可能就无法使用了 针对这个情况有几种解决办法: 1 ...

  6. Java通过openOffice实现word,excel,ppt转成pdf实现在线预览

    Java通过openOffice实现word,excel,ppt转成pdf实现在线预览 一.OpenOffice 1.1 下载地址 1.2 JodConverter 1.3 新建实体类PDFDemo ...

  7. PHP 实现 word/excel/ppt 转换为 PDF

    前段时间负责公司内部文件平台的设计,其中有一个需求是要能够在线浏览用户上传的 office 文件. 我的思路是先将 office 转换成 PDF,再通过 pdf.js 插件解析 PDF 文件,使其能在 ...

  8. winform利用itextsharp.dll实现图片文件转换PDF格式文件

    1.利用itextsharp.dll实现单个图片文件转换为PDF格式文件, 可以使用以下类: void ConvertJPG2PDF(string jpgfile, string pdf) { var ...

  9. 将 Graphviz .dot 文件转换为其他格式的图像

    参考: Graphviz: How to go from .dot to a graph? 将 Graphviz .dot 文件转换为其他格式的图像 在Linux系统下,使用以下命令: dot -Tp ...

随机推荐

  1. linux 下载软件

    wget 软件地址 eg: 下载tomcat : wget http://apache.fayea.com/apache-mirror/tomcat/tomcat-7/v7.0.52/bin/apac ...

  2. How to include JavaScript file in JSF

    In JSF 2.0, you can use <h:outputScript /> tag to render a HTML "script" element, an ...

  3. mysql优化(一)

    1.选取最适用的字段属性  MySQL可以很好的支持大数据量的存取,但是一般说来,数据库中的表越小,在它上面执行的查询也就会越快.因此,在创建表的时候,为了获得更好的性能,我们可以将表中字段的宽度设得 ...

  4. Codeforces 622B The Time 【水题】

    B. The Time time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  5. 在线的JSON formate工具

    一个非常好的json formate工具 可以很容易发现json的错误,以及对json进行格式化 https://jsonformatter.curiousconcept.com/

  6. Arduino Due, Maple and Teensy3.0 的 W5200性能测试

    开源平台中以太网连接方案里W5100是众所周知的,W5200正在此领域越来越受欢迎.这个测试结果是在Arduino Due(Atmel CortexM3-84Mhz), Maple(ST Cortex ...

  7. 绑定线程到特定CPU处理器

    参考这篇文章 http://blog.chinaunix.net/uid-27761170-id-5050258.html 代码如下: #define _GNU_SOURCE #include < ...

  8. UIButton的imageEdgeInsets 和 titleEdgeInsets

    我们知道,在UIButton中有一个UILabel和一个UIImageView,同时还有属性: titleEdgeInsets,imageEdgeInsets.介绍下 imageEdgeInsets ...

  9. 设计模式-结合Android代码

    开始学设计模式 1 单例模式 单例模式可以说是最容易理解的模式了,也是应用最广的模式之一,先看看定义吧. 定义:确保单例类只有一个实例,并且这个单例类提供一个函数接口让其他类获取到这个唯一的实例. 什 ...

  10. IIS支持其他类型下载

    路径:IIS--默认网站属性-->http 头-->MIME映射 -->文件类型 -->新类型