课程作业,准备做一个类似于豆丁之类的在线文库,解决方案也就是将文档(doc ppt xls)等转换成pdf,然后转成swf展示在页面中,今天下午经过研究,参考其他人的博客,实现了这个主要功能,这里也做下记录。

@肖恩也有梦想 http://www.cnblogs.com/luckyxiaoxuan/archive/2012/06/13/2548331.html 思路已经介绍的很详细了,下面我给出一个示例代码,来自http://blog.csdn.net/catoop/article/details/43150671 ,具体的可以去@肖恩也有梦想 查看。

我用线程池加了个多线程的支持,代码http://files.cnblogs.com/files/ywind/Convert.zip

import java.io.File;  

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant; /**
*
* 将jacob.dll放入JDK的bin目录下 把jacob.jar放入项目的buildPath中(web项目放到WEB-INF\lib目录下)
*
* @author 单红宇
*
*/
public class ConvertToPdf { /*转PDF格式值*/
private static final int wdFormatPDF = 17;
private static final int xlFormatPDF = 0;
private static final int ppFormatPDF = 32;
private static final int msoTrue = -1;
private static final int msofalse = 0; /*转HTML格式值*/
private static final int wdFormatHTML = 8;
private static final int ppFormatHTML = 12;
private static final int xlFormatHTML = 44; /*转TXT格式值*/
private static final int wdFormatTXT = 2; public boolean convert2PDF(String inputFile, String pdfFile) {
String suffix = getFileSufix(inputFile);
File file = new File(inputFile);
if (!file.exists()) {
System.out.println("文件不存在!");
return false;
}
if (suffix.equals("pdf")) {
System.out.println("PDF not need to convert!");
return false;
}
if (suffix.equals("doc") || suffix.equals("docx") || suffix.equals("txt")) {
return word2PDF(inputFile, pdfFile);
} else if (suffix.equals("ppt") || suffix.equals("pptx")) {
return ppt2PDF(inputFile, pdfFile);
} else if (suffix.equals("xls") || suffix.equals("xlsx")) {
return excel2PDF(inputFile, pdfFile);
} else {
System.out.println("文件格式不支持转换!");
return false;
}
} /**
* 获取文件后缀
*
* @param fileName
* @return
* @author SHANHY
*/
private String getFileSufix(String fileName) {
int splitIndex = fileName.lastIndexOf(".");
return fileName.substring(splitIndex + 1);
} /**
* Word文档转换
*
* @param inputFile
* @param pdfFile
* @author SHANHY
*/
private boolean word2PDF(String inputFile, String pdfFile) {
ComThread.InitSTA(); long start = System.currentTimeMillis();
ActiveXComponent app = null;
Dispatch doc = null;
try {
app = new ActiveXComponent("Word.Application");// 创建一个word对象
app.setProperty("Visible", new Variant(false)); // 不可见打开word
app.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏
Dispatch docs = app.getProperty("Documents").toDispatch();// 获取文挡属性 System.out.println("打开文档 >>> " + inputFile);
// Object[]第三个参数是表示“是否只读方式打开”
// 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
doc = Dispatch.call(docs, "Open", inputFile, false, true).toDispatch();
// 调用Document对象的SaveAs方法,将文档保存为pdf格式
System.out.println("转换文档 [" + inputFile + "] >>> [" + pdfFile + "]");
Dispatch.call(doc, "SaveAs", pdfFile, wdFormatPDF);//word保存为pdf格式宏,值为17
// Dispatch.call(doc, "ExportAsFixedFormat", pdfFile, wdFormatPDF); // word保存为pdf格式宏,值为17 long end = System.currentTimeMillis(); System.out.println("用时:" + (end - start) + "ms.");
return true;
} catch (Exception e) {
e.printStackTrace();
System.out.println("========Error:文档转换失败:" + e.getMessage());
} finally {
Dispatch.call(doc, "Close", false);
System.out.println("关闭文档");
if (app != null)
app.invoke("Quit", new Variant[] {});
}
// 如果没有这句话,winword.exe进程将不会关闭
ComThread.Release();
ComThread.quitMainSTA();
return false;
} /**
* PPT文档转换
*
* @param inputFile
* @param pdfFile
* @author SHANHY
*/
private boolean ppt2PDF(String inputFile, String pdfFile) {
ComThread.InitSTA(); long start = System.currentTimeMillis();
ActiveXComponent app = null;
Dispatch ppt = null;
try {
app = new ActiveXComponent("PowerPoint.Application");// 创建一个PPT对象
// app.setProperty("Visible", new Variant(false)); // 不可见打开(PPT转换不运行隐藏,所以这里要注释掉)
// app.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏
Dispatch ppts = app.getProperty("Presentations").toDispatch();// 获取文挡属性 System.out.println("打开文档 >>> " + inputFile);
// 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
ppt = Dispatch.call(ppts, "Open", inputFile,
true,// ReadOnly
true,// Untitled指定文件是否有标题
false// WithWindow指定文件是否可见
).toDispatch(); System.out.println("转换文档 [" + inputFile + "] >>> [" + pdfFile + "]");
Dispatch.call(ppt, "SaveAs", pdfFile, ppFormatPDF); long end = System.currentTimeMillis(); System.out.println("用时:" + (end - start) + "ms."); return true;
} catch (Exception e) {
e.printStackTrace();
System.out.println("========Error:文档转换失败:" + e.getMessage());
} finally {
Dispatch.call(ppt, "Close");
System.out.println("关闭文档");
if (app != null)
app.invoke("Quit", new Variant[] {});
}
ComThread.Release();
ComThread.quitMainSTA();
return false;
} /**
* Excel文档转换
*
* @param inputFile
* @param pdfFile
* @author SHANHY
*/
private boolean excel2PDF(String inputFile, String pdfFile) {
ComThread.InitSTA(); long start = System.currentTimeMillis();
ActiveXComponent app = null;
Dispatch excel = null;
try {
app = new ActiveXComponent("Excel.Application");// 创建一个PPT对象
app.setProperty("Visible", new Variant(false)); // 不可见打开
// app.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏
Dispatch excels = app.getProperty("Workbooks").toDispatch();// 获取文挡属性 System.out.println("打开文档 >>> " + inputFile);
// 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
excel = Dispatch.call(excels, "Open", inputFile, false, true).toDispatch();
// 调用Document对象方法,将文档保存为pdf格式
System.out.println("转换文档 [" + inputFile + "] >>> [" + pdfFile + "]");
// Excel 不能调用SaveAs方法
Dispatch.call(excel, "ExportAsFixedFormat", xlFormatPDF, pdfFile); long end = System.currentTimeMillis(); System.out.println("用时:" + (end - start) + "ms.");
return true;
} catch (Exception e) {
e.printStackTrace();
System.out.println("========Error:文档转换失败:" + e.getMessage());
} finally {
Dispatch.call(excel, "Close", false);
System.out.println("关闭文档");
if (app != null)
app.invoke("Quit", new Variant[] {});
}
ComThread.Release();
ComThread.quitMainSTA();
return false;
} /**
* 测试
*
* @param args
* @author SHANHY
*/
public static void main(String[] args) {
ConvertToPdf d = new ConvertToPdf();
d.convert2PDF("g:\\test.docx", "g:\\test.pdf");
d.convert2PDF("g:\\testppt.pptx", "g:\\testppt.pdf");
d.convert2PDF("g:\\testexcel.xlsx", "g:\\testexcel.pdf");
} }

