java调用WPS或pdfcreator的com接口实现doc转pdf

使用了jacob.jar来调用activex控件,本机需安装WPS或pdfcreator。
还需要jacob.jar以及jacob.dll 请看附件
jacob.dll 需要放置在系统system32下,如果系统是c盘:C://windows/system32/下面



Java代码   收藏代码
  1. import com.jacob.activeX.ActiveXComponent;  
  2. import com.jacob.com.Dispatch;  
  3. import com.jacob.com.DispatchEvents;  
  4. import com.jacob.com.Variant;  
  5. import java.io.File;  
  6. import java.util.logging.Level;  
  7. import java.util.logging.Logger;  
  8.   
  9. public class DocChangePdfForJco {  
  10.   
  11.     public static Converter newConverter(String name) {  
  12.         if (name.equals("wps")) {  
  13.             return new Wps();  
  14.         } else if (name.equals("pdfcreator")) {  
  15.             return new PdfCreator();  
  16.         }  
  17.         return null;  
  18.     }  
  19.   
  20.     public synchronized static boolean convert(String word, String pdf) {  
  21.         return newConverter("pdfcreator").convert(word, pdf);  
  22.     }  
  23.   
  24.     public abstract static interface Converter {  
  25.   
  26.         public boolean convert(String word, String pdf);  
  27.     }  
  28.   
  29.     public static class Wps implements Converter {  
  30.   
  31.         public synchronized boolean convert(String word, String pdf) {  
  32.             File pdfFile = new File(pdf);  
  33.             File wordFile = new File(word);  
  34.             ActiveXComponent wps = null;  
  35.             try {  
  36.                 wps = new ActiveXComponent("wps.application");  
  37.                 ActiveXComponent doc = wps.invokeGetComponent("Documents").invokeGetComponent("Open"new Variant(wordFile.getAbsolutePath()));  
  38.                 doc.invoke("ExportPdf"new Variant(pdfFile.getAbsolutePath()));  
  39.                 doc.invoke("Close");  
  40.                 doc.safeRelease();  
  41.             } catch (Exception ex) {  
  42.                 Logger.getLogger(DocChangePdfForJco.class.getName()).log(Level.SEVERE, null, ex);  
  43.                 return false;  
  44.             } catch (Error ex) {  
  45.                 Logger.getLogger(DocChangePdfForJco.class.getName()).log(Level.SEVERE, null, ex);  
  46.                 return false;  
  47.             } finally {  
  48.                 if (wps != null) {  
  49.                     wps.invoke("Terminate");  
  50.                     wps.safeRelease();  
  51.                 }  
  52.             }  
  53.             return true;  
  54.         }  
  55.     }  
  56.   
  57.     public static class PdfCreator implements Converter {  
  58.   
  59.         public static final int STATUS_IN_PROGRESS = 2;  
  60.         public static final int STATUS_WITH_ERRORS = 1;  
  61.         public static final int STATUS_READY = 0;  
  62.         private ActiveXComponent pdfCreator;  
  63.         private DispatchEvents dispatcher;  
  64.         private volatile int status;  
  65.         private Variant defaultPrinter;  
  66.   
  67.         private void init() {  
  68.             pdfCreator = new ActiveXComponent("PDFCreator.clsPDFCreator");  
  69.             dispatcher = new DispatchEvents(pdfCreator, this);  
  70.             pdfCreator.setProperty("cVisible"new Variant(false));  
  71.             pdfCreator.invoke("cStart"new Variant[]{new Variant("/NoProcessingAtStartup"), new Variant(true)});  
  72.             setCOption("UseAutosave"1);  
  73.             setCOption("UseAutosaveDirectory"1);  
  74.             setCOption("AutosaveFormat"0);  
  75.             defaultPrinter = pdfCreator.getProperty("cDefaultPrinter");  
  76.             status = STATUS_IN_PROGRESS;  
  77.             pdfCreator.setProperty("cDefaultprinter""PDFCreator");  
  78.             pdfCreator.invoke("cClearCache");  
  79.             pdfCreator.setProperty("cPrinterStop"false);  
  80.         }  
  81.   
  82.         private void setCOption(String property, Object value) {  
  83.             Dispatch.invoke(pdfCreator, "cOption", Dispatch.Put, new Object[]{property, value}, new int[2]);  
  84.         }  
  85.   
  86.         private void close() {  
  87.             if (pdfCreator != null) {  
  88.                 pdfCreator.setProperty("cDefaultprinter", defaultPrinter);  
  89.                 pdfCreator.invoke("cClearCache");  
  90.                 pdfCreator.setProperty("cPrinterStop"true);  
  91.                 pdfCreator.invoke("cClose");  
  92.                 pdfCreator.safeRelease();  
  93.                 pdfCreator = null;  
  94.             }  
  95.             if (dispatcher != null) {  
  96.                 dispatcher.safeRelease();  
  97.                 dispatcher = null;  
  98.             }  
  99.         }  
  100.   
  101.         public synchronized boolean convert(String word, String pdf) {  
  102.             File pdfFile = new File(pdf);  
  103.             File wordFile = new File(word);  
  104.             try {  
  105.                 init();  
  106.                 setCOption("AutosaveDirectory", pdfFile.getParentFile().getAbsolutePath());  
  107.                 setCOption("AutosaveFilename", pdfFile.getName());  
  108.                 pdfCreator.invoke("cPrintfile", wordFile.getAbsolutePath());  
  109.                 int seconds = 0;  
  110.                 while (isInProcess()) {  
  111.                     Thread.sleep(1000);  
  112.                     seconds++;  
  113.                     if (seconds > 20) { // timeout  
  114.                         break;  
  115.                     }  
  116.                 }  
  117.                 if (seconds > 20 || isWithError()) return false;  
  118.             } catch (InterruptedException ex) {  
  119.                 Logger.getLogger(DocChangePdfForJco.class.getName()).log(Level.SEVERE, null, ex);  
  120.                 return false;  
  121.             } catch (Exception ex) {  
  122.                 Logger.getLogger(DocChangePdfForJco.class.getName()).log(Level.SEVERE, null, ex);  
  123.                 return false;  
  124.             } catch (Error ex) {  
  125.                 Logger.getLogger(DocChangePdfForJco.class.getName()).log(Level.SEVERE, null, ex);  
  126.                 return false;  
  127.             } finally {  
  128.                 close();  
  129.             }  
  130.             return true;  
  131.         }  
  132.   
  133.         private boolean isInProcess() {  
  134.             return status == STATUS_IN_PROGRESS;  
  135.         }  
  136.   
  137.         private boolean isWithError() {  
  138.             return status == STATUS_WITH_ERRORS;  
  139.         }  
  140.   
  141.         // eReady event  
  142.         public void eReady(Variant[] args) {  
  143.             status = STATUS_READY;  
  144.         }  
  145.   
  146.         // eError event  
  147.         public void eError(Variant[] args) {  
  148.             status = STATUS_WITH_ERRORS;  
  149.         }  
  150.     }  
  151.   
  152.     public static void main(String[] args) {  
  153.         convert("d:\\Itext\\tt.xls","d:\\Itext\\xssss.pdf");  
  154.     }  

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值