1、java将office文档pdf文档转换成swf文件在线预览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.以命令行方式启动o
2、penoffice server,缺点是每次系统重启,都需要手动去把openoffice server启动。 2.将openoffice server作为操作系统的服务启动,既然成为了系统服务,就可以设定开机自动启动了。 我们先来看第一种方式,1.以命令行方式启动openoffice server在cmd命令下,cd opeonofiice的安装路径/program 如:cd c:program filesopenoffice.org 3programsoffice -headless -accept=socket,host=127.0.0.1,port=8100;urp; -nofirsts
3、tartwizard2.以系统服务的方式启动 这里我们还需要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 ki
4、t tools,我们进行默认安装。 1.打开Windows Resource Kit Tools 在Command Shell执行以下命令:C:Program FilesWindows Resource KitsToolsinstsrv OpenOfficeUnoServer C:Program FilesWindows Resource KitsToolssrvany.exe打开 管理工具-服务 可以找到以 OpenOfficeUnoServer 命名的服务 2.打开注册表寻找以下路径 HKEY_LOCAL_MACHINE - SYSTEM -ControlSet001 -Services
5、-OpenOfficeUnoServer新建项 Parameters,在该项下添加两个字符串值:key:Application value:C:Program FilesOpenOffice.org 3programsoffice.exe key:AppParametersvalue:-invisible -headless -accept=socket,host=127.0.0.1,port=8100;urp; -nofirststartwizard 3.在服务控制台,启动 openoffice 服务 4.在CMD中用以下命令查看8100是否已被监听:netstat -anop tcp这样O
6、penOffice3.0就以服务方式运行在Windows系统上了。(使用cmd命令:netstat -anp tcp查看8100端口是否工作)然後可以通过socket方式连接openOffice,以使用openoffice提供的某些服务,如文件转换服务,ms office转pdf等等。开源项目 JODConverter 就是结合openoffice来进行文档转换的java组件。另外有一个命令行工具swftools,该工具可以将pdf转换为swf格式的文档,提供给ie客戶端流览。另外,我们可以将该配置用bat文件来快速实现,运行前请先修改相应目录参数: openoffice service.ba
7、t文件 C:Program FilesWindows Resource KitsToolsinstsrv OpenOfficeUnoServer C:Program FilesWindows Resource KitsToolssrvany.exe reg add HKEY_LOCAL_MACHINESYSTEMControlSet001servicesOpenOfficeUnoServerParameters /ve /d reg add HKEY_LOCAL_MACHINESYSTEMControlSet001servicesOpenOfficeUnoServerParameters /v
8、 Application /t REG_SZ /d C:Program FilesOpenOffice.org 3programsoffice.exe reg add HKEY_LOCAL_MACHINESYSTEMControlSet001servicesOpenOfficeUnoServerParameters /v AppParameters /t REG_SZ /d -invisible -headless -accept=socket,host=127.0.0.1,port=8100;urp; -nofirststartwizard第二步,使用JODConverter将office文
9、档转换为pdfJODConverter是一个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文件夹的包导入到你的DocConver
10、ter项目的lib文件夹内。(假设你的项目是DocConverter)新建DOC2PDFUtil.javapackage com.iori.webapp.util;import java.io.File; import java.io.IOException;import .ConnectException; import java.util.Date; import com.artofsolving.jodconverter.DocumentConverter; import com.artofsolving.jodconverter.openoffice.connection.OpenOf
11、ficeConnection; 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;/ 输
12、出的文件 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 OpenO
13、fficeDocumentConverter(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
14、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 */ public static void main(String args) File i
15、nputFile = new File(c:/temp/333.xls); File outputFile = new File(c:/temp/333.pdf); DOC2PDFUtil dp=new DOC2PDFUtil(inputFile,outputFile); dp.start(); 在DOC2PDFUtil.java,右键属性 - Run as - Java Application ,输出main的测试结果。在jsp中执行新建MyDOC2PDFTest.jsp Simple jsp page Place your content here在项目DocConverter根目录,右键
16、属性 - Run as - MyEclipse Server Application发布到之前安装的Tomcat 6.0的根目录,然后用url路径访问:Http:/localhost:8080/DocConverter/MyDOC2PDFTest.jsp进行测试。JODConverter将office文档转换pdf,用到的代码如下:File inputFile = new File(c:/temp/333.xls);File outputFile = new File(c:/temp/333.pdf); / 链接 一个运行在8100端口的OpenOffice.org 实例OpenOfficeC
17、onnection connection = new SocketOpenOfficeConnection(8100);connection.connect(); / 创建一个converter对象并转换格式DocumentConverter converter = new OpenOfficeDocumentConverter(connection);converter.convert(inputFile, outputFile); / 关闭连接connection.disconnect();第三步,使用swftools将pdf转换为swf建议下载swftools-0.9.1,笔者起先下载的
18、是最新版的swftools-1.0版。貌似转换时出错,缺少什么组件。 继续笔者的DocConverter项目。笔者使用的开发环境是MyEclipse 9.0。新建PDF2SWFUtil.javapackage com.iori.webapp.util;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;public class PDF2SWFUtil /* * 利用SWFTools工具将pdf转换成swf,转换完后
19、的swf文件与pdf同名 * author iori * param fileDir PDF文件存放路径(包括文件名) * param exePath 转换器安装路径 * throws IOException */ public static synchronized void pdf2swf(String fileDir, String exePath) throws IOException /文件路径 String filePath = fileDir.substring(0, fileDir.lastIndexOf(/); /文件名,不带后缀 String fileName = file
20、Dir.substring(filePath.length() + 1), fileDir.lastIndexOf(.); Process pro = null; if (isWindowsSystem() /如果是windows系统 /命令行命令 String cmd = exePath + + fileDir + -o + filePath + / + fileName + .swf; /Runtime执行后返回创建的进程对象 pro = Runtime.getRuntime().exec(cmd); else /如果是linux系统,路径不能有空格,而且一定不能用双引号,否则无法创建进程
21、 String cmd = new String3; cmd0 = exePath; cmd1 = fileDir; cmd2 = filePath + / + fileName + .swf; /Runtime执行后返回创建的进程对象 pro = Runtime.getRuntime().exec(cmd); /非要读取一遍cmd的输出,要不不会flush生成文件(多线程) new DoOutput(pro.getInputStream().start(); new DoOutput(pro.getErrorStream().start(); try /调用waitFor方法,是为了阻塞当前
22、进程,直到cmd执行完 pro.waitFor(); catch (InterruptedException e) e.printStackTrace(); /* * 判断是否是windows操作系统 * author iori * return */ private static boolean isWindowsSystem() String p = System.getProperty(os.name); return p.toLowerCase().indexOf(windows) = 0 ? true : false; /* * 多线程内部类 * 读取转换时cmd进程的标准输出流和错
23、误输出流,这样做是因为如果不读取流,进程将死锁 * author iori */ private static class DoOutput extends Thread public InputStream is; /构造方法 public DoOutput(InputStream is) this.is = is; public void run() BufferedReader br = new BufferedReader(new InputStreamReader(this.is); String str = null; try /这里并没有对流的内容进行处理,只是读了一遍 whil
24、e (str = br.readLine() != null); catch (IOException e) e.printStackTrace(); finally if (br != null) try br.close(); catch (IOException e) e.printStackTrace(); /* * 测试main方法 * param args */ public static void main(String args) /转换器安装路径 String exePath = c:/Program Files/SWFTools/pdf2swf.exe; try PDF2S
25、WFUtil.pdf2swf(c:/temp/333.pdf, exePath); catch (IOException e) System.err.println(转换出错!); e.printStackTrace(); 在PDF2SWFUtil.java,右键属性 - Run as - Java Application ,输出main的测试结果。在jsp中执行新建MyPDF2SWFTest.jsp Simple jsp page Place your content here在项目DocConverter根目录,右键属性 - Run as - MyEclipse Server Application发布到之前安装的Tomcat 6.0的根目录,然后用url路径访问:Http:/localhost:8080/DocConverter/MyPDF2SWFTest.jsp进行测试。第四步,office文档转为pdf,同时进一步转为swf网上资料有很多office文档转为pdf,pdf转为swf,但都是单步转换。关
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1