软件的安装与配置

链接: 官网 / 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. NOIP模拟58

    T1 Lesson5 ! 解题思路 首先对于整张图求出拓扑序,然后顺着拓扑序其实也就是顺着边的方向,更新最长路,也就是从 1 节点到达这个节点的最长路. 然后再逆着拓扑序,反向求一下最长路,也就是从这 ...

  2. JavaServlet和后端的搭建(以Tomcat为例)

    目录 Servlet 前端如何才能访问到后端? 后端开发准备工作(配置Tomcat) 对象的生命周期 后端平台的搭建 创建Web项目(前提搭建好Tomcat配置) 创建Java文件 配置Web.xml ...

  3. 技术解密Java Chassis 3超实用的可观测性

    本文分享自华为云社区<Java Chassis 3技术解密:实用的可观测性>,作者:liubao68. 狭义的可观测性,指日志.调用链和指标,广义的可观测性则包含更多的内容,一般的,应用程 ...

  4. apollo配置中心从数据库中查询所有的配置及项目

    apollo配置中心从数据库中查询所有的配置及项目 需求背景:如果需要从Apollo查询某个配置项做批量的更新替换,如果一个一个找不合适且容易遗漏,需要从底层数据库表中模糊查询来实现. 1.查看apo ...

  5. http请求方式-HttpURLConnection

    http请求方式-HttpURLConnection import com.alibaba.fastjson.JSON; import com.example.core.mydemo.http.Ord ...

  6. MySQL Explain 关键字详解

    概述 explain 关键字可以模拟执行 sql 查询语句,输出执行计划,分析查询语句的执行性能 使用方式如下:explain + sql explain select * from t1 执行计划各 ...

  7. 基于Web3.0的区块链图片上传

    开始前,我们先简单了解一下基本的概念,我大致归纳为以下几个点 什么是Web3.0,和区块链又有什么关系?(上回的文章不就派上用场了) 需求:开发一个基于Python的Web 3.0图片上传系统.这个系 ...

  8. hdfs修复块

    磁盘满了一次,导致hdfs的很多块变成一个副本 看一下副本信息 执行 hdfs fsck / 1007个块少于3个副本一下  而且 hbase的regionsever启动报错 File /apps/h ...

  9. markdown折叠展开代码

    背景 有的时候,我们的代码太多,直接用cout<<"hello";很不方便. 我们可以将代码折叠. 效果 代码 普通代码折叠 <details> <s ...

  10. 高通mm-camera平台 Camera移植

    高通Cam-X平台 Camera移植 注:此文档以在高通8916平台移植OV5648为例,给大家讲解Android SOC的底层Camera. Reference: https://blog.csdn ...