最近项目中要实现WORD的文件预览功能,我们可以通过将WORD转换成PDF或者HTML,然后通过浏览器预览。

OpenOffice

OpenOffice.org 是一套跨平台的办公室软件套件,能在 Windows、Linux、MacOS X (X11)、和 Solaris 等操作系统上执行。它与各个主要的办公室软件套件兼容。OpenOffice.org 是自由软件,任何人都可以免费下载、使用、及推广它。

下载地址

http://www.openoffice.org/

JodConverter

jodconverter-2.2.2.zip 下载地址:

http://sourceforge.net/projects/jodconverter/files/JODConverter/

Word转换

启动OpenOffice的服务

进入openoffice安装目录,通过cmd启动一个soffice服务,启动的命令是soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"

如果觉得后台运行OpenOffice服务比较麻烦,可以通过

运行代码

public class PDFDemo {

    public static boolean officeToPDF(String sourceFile, String destFile) {
try { File inputFile = new File(sourceFile);
if (!inputFile.exists()) {
// 找不到源文件, 则返回false
return false;
}
// 如果目标路径不存在, 则新建该路径
File outputFile = new File(destFile);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
//如果目标文件存在,则删除
if (outputFile.exists()) {
outputFile.delete();
}
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
connection.connect();
//用于测试openOffice连接时间
System.out.println("连接时间:" + df.format(new Date()));
DocumentConverter converter = new StreamOpenOfficeDocumentConverter(
connection);
converter.convert(inputFile, outputFile);
//测试word转PDF的转换时间
System.out.println("转换时间:" + df.format(new Date()));
connection.disconnect();
return true;
} catch (ConnectException e) {
e.printStackTrace();
System.err.println("openOffice连接失败!请检查IP,端口");
} catch (Exception e) {
e.printStackTrace();
}
return false;
} public static void main(String[] args) {
officeToPDF("E:\\test.docx", "E:\\test.pdf");
}
}

Word、ppt转Html

只需要将后缀名从.pdf改为.html即可。

public static void main(String[] args) {
officeToPDF("E:\\test.docx", "E:\\test.html");
}

Maven配置

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>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.4.3</version>
</dependency>

Maven只有 2.2.1版本,2.2.1版本有一个问题,那就是不兼容docx和pptx,如果你们不使用jodconverter-2.2.2 中lib,而想要使用2.2.1版本,需要修改一下 BasicDocumentFormatRegistry 类中的 getFormatByFileExtension方法:

  1. 新建包 com.artofsolving.jodconverter
  2. 新建类BasicDocumentFormatRegistry,复制下面代码
package com.artofsolving.jodconverter;

/**
* @author 李文浩
* @date 2017/12/25
*/ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; 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;
}
}

下面是增加的部分,仅仅增加了将docx按照doc的处理方式处理。而2.2.2版本已经默认增加了。

if (extension.indexOf("doc") >= 0) {
extension = "doc";
}
if (extension.indexOf("ppt") >= 0) {
extension = "ppt";
}
if (extension.indexOf("xls") >= 0) {
extension = "xls";
}

参考文档

  1. Java实现在线预览–openOffice实现
  2. Java项目中使用OpenOffice转PDF
  3. java使用openoffice将office系列文档转换为PDF
  4. java 如何将 word,excel,ppt如何转pdf--jacob
  5. java 如何将 word,excel,ppt如何转pdf --openoffice (1)

