概述

使用OpenOffice将 office文档转为pdf,然后再将pdf转为图片,实现文档预览的功能。

依赖组件

OpenOffice.org或者LibreOffice

JODConverter

需要注意的问题

Linux平台下程序的兼容性

防止转换出来的文档乱码

防止文档转换不全,一些特殊格式的处理

OpenOffice

1.启动OpenOffice的服务

cd C:\Program Files (x86)\OpenOffice 4\program

执行命令

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

进程名称是soffice.exe

查看是否安装成功,查看端口对应的pid

netstat -ano|findstr "8100"

Pdf 文档转图片

pdf转高清图片需要的jar:http://download.csdn.net/detail/emoven/9666543

代码

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.util.GraphicsRenderingHints; import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.net.ConnectException; public class Main { public static void main(String[] args) {
int result = office2PDF("D:\\RabbitMQ的实战应用.pptx","D:\\RabbitMQ的实战应用.pdf");
System.out.println(result); pdf2Pic("D:\\RabbitMQ的实战应用.pdf", "D:\\22\\");
} public static void pdf2Pic(String pdfPath, String path){
Document document = new Document();
document.setFile(pdfPath);
float scale = 2.5f;//缩放比例
float rotation = 0f;//旋转角度 for (int i = 0; i < document.getNumberOfPages(); i++) {
BufferedImage image = (BufferedImage)
document.getPageImage(i, GraphicsRenderingHints.SCREEN, org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);
RenderedImage rendImage = image;
try {
String imgName = i + ".png";
System.out.println(imgName);
File file = new File(path + imgName);
ImageIO.write(rendImage, "png", file);
} catch (IOException e) {
e.printStackTrace();
}
image.flush();
}
document.dispose();
} /**
* 将Office文档转换为PDF. 运行该函数需要用到OpenOffice, OpenOffice下载地址为
* http://www.openoffice.org/
*
* <pre>
* 方法示例:
* String sourcePath = "F:\\office\\source.doc";
* String destFile = "F:\\pdf\\dest.pdf";
* Converter.office2PDF(sourcePath, destFile);
* </pre>
*
* @param sourceFile
* 源文件, 绝对路径. 可以是Office2003-2007全部格式的文档, Office2010的没测试. 包括.doc,
* .docx, .xls, .xlsx, .ppt, .pptx等. 示例: F:\\office\\source.doc
* @param destFile
* 目标文件. 绝对路径. 示例: F:\\pdf\\dest.pdf
* @return 操作成功与否的提示信息. 如果返回 -1, 表示找不到源文件, 或url.properties配置错误; 如果返回 0,
* 则表示操作成功; 返回1, 则表示转换失败
*/
public static int office2PDF(String sourceFile, String destFile) {
try {
File inputFile = new File(sourceFile);
if (!inputFile.exists()) {
return -1;// 找不到源文件, 则返回-1
} // 如果目标路径不存在, 则新建该路径
File outputFile = new File(destFile);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
} // connect to an OpenOffice.org instance running on port 8100
OpenOfficeConnection connection = new SocketOpenOfficeConnection(
"127.0.0.1", 8100);
connection.connect(); // convert
DocumentConverter converter = new OpenOfficeDocumentConverter(
connection);
converter.convert(inputFile, outputFile); // close the connection
connection.disconnect(); return 0;
} catch (ConnectException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} return 1;
}
}

遇到的问题

1. Exception in thread "main" java.lang.IllegalArgumentException: unknown document format for file: F:\RESTful接口设计规范.docx

不能识别docx, xlsx等文件

java使用openoffice将office系列文档转换为PDF

通过改JODConverter的源码使得其支持docx等类型的文件。

或者

将Office(如:Word、Excel、PPT 等)文件转html(通过OpenOffice实现)

使用JODConverter3.0版本,与2.2.2版本区别较大

总结

将文档转为html的格式没有转为pdf的格式好

参考

工具网站

