java实现MsOffice文档向pdf文档转化
2014-08-15 16:14
543 查看
本篇文档实现功能,将word和ppt文档的文件转化成pdf格式的文档
应用到jacob
第一步:下载压缩包
(1)jacob官网下载jacob压缩包
(2)网址:http://sourceforge.net/projects/jacob-project/
第二步:配置环境并导入jar包
(1)将下载好的压缩包解压
(2)将jacob.jar包导入项目中
(3)将jacob-1.18-M2-x64.dll和jacob-1.18-M2-x86.dll文件复制粘贴到jdk安装目录bin目录下,jdk安装目录jre的bin目录下,系统盘C:\Windows\System32目录下。
[b]第三步:调用MsOffice2Pdf类的静态方法,实现MsOffice文档转换。[/b]
[b]MsOffice2Pdf类的源代码[/b]
View Code
应用到jacob
第一步:下载压缩包
(1)jacob官网下载jacob压缩包
(2)网址:http://sourceforge.net/projects/jacob-project/
第二步:配置环境并导入jar包
(1)将下载好的压缩包解压
(2)将jacob.jar包导入项目中
(3)将jacob-1.18-M2-x64.dll和jacob-1.18-M2-x86.dll文件复制粘贴到jdk安装目录bin目录下,jdk安装目录jre的bin目录下,系统盘C:\Windows\System32目录下。
[b]第三步:调用MsOffice2Pdf类的静态方法,实现MsOffice文档转换。[/b]
[b]MsOffice2Pdf类的源代码[/b]
public class MsOffice2Pdf { static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。 public static final int wdFormatPDF = 17;// word转PDF 格式 public static final int ppSaveAsPDF = 32;// ppt 转PDF 格式 /** * 将指定路径的word文档转换成指定路径的pdf文档 * 此处路径为绝对路径 * @Title: word2PDF * @Description: TODO(这里用一句话描述这个方法的作用) * @param inputFile * @param pdfFile * @return void 返回类型 * @author 尚晓飞 * @date 2014-8-15 上午10:25:47 */ public static void word2PDF(String inputFile,String pdfFile){ System.out.println("启动Word"); long start = System.currentTimeMillis(); ActiveXComponent app = null; try { app = new ActiveXComponent("Word.Application"); app.setProperty("Visible", false); Dispatch docs = app.getProperty("Documents").toDispatch(); System.out.println("打开文档" +inputFile); Dispatch doc = Dispatch.call(docs,// "Open", // inputFile,// FileName false,// ConfirmConversions true // ReadOnly ).toDispatch(); System.out.println("转换文档到PDF " + pdfFile); File tofile = new File(pdfFile); if (tofile.exists()) { tofile.delete(); } Dispatch.call(doc,// "SaveAs", // pdfFile, // FileName wdFormatPDF); Dispatch.call(doc, "Close", false); long end = System.currentTimeMillis(); System.out.println("转换完成..用时:" + (end - start) + "ms."); } catch (Exception e) { System.out.println("========Error:文档转换失败:" + e.getMessage()); } finally { if (app != null) app.invoke("Quit", wdDoNotSaveChanges); } } /** * 将ppt格式的msoffice文档转换成pdf格式的文档 * @Title: ppt2pdf * @Description: TODO(这里用一句话描述这个方法的作用) * @param inputFile * @param pdfFile * @return void 返回类型 * @author 尚晓飞 * @date 2014-8-18 下午2:00:21 */ public static void ppt2pdf(String inputFile,String pdfFile){ System.out.println("启动PPT"); long start = System.currentTimeMillis(); ActiveXComponent app = null; try { app = new ActiveXComponent("Powerpoint.Application"); Dispatch presentations = app.getProperty("Presentations").toDispatch(); System.out.println("打开文档" + inputFile); Dispatch presentation = Dispatch.call(presentations,// "Open", inputFile,// FileName true,// ReadOnly true,// Untitled 指定文件是否有标题。 false // WithWindow 指定文件是否可见。 ).toDispatch(); System.out.println("转换文档到PDF " + pdfFile); File tofile = new File(pdfFile); if (tofile.exists()) { tofile.delete(); } Dispatch.call(presentation,// "SaveAs", // pdfFile, // FileName ppSaveAsPDF); Dispatch.call(presentation, "Close"); long end = System.currentTimeMillis(); System.out.println("转换完成..用时:" + (end - start) + "ms."); } catch (Exception e) { System.out.println("========Error:文档转换失败:" + e.getMessage()); } finally { if (app != null) app.invoke("Quit"); } } }
View Code
相关文章推荐
- java实现MsOffice文档向pdf转化之OpenOffice软件
- 将PPT转化为PDF我的JAVA代码实现!
- 用java实现文本文档的ANSI编码转化为UTF-8
- Java实现HTML代码生成PDF文档
- Java实现web在线预览office文档与pdf文档实例
- Java实现word文档转pdf/html在线预览
- Java平台要实现类似豆丁百度文科的文档在线阅读,总体思路是讲doc docx等文件格式利用jcom转换成pdf再用swftools转为swf。再用flexpaper组件显示swf。
- Java实现HTML代码生成PDF文档
- Java使用Flying Saucer实现HTML代码生成PDF文档
- Java使用PDFBox开发包实现对PDF文档内容编辑与保存
- java实现从url路径中下载pdf文档到本地
- Java使用PDFBox开发包实现对PDF文档内容编辑与保存
- Java实现office文档与pdf文档的在线预览功能
- Java实现pdf转化为png图片
- Java使用wkhtmltox实现HTML代码生成PDF文档或者图片
- Java实现HTML代码生成PDF文档
- java实现HTml转化为Doc文档
- (四)java实现文档的在线浏览-使用swftools将pdf转换为swf
- (五)java实现文档的在线浏览-转换pdf的同时转为swf
- java实现 wps 转换office文档到pdfandswf