700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Java用OpenOffice将word转换为PDF并预览

Java用OpenOffice将word转换为PDF并预览

时间:2022-06-13 18:14:49

相关推荐

Java用OpenOffice将word转换为PDF并预览

本文在原文的基础上有所修改,原文请参考:

/blog/1471606由于此blog不支持附件附件请到此处下载

/bigyuan/blog/165464

1. 需要用的软件

OpenOffice 下载地址/

JodConverter 下载地址/projects/jodconverter/files/JODConverter/,也可以直接从附件里面下载

jra包 需要jodconverter-2.2.2,jodconverter-2.2.1只能转.doc .xls .ppt

2.启动OpenOffice的服务

安装完openoffice,安装服务

cdC:\Program Files (x86)\OpenOffice 4\program

执行

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

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

netstat -ano|findstr "8100"

查看pid对应的服务程序名

tasklist|findstr "pid值"

3.将JodConverter相关的jar包添加到项目中

4. 下面是实现代码

附件里面有现成的可以用的项目示例,直接导入eclipse就可以运行

public static int office2PDF(String sourceFile, String destFile) throws FileNotFoundException {try {File inputFile = new File(sourceFile);if (!inputFile.exists()) {logger.info("找不到源文件");return -1;// 找不到源文件, 则返回-1}// 如果目标路径不存在, 则新建该路径File outputFile = new File(destFile);if (!outputFile.getParentFile().exists()) {outputFile.getParentFile().mkdirs();}// connect to an instance running on port 8100OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);connection.connect();// convertDocumentConverter converter = new OpenOfficeDocumentConverter(connection);converter.convert(inputFile, outputFile);// close the connectionconnection.disconnect();return 0;} catch (ConnectException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return 1;}

H5使用<embed src="xxx.pdf" />标签就可以在页面预览

maven 地址

<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>

jodconverter2.2.2 没有,只能手动下载配置

原文:/cxxjohnson/p/6880545.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。