使用OpenOffice实现文档预览的更多相关文章

  1. JAVA+FlexPaper+OpenOffice+SWFTools文档预览

    http://blog.csdn.net/core_star/article/details/10148047 1.软件环境: openoffice:启动openoffice服务:soffice.ex ...

  2. 在线文档预览方案-office web apps续篇

    上一篇在线文档预览方案-office web apps发布后收到很多网友的留言提问,所以准备再写一篇,一来介绍一下域控服务器安装,总结一下大家问的多的问题,二来宣传预览服务安装与技术支持的事情. 阅读 ...

  3. 在线文档预览方案-office web apps

    最近在做项目时,要在手机端实现在线文档预览的功能.于是百度了一下实现方案,大致是将文档转换成pdf,然后在通过插件实现预览.这些方案没有具体实现代码,也没有在线预览的地址,再加上项目时间紧迫.只能考虑 ...

  4. [转载]在线文档预览方案-Office Web Apps

    最近在做项目时,要在手机端实现在线文档预览的功能.于是百度了一下实现方案,大致是将文档转换成pdf,然后在通过插件实现预览.这些方案没有具体实现代码,也没有在线预览的地址,再加上项目时间紧迫.只能考虑 ...

  5. 微软office web apps 服务器搭建之在线文档预览(一)

    office web apps安装 系统要求为Windows Server 2012, 注意:转换文档需要两台服务器,一台为转换server,另外一台为域控server.(至于为什么要两台,这个请自行 ...

  6. 微软office web apps 服务器搭建之在线文档预览(二)

    上一篇文章已经介绍了整个安装过程了.只要在浏览器中输入文档转换server的ip,会自动跳转,出现如下页面. 那么就可以实现本地文档预览了,你可以试试.(注意:是本地哦,路径不要写错,类似“\\fil ...

  7. 解决officeOnline文档预览服务器只能域名提交的限制Redirect

    此项目是解决officeOnline文档预览只能用域名提交的限制 http://officeOnline文档预览域名或IP/op/generate.aspx // 微软原生页面 创建链接后会生成全屏预 ...

  8. 一文带你玩转对象存储COS文档预览

    随着"互联网+"的发展,各行各业纷纷"去纸化",商务合同.会议纪要.组织公文.商品图片.培训视频.学习课件.随堂讲义等电子文档无处不在.而要查看文档一般需要先下 ...

  9. 秒级接入、效果满分的文档预览方案——COS文档预览

    一.导语 ​ 说起 Microsoft Office 办公三件套,想必大家都不会陌生,社畜日常的工作或者生活中,多多少少遇到过这种情况: 本地创建的文档换一台电脑打开,就出现了字体丢失.排版混乱的情况 ...

随机推荐

  1. 一:robot framework环境安装

    1.安装robot framework: 打开cmd进入dos下,输入 pip install robotframework Microsoft Windows [版本 10.0.18362.267] ...

  2. CentOS7配置时间和CentOS6搭建局域网NTP

    NTP 2015年8月20日 星期四 17:34 CentOS 7配置本地时区和TIME ZONE #用tzselect配置时区和time zone [root@localhost Asia]# /u ...

  3. 【服务总线 Azure Service Bus】Service Bus在使用预提取(prefetching)后出现Microsoft.Azure.ServiceBus.MessageLockLostException异常问题

    问题描述 Service Bus接收端的日志中出现大量的MessageLockLostException异常.完整的错误消息为: Microsoft.Azure.ServiceBus.MessageL ...

  4. select监听服务端

    # can_read, can_write, _ = select.select(inputs, outputs, None, None)## 第一个参数是我们需要监听可读的套接字, 第二个参数是我们 ...

  5. NDK&JNI开发总结

    NDK&JNI开发总结 简介 附个不错的博客 https://www.jianshu.com/p/87ce6f565d37 在Android Framework中,需要提供一种媒介或 桥梁,将 ...

  6. JavaScript原型链及其污染

    JavaScript原型链及其污染 一.什么是原型链? 1.JavaScript中,我们如果要define一个类,需要以define"构造函数"的方式来define: functi ...

  7. Java之选择排序(正序、逆序)

    public class SelectSort { public static void main(String[] args) { /** * @author JadeXu * @// TODO: ...

  8. python核心高级学习总结7---------正则表达式

    正则表达式在爬虫项目中应用很广泛,主要方面就是在字符串处理方面,经常会涉及到字符串格式的校验,用起来经常要查看文档才能完成,所以抽了个时间将正则的内容复习了一下. Start re---导入re模块使 ...

  9. 第10.5节 使用__all__定义Python模块导入白名单

    一. 引言 <第10.4节 Python模块的弱封装机制>介绍了Python模块的的弱封装机制,除了使用弱封装机制来从一定程度上防止导入特定成员外,Python模块中还提供可另外一种类似白 ...

  10. Android10_原理机制系列_Window介绍及WMS的启动过程

    简介 Window简介 Android中,Window是一个重要部分,用户看到的界面.触摸显示界面进行一系列操作都涉及到Window.但实际上,Window本身并不具备绘制功能. 该篇简单介绍下Win ...