Java使用Openoffice将word、ppt转换为PDF的更多相关文章

  1. PHP windoews调用OpenOffice实现word/ppt转PDF

    1.安装免费的openOffice软件 2.需要JDK支持 3.安装完openOffice后,在开始--运行中输入Dcomcnfg打开组件服务.在组件服务—计算机—我的电脑—DCOMP配置中 4. 先 ...

  2. 使用openoffice将word文件转换为pdf格式遇到问题:The type com.sun.star.lang.XEventListener cannot be resolved. It is indirectly referenced from required

    The type com.sun.star.lang.XEventListener cannot be resolved. It is indirectly referenced from requi ...

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

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

  4. Aspose office (Excel,Word,PPT),PDF 在线预览

    前文: 做个备份,拿的是试用版的 Aspose,功能见标题 代码: /// <summary> /// Aspose office (Excel,Word,PPT),PDF 在线预览 // ...

  5. java 调用OpenOffice将word格式文件转换为pdf格式

    一:环境搭建 OpenOffice 下载地址http://www.openoffice.org/ JodConverter 下载地址http://sourceforge.net/projects/jo ...

  6. Java用OpenOffice将word转换为PDF

    一.      软件安装以及jar包下载 官网的下载地址如下(英文): OpenOffice 下载地址http://www.openoffice.org/ JodConverter 下载地址http: ...

  7. PHP 实现 word/excel/ppt 转换为 PDF

    前段时间负责公司内部文件平台的设计,其中有一个需求是要能够在线浏览用户上传的 office 文件. 我的思路是先将 office 转换成 PDF,再通过 pdf.js 插件解析 PDF 文件,使其能在 ...

  8. [Python Study Notes]批量将ppt转换为pdf v1.0

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...

  9. word、ppt转换为pdf

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

随机推荐

  1. Nginx概述和安装(1)

    一.Nginx概述 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 I ...

  2. Java与算法之(5) - 老鼠走迷宫(深度优先算法)

    小老鼠走进了格子迷宫,如何能绕过猫并以最短的路线吃到奶酪呢? 注意只能上下左右移动,不能斜着移动. 在解决迷宫问题上,深度优先算法的思路是沿着一条路一直走,遇到障碍或走出边界再返回尝试别的路径. 首先 ...

  3. MyBatis之基于XML的属性与列名映射

    上一博客主要是对单表的增删改查,比较简单,而且每个属性与table表的列都是一一对应名字也一样,今天主要学习属性与table表列名不一致的处理,主要有两种一是属性与列名不一致,二是枚举的情况,这里暂时 ...

  4. linux(六)之文本操作

    接下来我们一起来看一下再linux中怎么去对文本进行操作的 一.文本文件 既然要操作文本,所以我们要对文本有一个了解,那什么是文本文件呢. 文本文件是一种由若干行字符构成的计算机文件.文本文件存在于计 ...

  5. (亲测)躺着破解IDM下载权限,治疗不用破解补丁的强迫症们

    首先.如果触犯了某些规则权限,请原谅. 很早以前就做过这个的破解,挺实用的,我今天就把之前写的经验贴出来大家一起学习学习~~~ 今天利用这个方法破解了最新版,最终的效果如下所示:我不是来刷存在感的.只 ...

  6. Vijos P1113 不高兴的津津【模拟】

    不高兴的津津 描述 津津上初中了.妈妈认为津津应该更加用功学习,所以津津除了上学之外,还要参加妈妈为她报名的各科复习班.另外每周妈妈还会送她去学习朗诵.舞蹈和钢琴.但是津津如果一天上课超过八个小时就会 ...

  7. codeforces 746C 模拟

    C. Tram time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  8. Win10没有以太网图标如何找回?以太网适配器不见了怎么恢复?

    Win10以太网适配器不见了怎么恢复?以太网其实就是Win7系统中常说的"本地连接"假若用户发现网络适配器中的以太网适配器图标不见了,可以在设备管理器中添加一些这类适配器,具体过程 ...

  9. 从零开始学习前端JAVASCRIPT — 4、JavaScript基础Math和Date对象的介绍

    Math对象的介绍 1:Math对象 Math 对象用于执行数学任务.并不像 Date 和 String 那样是对象的类,因此没有构造函数 Math().您无需创建它,通过把 Math 作为对象使用就 ...

  10. zookeeper部署和运行

    环境准备: 操作系统,此处使用windows系统 Java运行环境,JDK1.6以上 下载对应操作系统zookeeper安装包zookeeper-x.x.x.tar.gz,下载地址:http://zo ...