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


    本篇文档实现功能,将word和ppt文档的文件转化成pdf格式的文档

    应用到jacob

     

    第一步:下载压缩包

    1jacob官网下载jacob压缩包

    2)网址:http://sourceforge.net/projects/jacob-project/

     

     

    第二步:配置环境并导入jar

     1)将下载好的压缩包解压

     (2)将jacob.jar包导入项目中

     (3)将jacob-1.18-M2-x64.dlljacob-1.18-M2-x86.dll文件复制粘贴到jdk安装目录bin目录下,jdk安装目录jrebin目录下,系统盘C:WindowsSystem32目录下。

     

     

    第三步:调用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

     

  • 相关阅读:
    数据的输入输出
    运算符和表达式
    深入理解Magento – 第三章 – 布局,块和模板
    压抑中......
    css控制图片自适应大小
    问来北京的追求是什么
    magento目录结构精编版
    无所事事的日子。
    jQuery实现等比例缩放大图片让大图片自适应页面布局
    MVC 小常识
  • 原文地址:https://www.cnblogs.com/shangxiaofei/p/3915150.html
Copyright © 2020-2023  润新知