• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

无信不立

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

java实现MsOffice文档向pdf文档转化

本篇文档实现功能,将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目录下。

 

 

第三步:调用MsOffice2Pdf类的静态方法,实现MsOffice文档转换。

 

MsOffice2Pdf类的源代码

  1 public class MsOffice2Pdf {
  2     
  3 
  4     static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。  
  5     public static final int wdFormatPDF = 17;// word转PDF 格式  
  6     public static final int ppSaveAsPDF = 32;// ppt 转PDF 格式  
  7     
  8     
  9     
 10     /**
 11      * 将指定路径的word文档转换成指定路径的pdf文档
 12      * 此处路径为绝对路径
 13     * @Title: word2PDF 
 14     * @Description: TODO(这里用一句话描述这个方法的作用) 
 15     * @param inputFile
 16     * @param pdfFile
 17     * @return void    返回类型 
 18     * @author 尚晓飞
 19     * @date 2014-8-15 上午10:25:47
 20      */
 21     public static void word2PDF(String inputFile,String pdfFile){
 22         System.out.println("启动Word");  
 23         long start = System.currentTimeMillis();  
 24         ActiveXComponent app = null;  
 25         try {  
 26             app = new ActiveXComponent("Word.Application");  
 27             app.setProperty("Visible", false);  
 28   
 29             Dispatch docs = app.getProperty("Documents").toDispatch();  
 30             System.out.println("打开文档" +inputFile);  
 31             Dispatch doc = Dispatch.call(docs,//  
 32                     "Open", //  
 33                     inputFile,// FileName  
 34                     false,// ConfirmConversions  
 35                     true // ReadOnly  
 36                     ).toDispatch();  
 37   
 38             System.out.println("转换文档到PDF " + pdfFile);  
 39             File tofile = new File(pdfFile);  
 40             if (tofile.exists()) {  
 41                 tofile.delete();  
 42             }  
 43             Dispatch.call(doc,//  
 44                     "SaveAs", //  
 45                     pdfFile, // FileName  
 46                     wdFormatPDF);  
 47   
 48             Dispatch.call(doc, "Close", false);  
 49             long end = System.currentTimeMillis();  
 50             System.out.println("转换完成..用时:" + (end - start) + "ms.");  
 51         } catch (Exception e) {  
 52             System.out.println("========Error:文档转换失败:" + e.getMessage());  
 53         } finally {  
 54             if (app != null)  
 55                 app.invoke("Quit", wdDoNotSaveChanges);  
 56         }  
 57         
 58     }
 59     
 60     
 61     /**
 62      * 将ppt格式的msoffice文档转换成pdf格式的文档
 63     * @Title: ppt2pdf 
 64     * @Description: TODO(这里用一句话描述这个方法的作用) 
 65     * @param inputFile
 66     * @param pdfFile
 67     * @return void    返回类型 
 68     * @author 尚晓飞
 69     * @date 2014-8-18 下午2:00:21
 70      */
 71     public static void ppt2pdf(String inputFile,String pdfFile){  
 72         System.out.println("启动PPT");  
 73         long start = System.currentTimeMillis();  
 74         ActiveXComponent app = null;  
 75         try {  
 76             app = new ActiveXComponent("Powerpoint.Application");  
 77             Dispatch presentations = app.getProperty("Presentations").toDispatch();  
 78             System.out.println("打开文档" + inputFile);  
 79             Dispatch presentation = Dispatch.call(presentations,//  
 80                     "Open",   
 81                     inputFile,// FileName  
 82                     true,// ReadOnly  
 83                     true,// Untitled 指定文件是否有标题。  
 84                     false // WithWindow 指定文件是否可见。  
 85                     ).toDispatch();  
 86   
 87             System.out.println("转换文档到PDF " + pdfFile);  
 88             File tofile = new File(pdfFile);  
 89             if (tofile.exists()) {  
 90                 tofile.delete();  
 91             }  
 92             Dispatch.call(presentation,//  
 93                     "SaveAs", //  
 94                     pdfFile, // FileName  
 95                     ppSaveAsPDF);  
 96   
 97             Dispatch.call(presentation, "Close");  
 98             long end = System.currentTimeMillis();  
 99             System.out.println("转换完成..用时:" + (end - start) + "ms.");  
100         } catch (Exception e) {  
101             System.out.println("========Error:文档转换失败:" + e.getMessage());  
102         } finally {  
103             if (app != null) app.invoke("Quit");  
104         }  
105     }  
106     
107     
108 
109 }
View Code

 

 

 

posted on 2014-08-15 16:14  无信不立  阅读(452)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3