java将office文档pdf文档转换成swf文件在线预览
第一步,安装openoffice.org
openoffice.org是一套sun的开源office办公套件,能在widows,linux,solaris等操作系统上执行。
主要模块有writer(文本文档),impress(演示文稿),Calc(电子表格),Draw(绘图),Math(公式),base(数据库)
笔者下载的是openoffice.org 3.3.0。下载完直接安装即可。
但是,我们还需要启动openoffice server。有两种做法:
1.以命令行方式启动openoffice server,缺点是每次系统重启,都需要手动去把openoffice server启动。
2.将openoffice server作为操作系统的服务启动,既然成为了系统服务,就可以设定开机自动启动了。
我们先来看第一种方式,
1.以命令行方式启动openoffice server
在cmd命令下,cd opeonofiice的安装路径/program 如:cd c:\program files\openoffice.org 3\program\soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
2.以系统服务的方式启动
这里我们还需要Windows Resource Kit tools ,将openoffice server设为系统服务。
Windows Resource Kit tools 是微软专为管理人员、开发人员和高级用户开发的,包括管理活动目录、组策略、TCP/IP网络、注册表、系统安全、监测等涉及Windows Server 2003 操作系统的其它很多方面的非常规安装的工具组件。Resource Kit Tools for XP的发布使得XP用户也能使用Resource Kit Tools对这些问题进行处理。
下载windows resource kit tools,我们进行默认安装。
1.打开Windows Resource Kit Tools
在Command Shell执行以下命令:
"C:\Program Files\Windows Resource Kits\Tools\instsrv" OpenOfficeUnoServer "C:\Program Files\Windows Resource Kits\Tools\srvany.exe"
打开 管理工具->服务 可以找到以 OpenOfficeUnoServer 命名的服务
2.打开注册表寻找以下路径
HKEY_LOCAL_MACHINE -> SYSTEM ->ControlSet001 ->Services ->OpenOfficeUnoServer
新建项 Parameters,在该项下添加两个字符串值:
key:Application
value:C:\Program Files\OpenOffice.org 3\program\soffice.exe
key:AppParameters
value:-invisible -headless -accept=socket,host=127.0.0.1,port=8100;urp; -nofirststartwizard
3.在服务控制台,启动 openoffice 服务
4.在CMD中用以下命令查看8100是否已被监听:netstat -anop tcp
这样OpenOffice3.0就以服务方式运行在Windows系统上了。(使用cmd命令:netstat -anp tcp查看8100端口是否工作)
然後可以通过socket方式连接openOffice,以使用openoffice提供的某些服务,如文件转换服务,ms office转pdf等等。
开源项目 JODConverter 就是结合openoffice来进行文档转换的java组件。
另外有一个命令行工具swftools,该工具可以将pdf转换为swf格式的文档,提供给ie客戶端流览。
另外,我们可以将该配置用bat文件来快速实现,运行前请先修改相应目录参数:
openoffice service.bat文件
"C:\Program Files\Windows Resource Kits\Tools\instsrv" OpenOfficeUnoServer "C:\Program Files\Windows Resource Kits\Tools\srvany.exe"
reg add
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\OpenOfficeUnoServer\Parameters
/ve /d
reg add
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\OpenOfficeUnoServer\Parameters
/v Application /t REG_SZ /d "C:\Program Files\OpenOffice.org
3\program\soffice.exe"
reg add
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\OpenOfficeUnoServer\Parameters
/v AppParameters /t REG_SZ /d "-invisible -headless
-accept=socket,host=127.0.0.1,port=8100;urp; -nofirststartwizard"
第二步,使用JODConverter将office文档转换为pdf
JODConverter是一个java的OpenDucument文件转换器,可以进行许多文件格式的转换,它利用
OpenOffice来进行转换工作,它能进行以下的转换工作:
1.Microsoft Office格式转换为OpenDucument,以及OpenDucument转换为Microsoft Office
2.OpenDucument转换为PDF,Word、Excel、PowerPoint转换为PDF,RTF转换为PDF等。
它是一个开源项目。
我的项目是在MyEclipse下开发的。
下载最新版的jodconverter-2.2.2,把lib文件夹的包导入到你的DocConverter项目的lib文件夹内。
(假设你的项目是DocConverter)
新建DOC2PDFUtil.java
package com.iori.webapp.util;
import java.io.File;
import java.io.IOException;
import java.net.ConnectException;
import java.util.Date;
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;
public class DOC2PDFUtil extends
java.lang.Thread {
private File inputFile;// 需要转换的文件
private File outputFile;// 输出的文件
public DOC2PDFUtil(File inputFile, File outputFile) {
this.inputFile = inputFile;
this.outputFile = outputFile;
}
public void docToPdf() {
Date start = new Date();
OpenOfficeConnection connection = new
SocketOpenOfficeConnection(8100);
try {
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
} catch (ConnectException cex) {
cex.printStackTrace();
} finally {
// close the connection
if (connection != null) {
connection.disconnect();
connection = null;
}
}
}
/**
* 由于服务是线程不安全的,所以……需要启动线程
*/
public void run() {
this.docToPdf();
}
public File getInputFile() {
return inputFile;
}
public void setInputFile(File inputFile) {
this.inputFile = inputFile;
}
public File getOutputFile() {
return outputFile;
}
public void setOutputFile(File outputFile) {
this.outputFile = outputFile;
}
/**
* 测试main方法
* @param args
*/<