在线文库解决方案Jacob+SwfTools+FlexPaper的更多相关文章

  1. 文档在线预览开源实现方案一:OpenOffice + SwfTools + FlexPaper

    在文档在线预览方面,项目组之前使用的是Microsoft office web apps, 由于该方案需要按照微软License付费,项目经理要我预研一个文档在线预览的开源实现方案.仔细钻入该需求发现 ...

  2. java实现附件预览(openoffice+swfTools+FlexPaper) (转载)

    下边例子是在网上找了一个网友做的例子,在次记录 1.概述 主要原理 1.通过第三方工具openoffice,将word.excel.ppt.txt等文件转换为pdf文件 2.通过swfTools将pd ...

  3. java实现附件预览(openoffice+swftools+flexpaper)

    先附上本人参考的文章,基于的 flexpaper版本 为 1.5,本人由于使用的是 2.1.9 ,故之后说明: 已经支持加载中文文件名 代码下载 1.概述 主要原理 1.通过第三方工具openoffi ...

  4. 浅谈基于WOPI协议实现跨浏览器的Office在线编辑解决方案

    如今,基于Web版的Office 在线预览与编辑功能已成为一种趋势,而关于该技术的实现却成为了国内大部份公司的技术挑战,挑战主要存在于两方面: 其一:目前国内乃至微软本身,还没有相对较为完善的解决方案 ...

  5. 使用jodconverter和swftools实现文件在线预览

    参考:仿百度文库解决方案(四)——利用JODConverter调用OpenOffice.org服务转换文档为PDF 文档在线预览主要用到如下两个工具 1,安装openoffice(同时下载jodcon ...

  6. PDF解决方案(4)--在线浏览

    相关专题链接 PDF解决方案(1)--文件上传 PDF解决方案(2)--文件转PDF PDF解决方案(3)--PDF转SWF PDF解决方案(4)--在线浏览 前言:上一篇主要提到了PDF在线浏览的各 ...

  7. FlexPaper+SWFTool+操作类=在线预览PDF(转)

    引言 由于客户有在线预览PDF格式的需求,在网上找了一下解决方案,觉得FlexPaper用起来还是挺方便的,flexpaper是将pdf转换为swf格式的文件预览的,所以flexpaper一般和swf ...

  8. FlexPaper+SWFTool+操作类=在线预览PDF

    引言 由于客户有在线预览PDF格式的需求,在网上找了一下解决方案,觉得FlexPaper用起来还是挺方便的,flexpaper是将pdf转换为swf格式的文件预览的,所以flexpaper一般和swf ...

  9. iStylePDF安全电子文档解决方案之电子合同在线订立

    交易是商业世界不可或缺的一部分,而签名是交易的凭证.可是,尽管互联网和IT技术已经很发达,但每逢遇到签名,还是得用最原始的方法——握笔写字.与如今走到哪都能听到“互联网+”相比有点不合潮流,通过电子签 ...

随机推荐

  1. 1、Locust压力测试环境搭建

    环境准备:阿里云服务器一台.python2.7.pip Locust 介绍 Locust 是一个开源负载测试工具.使用 Python 代码定义用户行为,也可以仿真百万个用户. Locust 简单易用, ...

  2. 面向对象(五)——isinstance与issubclass、反射、内置方法

    isinstance与issubclass.反射.内置方法 一.isinstance与issubclass方法 1.isinstance是用来判断对象是否是某个类 isinstance(obj,cla ...

  3. react map循环数据 死循环

    项目条件:react es6 antidesign 已在commonState中获取到list,但是在循环map填充DOM的时候陷入死循环. 原因:因为是子组件 ,在父组件请求数据的时候 有个时差过程 ...

  4. 自从阿里买了Flink母公司以后,你不懂Flink就out了!

    个免费报名权限 Ps:小助理手动给大家发送资料,精力有限,仅限前100名免费领取,这份资料对于想要提升大数据技能进阶的小伙伴来说,将会是一份不可或缺的宝贵资料. 特别感谢飞总的部分原创支持!

  5. Python移动自动化测试面试✍✍✍

    Python移动自动化测试面试  整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课程本身没问题,大家看的时候可以 ...

  6. hdu6331 /// Floyd+分块DP

    题目大意: 给定单向图的n m 为点数和单向边数 接下来m行给定 u v w 为边的起点终点和长度 给定q 为询问个数 接下来q行给定 x y k 求从x到y至少经过k条边的最短路长度 https:/ ...

  7. 在Nginx/Tengine服务器上安装证书

    可以从SSL证书服务控制台下载证书安装到Nginx/Tengine服务器上. 环境:以CentOS 7.Nginx 1.15.6为例. 证书名称以domain name为示例,如证书文件名称为doma ...

  8. 自定义实现系统max方法

    function MyMath(){ //添加了一个方法 this.getMax=function(){ //所有数字中的最大值 var max=arguments[0]; for(var i=0;i ...

  9. centos7 dns(bind)安装配置

    yum install -y bind bind-chroot bind-utils chroot是通过相关文件封装在一个伪根目录内,已达到安全防护的目的,一旦程序被攻破,将只能访问伪根目录内的内容, ...

  10. JUC 一 ReentrantReadWriteLock

    java.util.concurrent.locks ReentrantLock是独占锁,一种相对比较保守的锁策略,在这种情况下任何"读/读"."读/写".&q ...