前言

因为网上的信息比较多,每个项目又不太一样,故整理一下以作记录。

一、下载openoffice

下载地址:https://www.openoffice.org/download/
下载后,安装即可。

二、引入依赖

使用的JodConverter版本为2.2.1。因为jodconverter2.2.1必须依赖slf4j-jdk14必须这个版本,但是因为该版本过低,所以使用slf4j-api,slf4j-log4j12作为替代代替。

 <!--openOffice所需依赖--><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><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.5.6</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.5.6</version></dependency>

代码实现

         private static String OPENOFFICE_START = "E:\\it\\OpenOffice4\\program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";//openoffice安装路径以及启动命令private static String OPENOFFICE_IP = "127.0.0.1";//ipprivate static int OPENOFFICE_PORT = 8100;//端口private static String FILE_PATH = "D:\\fileConversion\\sourceFile";//源文件夹private static String DESTFILE_PATH = "D:\\fileConversion\\destFile\\";//目标文件夹private static String DESTFILE_SUFFIX = ".pdf";//文件后缀//使用openoffice 将word格式的文件转换为pdf格式@Testpublic void OfficeTest(){//获取源文件的文件夹下所有文件Map fileMap = null;Process progress = null;//openOffice进程OpenOfficeConnection connection = null;//openOffice连接// 调用openoffice服务线程try {if (progress == null){progress = Runtime.getRuntime().exec(OPENOFFICE_START);}connection = new SocketOpenOfficeConnection(OPENOFFICE_IP, OPENOFFICE_PORT);//创建连接对象connection.connect();//用于测试openOffice连接时间DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");System.out.println("连接时间:" + df.format(new Date()));DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);//创建转换器/*** 获取文件夹下所有文件的集合**/       fileMap = readfile(FILE_PATH);Set<Map.Entry<String,String>> setFile = fileMap.entrySet();for (Map.Entry<String, String> file : setFile) {String fileName = file.getKey();//文件名String filePath = file.getValue();//绝对路径String destFileName = fileName.substring(0, fileName.lastIndexOf("."));//获取不带后缀的文件名//转换文件converter.convert(filePath, DESTFILE_PATH + destFileName + DESTFILE_SUFFIX);}System.out.println("转换完成");} catch (IOException e) {e.printStackTrace();}finally {if (connection != null) {// 关闭连接System.out.println("关闭连接");connection.disconnect();}if (progress != null) {// 关闭进程System.out.println("关闭进程");progress.destroy();}}}

