java使用jacob word 转pdf

首先我自己的环境是windows xp ,office2007 ,jdk1.5;

1.先下载office2007 能另存为pdf的插件 SaveAsPDFandXPS.exe 安装。(纠结了好久才知道要安装这个插件,否则无法调用SaveAs方法)

2.下载jacob,位置如下:

jacob.jar 放在 E:\jdk1.5.0_14\lib
jacob.dll 放在 E:\jdk1.5.0_14\bin
源代码如下:

  1. package test.jacob;  
  2.   
  3. import java.io.File;  
  4.   
  5. import com.jacob.activeX.ActiveXComponent;  
  6. import com.jacob.com.ComThread;  
  7. import com.jacob.com.Dispatch;  
  8. import com.jacob.com.Variant;  
  9.   
  10. public class Test {  
  11.     static final int wdFormatPDF = 17;// PDF 格式     
  12.     public void wordToPDF(String sfileName,String toFileName){    
  13.             
  14.         System.out.println("启动Word...");      
  15.         long start = System.currentTimeMillis();      
  16.         ActiveXComponent app = null;  
  17.         Dispatch doc = null;  
  18.         try {      
  19.             app = new ActiveXComponent("Word.Application");      
  20.             app.setProperty("Visible"new Variant(false));  
  21.             Dispatch docs = app.getProperty("Documents").toDispatch();   
  22. //          doc = Dispatch.call(docs,  "Open" , sourceFile).toDispatch();    
  23.             doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] {                    
  24.                sfileName, new Variant(false),new Variant(true) }, new int[1]).toDispatch();               
  25.             System.out.println("打开文档..." + sfileName);  
  26.             System.out.println("转换文档到PDF..." + toFileName);      
  27.             File tofile = new File(toFileName);      
  28.             if (tofile.exists()) {      
  29.                 tofile.delete();      
  30.             }        
  31. //          Dispatch.call(doc, "SaveAs",  destFile,  17);                     
  32.             Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {                
  33.                 toFileName, new Variant(17) }, new int[1]);    
  34.             long end = System.currentTimeMillis();      
  35.             System.out.println("转换完成..用时:" + (end - start) + "ms.");                
  36.         } catch (Exception e) {  
  37.             e.printStackTrace();  
  38.             System.out.println("========Error:文档转换失败:" + e.getMessage());      
  39.         } finally {  
  40.             Dispatch.call(doc,"Close",false);  
  41.             System.out.println("关闭文档");  
  42.             if (app != null)      
  43.                 app.invoke("Quit"new Variant[] {});      
  44.             }  
  45.           //如果没有这句话,winword.exe进程将不会关闭   
  46.            ComThread.Release();     
  47.     }  
  48.     public static void main(String[] args) {  
  49.         Test d = new Test();  
  50.         d.wordToPDF("d:\\ftp\\aaa.docx""d:\\ftp\\aaa.pdf");  
  51.     }  
  52.   
  53. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值