软件的安装与配置

链接: 官网 / C**N / 毒盘:提取码k47b

有能力的大佬可以选择c**n下载支持支持,官网下载可能有点慢

Windows下安装配置

下载安装包,安装到硬盘(这个就不截图了吧),记住你的安装路径,安装好后进入安装目录下的program文件夹(有soffice.exe文件的那个),运行以下命令开启服务

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

到这里就安装完成

Linux下安装配置

emmm…暂时没时间,所以没有测试等回头测试一次了再来发

项目配置

项目使用的是SpringBoot框架,下载jar文件C**N / 毒盘(提取码:23wu) (emmm…上传了再来加链接),解压出来,进入到项目目录下(有pom文件的目录),打开cmd窗口,依次运行

mvn install:install-file -Dfile=D:\jar文件目录,记得修改成自己的\jodconverter-core-3.0-beta-4.jar -DgroupId='org.artofsolving.jodconverter' -DartifactId=jodconverter-core -Dversion='3.0-beta-4' -Dpackaging=jar
mvn install:install-file -Dfile=D:\jar文件目录\jodconverter-2.2.2.jar -DgroupId='com.artofsolving' -DartifactId=jodconverter -Dversion='2.2.2' -Dpackaging=jar

引入pom依赖

        <dependency>
<groupId>com.artofsolving</groupId>
<artifactId>jodconverter</artifactId>
<version>2.2.2</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>
<!-- https://mvnrepository.com/artifact/org.artofsolving.jodconverter/jodconverter-core -->
<dependency>
<groupId>org.artofsolving.jodconverter</groupId>
<artifactId>jodconverter-core</artifactId>
<version>3.0-beta-4</version>
</dependency>

代码实现

简单版

/**
* 文件转pdf
* @param inputFile 源文件
* @param OpenOfficeHost host地址 (127.0.0.1)
* @param OpenOfficePort port端口
* @return
*/
public static String file2PDF(File inputFile, String OpenOfficeHost , Integer OpenOfficePort) {
String outPath="";
try {
if (!inputFile.exists()) {
return outPath;
}
// 如果目标路径不存在, 则新建该路径
outPath=inputFile.toString().substring(0, inputFile.toString().indexOf("."))+".pdf";
File outputFile = new File(outPath);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
//如果目标文件存在,则删除
if (outputFile.exists()) {
outputFile.delete();
}
OpenOfficeConnection connection = new SocketOpenOfficeConnection(OpenOfficeHost, OpenOfficePort);
connection.connect();
DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
connection.disconnect();
return outPath;
} catch (Exception e) {
e.printStackTrace();
}
return outPath;
}

完整版

private static OfficeManager officeManager;