获取文件夹下所有文件

 /*** 读取某个文件夹下的所有文件(支持多级文件夹)*/public Map readfile(String filepath) throws FileNotFoundException, IOException {//声明一个HashMap,用于存放源文件,格式:<文件名,绝对路径>,以文件名为KEY,可以得到整个文件所在的路径和文件名Map<String, String> fileMap = new HashMap<>();try {File file = new File(filepath);if (!file.isDirectory()) {//文件fileMap.put(file.getName(),file.getAbsolutePath());} else if (file.isDirectory()) {//文件夹String[] filelist = file.list();for (int i = 0; i < filelist.length; i++) {File readfile = new File(filepath + "\\" + filelist[i]);if (!readfile.isDirectory()) {fileMap.put(readfile.getName(),readfile.getAbsolutePath());} else if (readfile.isDirectory()) {readfile(filepath + "\\" + filelist[i]);}}}} catch (FileNotFoundException e) {System.out.println("readfile()   Exception:" + e.getMessage());}return fileMap;}

注意JodConverter2.2.1不兼容docx、xlsx、pptx格式。

解决方法:重写BasicDocumentFormatRegistry类。新建com.artofsolving.jodconverter包

/*** openOffice工具类,用于解决不兼容docx\xlsx和pptx,导致转换pdf异常问题*/
public class BasicDocumentFormatRegistry implements DocumentFormatRegistry {private List documentFormats = new ArrayList();public BasicDocumentFormatRegistry() {}public void addDocumentFormat(DocumentFormat documentFormat) {this.documentFormats.add(documentFormat);}protected List getDocumentFormats() {return this.documentFormats;}public DocumentFormat getFormatByFileExtension(String extension) {if (extension == null) {return null;} else {if (extension.indexOf("doc") >= 0) {extension = "doc";}if (extension.indexOf("ppt") >= 0) {extension = "ppt";}if (extension.indexOf("xls") >= 0) {extension = "xls";}String lowerExtension = extension.toLowerCase();Iterator it = this.documentFormats.iterator();DocumentFormat format;do {if (!it.hasNext()) {return null;}format = (DocumentFormat)it.next();} while(!format.getFileExtension().equals(lowerExtension));return format;}}public DocumentFormat getFormatByMimeType(String mimeType) {Iterator it = this.documentFormats.iterator();DocumentFormat format;do {if (!it.hasNext()) {return null;}format = (DocumentFormat)it.next();} while(!format.getMimeType().equals(mimeType));return format;}
}

总结:结合网上信息,进行资源整合,作为笔记。

java整合openoffice实现word、execl、ppt转换为pdf相关推荐

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

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

  2. windows/linux服务器上java使用openoffice将word文档转换为PDF(亲测可用)

    一. 前言 1. 开发过程中经常会使用java将office系列文档转换为PDF, 一般都使用微软提供的openoffice+jodconverter 实现转换文档. 2. openoffice既有w ...

  3. linux下安装OpenOffice及使用java调用OpenOffice转换word、Excel为pdf并添加水印

    一.linux下安装OpenOffice OpenOffice是Apache的一款开源的软件,我主要是用来转换office文档成pdf文件,并给其添加水印等. (1)下载安装包 进入主题,首先我们需要 ...

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

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

  5. Spring Boot整合OpenOffice实现Word、Excel、PPT在线预览

    Spring Boot整合OpenOffice实现Word.Excel.PPT在线预览 1 介绍下OpenOffice 官网:https://www.openoffice.org/download/ ...

  6. java 利用openOffice实现word ppt 等资源转化为pdf文件

    网上有很多教程,但是很多坑,比如jobconvert2.2.1无法转pptx等格式的文件 而jobconvert2.2.2可以  但是maven仓库又没有  对于公司开发  这是个问题 毕竟公司的仓库 ...

  7. Java 将 Word 文档转换为 PDF 的完美工具

    点击上方 好好学java ,选择 星标 公众号 重磅资讯.干货,第一时间送达 今日推荐:为什么魂斗罗只有 128 KB却可以实现那么长的剧情?个人原创+1博客:点击前往,查看更多 来源:https:/ ...

  8. azw3转换为pdf_干货:如何Java 将 Word 文档转换为 PDF

    在日常工作中,PDF格式良好的视觉阅读性和稳定性使其使用越来越广泛.因此我们常会遇到需要将成型的Word文档转换为PDF格式的情况.本文就将通过使用Java程序来演示如何将Word文档转换成PDF格式 ...

  9. Java将Word文档转换为PDF的完美工具

    引用至:https://mp.weixin.qq.com/s/JIgo3f98HufGJx23mgtvag Java 将 Word 文档转换为 PDF 的完美工具 在日常工作中,PDF格式良好的视觉阅 ...

最新文章

  1. Android系统Google Maps开发实例浅析
  2. iOS架构-静态库.a 和.framework的区别(0)
  3. 原理分析之四:一次SQL查询的源码分析
  4. 文件上传api——MultipartFile
  5. 【算法与数据结构】堆排序是什么鬼?
  6. 点击了SAP CRM HANA report超链接,背后都发生了什么
  7. 【HRBUST - 1613】迷宫问题 (bfs)
  8. Python 函数(三)
  9. Web安全与Rational AppScan入门
  10. 【C#】SQL数据库助手类2.0(自用)
  11. 第3章第1讲算法与流程图
  12. 天龙八部雁门关外的故事
  13. 统计各个部门对应员工涨幅的次数总和,给出部门编码dept_no、部门名称dept_name以及次数sum
  14. 我沪漂 16 年,再也不打工了!
  15. curl http POST请求出现405错误
  16. Python程序设计 作业1
  17. pp模块常用表 sap_SAPpp模块内表..doc
  18. 创业实践案例课程随堂检测答案
  19. EDID是什么?为什么要使用它?和DDC的关系?
  20. 前诺基亚MeeGo开发者揭示收款Jolla Sailfish智能手机

热门文章

  1. CAD、CAM在钣金放样展开及下料方面的应用
  2. 戴尔服务器硬盘监控软件,Hp Dell服务器硬件监控
  3. 验证码的生成与验证,控制层工具
  4. Vue小黑记事本案例
  5. 聊QQ时,系统消息提示“被迫下线”怎么回事
  6. 欢迎试用Android定时达人~
  7. 大数据学习路线(完整详细版,含整套教程)
  8. Unity IAP Google支付问题汇总(排雷指南)(持续更新...)
  9. 阿里妈妈技术质量再度重磅开源:国内首个智能化功能测试开源平台Markov
  10. 解决UOS家庭版桌面图标消失,文件管理器进不去