(三)java实现文档的在线浏览-使用JODConverter将office文档转换为pdf
2015-05-28 10:44
916 查看
JODConverter是一个java的OpenDucument文件转换器,可以进行许多文件格式的转换,它利用
OpenOffice来进行转换工作,它能进行以下的转换工作:
1.Microsoft Office格式转换为OpenDucument,以及OpenDucument转换为Microsoft Office
2.OpenDucument转换为PDF,Word、Excel、PowerPoint转换为PDF,RTF转换为PDF等。
它是一个开源项目。
下载最新版的jodconverter-2.2.2,把lib文件夹的包导入到你的项目的lib文件夹内。
新建转换工具类:DOC2PDFUtil.java
在jsp中执行
新建MyDOC2PDFTest.jsp
在项目DocConverter根目录,右键属性 - >Run as - >MyEclipse Server Application
发布到之前安装的Tomcat 6.0的根目录,然后用url路径访问:Http://localhost:8080/项目名/MyDOC2PDFTest.jsp 进行测试。
JODConverter将office文档转换pdf,用到的代码如下:
OpenOffice来进行转换工作,它能进行以下的转换工作:
1.Microsoft Office格式转换为OpenDucument,以及OpenDucument转换为Microsoft Office
2.OpenDucument转换为PDF,Word、Excel、PowerPoint转换为PDF,RTF转换为PDF等。
它是一个开源项目。
下载最新版的jodconverter-2.2.2,把lib文件夹的包导入到你的项目的lib文件夹内。
新建转换工具类: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 */ public static void main(String[] args) { File inputFile = 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
<%@ page import="java.io.*"%> <%@ page import="com.artofsolving.jodconverter.openoffice.connection.*"%> <%@ page import="com.artofsolving.jodconverter.openoffice.connection.*"%> <%@ page import="com.artofsolving.jodconverter.openoffice.converter.*"%> <%@ page import="com.artofsolving.jodconverter.*"%> <%@ page import="java.util.*"%> <%@ page import="com.iori.webapp.util.*"%> <% File inputFile = new File("c://temp//333.xls"); File outputFile = new File("c://temp//333.pdf"); DOC2PDFUtil dp=new DOC2PDFUtil(inputFile,outputFile); dp.start(); %> <!-- 下面这些html可以去掉 --> <html> <head><title>Simple jsp page</title></head> <body>Place your content here</body> </html>
在项目DocConverter根目录,右键属性 - >Run as - >MyEclipse Server Application
发布到之前安装的Tomcat 6.0的根目录,然后用url路径访问:Http://localhost:8080/项目名/MyDOC2PDFTest.jsp 进行测试。
JODConverter将office文档转换pdf,用到的代码如下:
File inputFile = new File("c://temp//333.xls"); File outputFile = new File("c://temp//333.pdf"); // 链接 一个运行在8100端口的OpenOffice.org 实例 OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); connection.connect(); // 创建一个converter对象并转换格式 DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(inputFile, outputFile); // 关闭连接 connection.disconnect();
相关文章推荐
- (四)java实现文档的在线浏览-使用swftools将pdf转换为swf
- (五)java实现文档的在线浏览-转换pdf的同时转为swf
- 仿百度文库方案[openoffice.org 3+swftools+flexpaper] 使用JODConverter将office文档转换为pdf
- 文档在线预览(二)-使用JODConverter转换文档为PDF
- 仿百度文库方案[openoffice.org 3+swftools+flexpaper](三) 之 使用JODConverter将office文档转换为pdf
- 仿百度文库方案[openoffice.org 3+swftools+flexpaper](三) 之 使用JODConverter将office文档转换为pdf
- 在禅道中实现WORD等OFFICE文档转换为PDF进行在线浏览
- 使用jodconverter-3.0-beta-2.jar 转换Office 文档为 PDF
- 仿百度文库方案[openoffice.org 3+swftools+flexpaper](三) 之 使用JODConverter将office文档转换为pdf
- 仿百度文库方案[openoffice.org 3+swftools+flexpaper](三) 之 使用JODConverter将office文档转换为pdf
- Java平台要实现类似豆丁百度文科的文档在线阅读,总体思路是讲doc docx等文件格式利用jcom转换成pdf再用swftools转为swf。再用flexpaper组件显示swf。
- Java平台要实现类似豆丁百度文科的文档在线阅读,总体思路是讲doc docx等文件格式利用jcom转换成pdf再用swftools转为swf。再用flexpaper组件显示swf。
- java将office文档pdf文档转换成swf文件在线预览
- 利用jodconverter+openoffice+flexpaper实现的在线文档系统核心demo
- 用java写的jodconverter借用openoffice来转换office成pdf的示例代码
- 仿百度文库解决方案(四)——利用JODConverter调用OpenOffice.org服务转换文档为PDF
- 使用JODConverter转换文档为PDF
- java将office文档pdf文档转换成swf文件在线预览
- java将office文档pdf文档转换成swf文件在线预览
- java将office文档pdf文档转换成swf文件在线预览