课程作业,准备做一个类似于豆丁之类的在线文库,解决方案也就是将文档(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. checkbox、radio使用jquery改变状态以及其他操作

    $('input[type=checkbox]:checked').each(function(index,elem){ $(elem).attr("checked",false) ...

  2. 使用CSS将图片转换成黑白(灰色、置灰) & 毛玻璃效果

    法1⃣️: IE浏览器: filter: gray; 其他浏览器: .gray { -webkit-filter: grayscale(100%); -moz-filter: grayscale(10 ...

  3. 用注解实现SpringMvc

    在第一次完成spirngmvc代码的基础上: 开始时代码 index.jsp <%@ page contentType="text/html;charset=UTF-8" l ...

  4. Hadoop 基础知识

    Hadoop 数据是存储在HDFS, Mapreduce 是一种计算框架,负责计算处理. HDFS上的数据存储默认是本地节点数据一份,同一机架不同节点一份,不同机架不同节点一份.默认是存储3份 HDF ...

  5. [已解决]报错:execjs._exceptions.ProgramError: ReferenceError: window is not defined

    问题: execjs._exceptions.ProgramError: ReferenceError: window is not defined 解决: 定义一个就行 var window = { ...

  6. 小程序登录时如何获取input框中的内容

    最近写小程序项目遇到一些问题,今天整理下这些问题的解决方法,希望对用户有帮助.下面是登录页,点击登录时获取input框中的值, 效果如下: wxml布局如下: <view > <in ...

  7. 解决MySQL登录密码正确却提示错误-1045的方法

    MySQL密码正确却无法本地登录-1045 Access denied for user 'root'@'localhost' (using password:YES MySQL密码正确却无法本地登录 ...

  8. Tomcat 调优的技巧

    转载:www.cnblogs.com/wangsen, https://mp.weixin.qq.com/s/WrIsOOyR7o4SwSXMT0Zecg 最近,看到一篇讲述 Tomcat 调优的文章 ...

  9. TPCx-BB官宣最新世界纪录,阿里巴巴计算力持续突破

    2019年9月17日,TPC官宣Alibaba Cloud MaxCompute认证结果.同月26日,杭州云栖大会阿里巴巴宣布了这一成绩,飞天大数据平台计算引擎MaxCompute成为全球首个TPCx ...

  10. IDEA maven package失败

    选中要打包的模块,选择工具栏中的Build,选择Rebuild Module xxx,重新打包