使用java jacob转换office到pdf

      使用java jacob转换office到pdf,注意事项:

      1.本机需要安装office,或者openoffice(未测试过)

       2.下载最新的jacob的jar,解压后将相关的dll文件拷贝到java/jre/bin目录下.

       3.如果包如下错误:

            no jacob-1.18-x64 in java.library.path  原因可能是找不到相关dll文件,将配套dll文件拷贝到相关使用的jdk/jre/bin目录

 

转换代码(代码来源于互联网,由于时间原因,原创暂时无法考证):

 

Java代码   收藏代码
  1. import com.jacob.activeX.ActiveXComponent;  
  2. import com.jacob.com.Dispatch;  
  3. /** 
  4.  *  
  5. * @ClassName: OfficeToPDF 
  6. * @Description: 使用jacob转换office为pdf,也可以将txt文件转换为pdf. 
  7. * @author *************** 
  8. * @date 2015年9月21日 上午11:15:44 
  9. * 
  10.  */  
  11. public class OfficeToPDF {  
  12.        
  13.      private static final int wdFormatPDF = 17;  
  14.      private static final int xlTypePDF = 0;  
  15.      private static final int ppSaveAsPDF = 32;  
  16.      private static final int msoTrue = -1;  
  17.      private static final int msofalse = 0;  
  18.        
  19.     public static void main(String[] args) {  
  20.         convert2PDF("D:\\aaaa.ppt","D:\\bbbbb.pdf");  
  21.           
  22.     }  
  23.       
  24.     public static boolean convert2PDF(String inputFile, String pdfFile) {  
  25.         String suffix =  getFileSufix(inputFile);  
  26.         File file = new File(inputFile);  
  27.         if(!file.exists()){  
  28.             System.out.println("文件不存在!");  
  29.             return false;  
  30.         }  
  31.         if(suffix.equals("pdf")){  
  32.             System.out.println("PDF not need to convert!");  
  33.             return false;  
  34.         }  
  35.         if(suffix.equals("doc")||suffix.equals("docx")||suffix.equals("txt")){  
  36.             return word2PDF(inputFile,pdfFile);  
  37.         }else if(suffix.equals("ppt")||suffix.equals("pptx")){  
  38.             return ppt2PDF(inputFile,pdfFile);  
  39.         }else if(suffix.equals("xls")||suffix.equals("xlsx")){  
  40.             return excel2PDF(inputFile,pdfFile);  
  41.         }else{  
  42.             System.out.println("文件格式不支持转换!");  
  43.             return false;  
  44.         }  
  45.     }  
  46.       
  47.     public static String getFileSufix(String fileName){  
  48.         int splitIndex = fileName.lastIndexOf(".");  
  49.         return fileName.substring(splitIndex + 1);  
  50.     }  
  51.       
  52.     /** 
  53.      *  
  54.     * @Title: word2PDF 
  55.     * @Description: 转换word文档为pdf 
  56.     * @param @param inputFile 
  57.     * @param @param pdfFile 
  58.     * @param @return    设定文件 
  59.     * @return boolean    返回类型 
  60.     * @throws 
  61.      */  
  62.     public static boolean word2PDF(String inputFile,String pdfFile){  
  63.         try{  
  64.         //打开word应用程序  
  65.         ActiveXComponent app = new ActiveXComponent("Word.Application");  
  66.         //设置word不可见  
  67.         app.setProperty("Visible"false);  
  68.         //获得word中所有打开的文档,返回Documents对象  
  69.         Dispatch docs = app.getProperty("Documents").toDispatch();  
  70.         //调用Documents对象中Open方法打开文档,并返回打开的文档对象Document  
  71.         Dispatch doc = Dispatch.call(docs,  
  72.                                     "Open",  
  73.                                     inputFile,  
  74.                                     false,  
  75.                                     true  
  76.                                     ).toDispatch();  
  77.         //调用Document对象的SaveAs方法,将文档保存为pdf格式  
  78.         /* 
  79.         Dispatch.call(doc, 
  80.                     "SaveAs", 
  81.                     pdfFile, 
  82.                     wdFormatPDF     //word保存为pdf格式宏,值为17 
  83.                     ); 
  84.                     */  
  85.         Dispatch.call(doc,  
  86.                 "ExportAsFixedFormat",  
  87.                 pdfFile,  
  88.                 wdFormatPDF     //word保存为pdf格式宏,值为17  
  89.                 );  
  90.         //关闭文档  
  91.         Dispatch.call(doc, "Close",false);  
  92.         //关闭word应用程序  
  93.         app.invoke("Quit"0);  
  94.         return true;  
  95.     }catch(Exception e){  
  96.         return false;  
  97.     }  
  98.     }  
  99.       
  100.     /** 
  101.      *  
  102.     * @Title: excel2PDF 
  103.     * @Description: 转换excel为PDF 
  104.     * @param @param inputFile 
  105.     * @param @param pdfFile 
  106.     * @param @return    设定文件 
  107.     * @return boolean    返回类型 
  108.     * @throws 
  109.      */  
  110.     public static boolean excel2PDF(String inputFile,String pdfFile){  
  111.         try{  
  112.             ActiveXComponent app = new ActiveXComponent("Excel.Application");  
  113.         app.setProperty("Visible"false);  
  114.         Dispatch excels = app.getProperty("Workbooks").toDispatch();  
  115.         Dispatch excel = Dispatch.call(excels,  
  116.                                     "Open",  
  117.                                     inputFile,  
  118.                                     false,  
  119.                                     true  
  120.                                     ).toDispatch();  
  121.         Dispatch.call(excel,  
  122.                     "ExportAsFixedFormat",  
  123.                     xlTypePDF,        
  124.                     pdfFile  
  125.                     );  
  126.         Dispatch.call(excel, "Close",false);  
  127.         app.invoke("Quit");  
  128.         return true;  
  129.     }catch(Exception e){  
  130.         return false;  
  131.     }  
  132.            
  133.     }  
  134.       
  135.     /** 
  136.      *  
  137.     * @Title: ppt2PDF 
  138.     * @Description: 转换ppt为office 
  139.     * @param @param inputFile 
  140.     * @param @param pdfFile 
  141.     * @param @return    设定文件 
  142.     * @return boolean    返回类型 
  143.     * @throws 
  144.      */  
  145.     public static boolean ppt2PDF(String inputFile,String pdfFile){  
  146.         try{  
  147.         ActiveXComponent app = new ActiveXComponent("PowerPoint.Application");  
  148.         //app.setProperty("Visible", msofalse);  
  149.         Dispatch ppts = app.getProperty("Presentations").toDispatch();  
  150.            
  151.         Dispatch ppt = Dispatch.call(ppts,  
  152.                                     "Open",  
  153.                                     inputFile,  
  154.                                     true,//ReadOnly  
  155.                                     true,//Untitled指定文件是否有标题  
  156.                                     false//WithWindow指定文件是否可见  
  157.                                     ).toDispatch();  
  158.            
  159.         Dispatch.call(ppt,  
  160.                     "SaveAs",  
  161.                     pdfFile,  
  162.                     ppSaveAsPDF   
  163.                     );  
  164.                    
  165.         Dispatch.call(ppt, "Close");  
  166.            
  167.         app.invoke("Quit");  
  168.         return true;  
  169.         }catch(Exception e){  
  170.             return false;  
  171.         }  
  172.      }  
  173. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值