使用OpenOffice实现文档预览
概述
使用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的格式好
参考
- Java用OpenOffice将word转换为PDF
- Office在线预览及PDF在线预览的实现方式大集合
- pdf 转 高清图片
- JAVA中pdf转图片的方法
- 把office文档转换为html过程中的一些坑
- 将Office(如:Word、Excel、PPT 等)文件转html(通过OpenOffice实现)
- 仿百度文库解决方案之一:总体思路
工具网站
使用OpenOffice实现文档预览的更多相关文章
- JAVA+FlexPaper+OpenOffice+SWFTools文档预览
http://blog.csdn.net/core_star/article/details/10148047 1.软件环境: openoffice:启动openoffice服务:soffice.ex ...
- 在线文档预览方案-office web apps续篇
上一篇在线文档预览方案-office web apps发布后收到很多网友的留言提问,所以准备再写一篇,一来介绍一下域控服务器安装,总结一下大家问的多的问题,二来宣传预览服务安装与技术支持的事情. 阅读 ...
- 在线文档预览方案-office web apps
最近在做项目时,要在手机端实现在线文档预览的功能.于是百度了一下实现方案,大致是将文档转换成pdf,然后在通过插件实现预览.这些方案没有具体实现代码,也没有在线预览的地址,再加上项目时间紧迫.只能考虑 ...
- [转载]在线文档预览方案-Office Web Apps
最近在做项目时,要在手机端实现在线文档预览的功能.于是百度了一下实现方案,大致是将文档转换成pdf,然后在通过插件实现预览.这些方案没有具体实现代码,也没有在线预览的地址,再加上项目时间紧迫.只能考虑 ...
- 微软office web apps 服务器搭建之在线文档预览(一)
office web apps安装 系统要求为Windows Server 2012, 注意:转换文档需要两台服务器,一台为转换server,另外一台为域控server.(至于为什么要两台,这个请自行 ...
- 微软office web apps 服务器搭建之在线文档预览(二)
上一篇文章已经介绍了整个安装过程了.只要在浏览器中输入文档转换server的ip,会自动跳转,出现如下页面. 那么就可以实现本地文档预览了,你可以试试.(注意:是本地哦,路径不要写错,类似“\\fil ...
- 解决officeOnline文档预览服务器只能域名提交的限制Redirect
此项目是解决officeOnline文档预览只能用域名提交的限制 http://officeOnline文档预览域名或IP/op/generate.aspx // 微软原生页面 创建链接后会生成全屏预 ...
- 一文带你玩转对象存储COS文档预览
随着"互联网+"的发展,各行各业纷纷"去纸化",商务合同.会议纪要.组织公文.商品图片.培训视频.学习课件.随堂讲义等电子文档无处不在.而要查看文档一般需要先下 ...
- 秒级接入、效果满分的文档预览方案——COS文档预览
一.导语 说起 Microsoft Office 办公三件套,想必大家都不会陌生,社畜日常的工作或者生活中,多多少少遇到过这种情况: 本地创建的文档换一台电脑打开,就出现了字体丢失.排版混乱的情况 ...
随机推荐
- redis cluster可用性测试
上一节,我们用三台redis组成了cluster,现在我们停掉一台试试: 比较奇怪的是,在停掉其中一台服务器之前建立的链接仍然可以正常执行命令,当我们断开重连时,命令就都被拒绝了: 关联知识: 什么时 ...
- Potato家族本地提权分析
原文来自SecIN社区-作者:Zeva 0x00 前言 在实际渗透中,我们用到最多的就是Potato家族的提权.本文着重研究Potato家族的提权原理以及本地提权细节 0x01 原理讲解 1.利用Po ...
- python语法元素的名称
变量 什么是变量? """ 变量是保存和表示数据值的一种语法元素,在程序中十分常见.顾名思义,变量的值是可以改变的,能够通过赋值(使用等号"=")方式 ...
- oracle 导入导出表,库
Exp/Imp是oracle备份数据的两个命令行工具 1.本地数据库导入导出 1.导出 (运行---cmd中操作) exp 用户名/密码@数据库实例名file=本地存放路径 eg: exp jnjp/ ...
- WPF 学习笔记(一)
一.概述 WPF(Windows Presentation Foundation)是微软推出的基于Windows 的用户界面框架,随着.NET Framework 3.0发布第一个版本.它提供了统一的 ...
- cJSON的使用
1 安装cJSON github地址:https://github.com/DaveGamble/cJSON.git 下载完成后进入cJSON目录,执行下面命令生成Makefile文件 mkdir b ...
- Python中import模块时报SyntaxError: (unicode error)utf-8 codec can not decode 错误的解决办法
老猿有个通过UE编辑(其他文本编辑器一样有类似问题)的bmi.py文件,在Python Idle环境打开文件执行时没有问题,但import时报错: SyntaxError: (unicode erro ...
- ripple Failed to load resource: the server responded with a status of 404 (Not Found)
在VS2015中使用Cordova + typescript开发中,遇到个问题. 在javascript console 中提示: Failed to load resource: the serve ...
- starsWidth 和endWidth ie不兼容方案
if (typeof String.prototype.startsWith != 'function') { String.prototype.startsWith = function (pref ...
- 题解-ARC058D Iroha Loves Strings
题面 ARC058D Iroha Loves Strings 给定 \(n\) 个字符串,从中选出若干个按给出顺序连接起来,总长等于 \(m\),求字典序最小的,保证有解. 数据范围:\(1\le n ...