在线文库解决方案Jacob+SwfTools+FlexPaper
2024-09-30 06:37:53
课程作业,准备做一个类似于豆丁之类的在线文库,解决方案也就是将文档(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");
} }
最新文章
- CSS3初学篇章_3(属性选择符/字体样式/元素样式)
- Clustering with the ArcGIS API for Flex
- 转载 asp.net的Request.ServerVariables参数说明
- Apache HTTPServer与JBoss/Tomcat的整合与请求分发
- 【转】Java集合框架List,Map,Set等全面介绍
- ThrottleStop
- Entity Framework mvc Code First data migration
- velocity基本语法
- svn的使用详细说明
- 利用StringBuffer向字符串特定的重复子字符串插入数据
- C++学习日记(一)————类与对象
- 部署 k8s Cluster(上)- 每天5分钟玩转 Docker 容器技术(118)
- git出现错误原因解释
- WdatePicker()时间控制方式(转载+原创)
- C_C++变量命名规则
- PAM 認 證 模 組
- mysql主从(主备)同步一键配置,配自动检测功能
- Lucene suggest [转]
- Windows向虚拟机Linux传输文件方法
- 十天精通CSS3(7)