软件的安装与配置

链接: 官网 / 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. kubernetes——资源管理

    Kubernetes 资源管理 介绍 kubernetes中,所有的内容都抽象为资源,用户需要通过操作资源来管理kubernetes. kubernetes的最小管理单元是pod而不是容器,所以只能将 ...

  2. jquery jquery的选择器

      什么是 jQuery 前端的一个方法库/函数库                 将很多过功能,封装好,我们可以直接使用             官网  https://jquery.com/    ...

  3. 关于 CSDN 的恶臭嘴脸

    我有段时间确实希望通过 CSDN 增加我文章的阅读量.但是我怎么使用 CSDN,我想要的大概也就 cnblogs 相对符合预期,CSDN 真是垃圾. 为了钱真是什么都不要了,让我们这种没有自主经济来源 ...

  4. C#.NET 国密 BASE64编码的私钥提取16进制私钥 (锦州银行、建行轻应用)

    C#.NET 国密 BASE64编码的私钥提取16进制私钥 (锦州银行.建行轻应用), 从BASE64编码的公钥中提取16进制字符串公钥, 从BASE64编码的私钥中提取16进制字符串私钥, 锦州银行 ...

  5. Centos7部署FytSoa项目至Docker——第二步:安装Mysql、Redis

    FytSoa项目地址:https://gitee.com/feiyit/FytSoaCms 部署完成地址:http://82.156.127.60:8001/ 先到腾讯云申请一年的云服务器,我买的是一 ...

  6. JSONObject应用Json字符串和Object对象之间的转换,Map封装数据思路

    JSONObject应用Json字符串和Object对象之间的转换,Map封装数据思路 package com.example.core.mydemo.json5; import com.alibab ...

  7. 如何在 VSCode 中配置和编写 LINGO

    目录 如何在 VSCode 中配置和编写 LINGO 安装 VSCode 扩展 LINGO 脚本文件与 runlingo 命令 LINGO 命令行交互和脚本文件 配置 Visual Stdio Cod ...

  8. python重拾第十三天-那就做个堡垒机吧

    本节内容 项目实战:运维堡垒机开发 前景介绍 到目前为止,很多公司对堡垒机依然不太感冒,其实是没有充分认识到堡垒机在IT管理中的重要作用的,很多人觉得,堡垒机就是跳板机,其实这个认识是不全面的,跳板功 ...

  9. Linux内核中的static-key机制

    # Linux内核中的static-key机制 背景 在移植某个TP时,发现频繁操作屏幕会导致i2c总线死掉.在跟踪代码的时候,我发现了这个static-key. 因此,学习一下这块的知识. refe ...

  10. 解决keil5仿真错误:Encountered an improper argument

    --- title: 解决keil5仿真错误:Encountered an improper argument date: 2020-06-18 03:13:18 categories: tags: ...