java使用jacob 将word转换为pdf

查询了很多网站 知道使用jacob 可以将word 转换成pdf;

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

1.先下载office2007 能另存为pdf的插件 SaveAsPDFandXPS.exe 安装

2.附件提供了jacob1.9的下载

3.这点很重要也是耗时我最久的就是jacob.dll与jacob.jar的存放位置;位置不对则会出现app.getProperty("Documents").toDispatch()这句话无法正常执行;

位置如下文件存放在jdk的 jre下 例如我这边是
jacob.jar 放在 E:\jdk1.5.0_14\jre\lib\ext
jacob.dll 放在 E:\jdk1.5.0_14\jre\bin
下能正常执行;
按网上说的 放在windows\system32下则会出现错误

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值