private static boolean reconnect(Integer OpenOfficePort){
try {
ExternalOfficeManagerConfiguration externalProcessOfficeManager = new ExternalOfficeManagerConfiguration();
externalProcessOfficeManager.setConnectOnStart(true);
externalProcessOfficeManager.setPortNumber(OpenOfficePort);
officeManager = externalProcessOfficeManager.buildOfficeManager();
officeManager.start();
return true;
} catch (OfficeException e) {
e.printStackTrace();
return false;
}
}
// 开启新的openoffice的进程
private static void start(Integer OpenOfficePort,String OpenOfficeHome) {
try {
DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
configuration.setPortNumbers(OpenOfficePort);// 端口号
configuration.setOfficeHome(OpenOfficeHome);//安装位置
configuration.setTaskExecutionTimeout(1000 * 60 * 5);// 设置任务执行超时为5分钟
configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24);// 设置任务队列超时为24小时
officeManager = configuration.buildOfficeManager();
officeManager.start(); // 启动服务
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 文件转pdf
* @param input 输入文件
* @param OpenOfficePort 端口号
* @param OpenOfficeHome 安装地址
* @return
*/
public static File convertToPdf(String input,Integer OpenOfficePort,String OpenOfficeHome) {
File inputFile = null;
File outFile = null;
try {
// 如果已存在的服务不能连接或者不存在服务,那么开启新的服务    
if(!reconnect(OpenOfficePort)){
start(OpenOfficePort , OpenOfficeHome);
}
String output = FilenameUtils.separatorsToSystem(FilenameUtils.getFullPath(input) + FilenameUtils.getBaseName(input) + ".pdf");
inputFile = new File(input);
outFile = new File(output);
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
converter.convert(inputFile,outFile); // 转换文档
} catch (OfficeException o){
System.out.println("无法连接到外部办公流程!");
} catch (Exception e) {
outFile = null;
}finally{
stop();
}
return outFile;
}
// 关闭该进程
private static void stop() {
try {
if (officeManager != null)
officeManager.stop();
} catch (Exception e) {
e.printStackTrace();
}
}

附一张转换后页面预览的图吧

java 使用openoffice将doc、docx、ppt、pptx等转换pdf格式文件的更多相关文章

  1. java中使用apache poi 读取 doc,docx,ppt,pptx,xls,xlsx,txt,csv格式的文件示例代码

    java使用apache poi 读取 doc,docx,ppt,pptx,xls,xlsx,txt,csv格式的文件示例代码 1.maven依赖添加 在 pom 文件中添加如下依赖 <depe ...

  2. java读取txt/pdf/xls/xlsx/doc/docx/ppt/pptx

    环境准备txt利用common-iopdf利用pdfbox剩下的用POI关于POI,读取xls没啥特别的,主要是读取doc和ppt,需要下载poi源代码,然后将poi-src-3.7-20101029 ...

  3. c/c++常用代码---doc,ppt,xls文件格式转PDF格式[转]

    [转]doc,ppt,xls文件格式转PDF格式 http://blog.csdn.net/lee353086/article/details/7920355 确实好用. 需要注意的是#import文 ...

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

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

  5. 前端实现docx、pdf格式文件在线预览

    theme: vuepress highlight: atelier-heath-light 介绍 在业务中,如果遇到文档管理类的功能,会出现需要在线预览的业务需求,本文主要是通过第三方库来实现文档预 ...

  6. java 20 -2 递归之找特定目录下的特定格式文件

    /* 需求:把C:\Users\Administrator\Desktop\记录目录下所有以.java结尾的文件的绝对路径输出到控制台 分析: A:封装该目录 B:获取该目录下的所有文件或文件夹的Fi ...

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

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

  8. Java实现windows,linux服务器word,excel转为PDF;aspose-words,Documents4j

    Java实现windows,linux服务器word,excel转为PDF:aspose-words,Documents4j 一.通过aspose-words将word,Excel文档转为PDF 1. ...

  9. asp.net将ppt文档转换成pdf

    一.添加引用 using Microsoft.Office.Core;using Microsoft.Office.Interop.PowerPoint; 二.转换方法   C# 代码   复制 // ...

  10. office doc/xls/ppt 和 docx/xlsx/pptx 区别

    经同事告诉,今天才真正明白两都区别: doc/xls/ppt  是office2007以前的扩展名: docx/xlsx/pptx 是office2007版本及以后的扩展名,是基于xml的文件格式,x ...

随机推荐

  1. Java中编译异常与运行异常的区别

    编译期异常和运行期异常的区别 编译期异常和运行期异常的区别如下 异常处理要求不同:编译期异常(也称为检测异常checked Exception)要求在代码中显式地处理(使用try-catch或者thr ...

  2. B站login-极验逆向

    声明 本文章中所有内容仅供学习交流,抓包内容.敏感网址.数据接口均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关,若有侵权,请联系我立即删除! 目标网站 https:// ...

  3. kettle从入门到精通 第四十二课 kettle 1对多表拆分同步

    1.在有的业务场景中,会涉及一对多表拆分同步的业务场景,也就是说原表是一张表,将原表字段进行拆分放入目标库中的多张表,如下面的示例将表student_third中的数据 同步到student.teac ...

  4. 支付宝APP支付 订单已付款成功,请勿重复提交 和 微信H5支付 INVALID_REQUEST 201 商户订单号重复

    支付宝APP支付 返回请求给前端SDK 提示报错"订单已付款成功,请勿重复提交" 产生原因:存在商家订单号已经支付成功,重复再次请求的情况.每一笔的支付项目商家订单号是唯一的,如果 ...

  5. ansible v2.9.9离线安装脚本

    链接:https://pan.baidu.com/s/18uxyWWyJ39i1mJJ1hb8zww?pwd=QWSC 提取码:QWSC

  6. version `GLIBC_2.14' not found 问题解决

    参考连接:https://blog.csdn.net/u011262253/article/details/99056385

  7. 常用RAID级别简介

    RAID不同等级的两个目标: 1. 增加数据可靠性 2. 增加存储的读写性能 RAID级别: ​ RAID-0: 是以条带的形式将数据均匀分布在阵列的各个磁盘上 ​ 优点:读写性能高,不存在校验,不会 ...

  8. spring事务传递特性-REQUIRES_NEW和NESTED

    spring对于事务的实现的确是它的一大优点,节省了程序员不少时间. 关于事务,有许多可以聊的内容,例如实现方式.实现原理.传递特性等. 本文讨论传递特性中的REQUIRES_NEW,NESTED. ...

  9. VUE中watch的详细使用教程

      1.watch是什么? watch:是vue中常用的侦听器(监听器),用来监听数据的变化 2.watch的使用方式如下 watch: { 这里写你在data中定义的变量名或别处方法名: { han ...

  10. VS图片