java调用WPS或pdfcreator的com接口实现doc转pdf
来源:互联网 发布:淘宝网冬装女装套装裙 编辑:程序博客网 时间:2024/05/07 15:11
使用了jacob.jar来调用activex控件,本机需安装WPS或pdfcreator。
还需要jacob.jar以及jacob.dll 请看附件
jacob.dll 需要放置在系统system32下,如果系统是c盘:C://windows/system32/下面
还需要jacob.jar以及jacob.dll 请看附件
jacob.dll 需要放置在系统system32下,如果系统是c盘:C://windows/system32/下面
- import com.jacob.activeX.ActiveXComponent;
- import com.jacob.com.Dispatch;
- import com.jacob.com.DispatchEvents;
- import com.jacob.com.Variant;
- import java.io.File;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class DocChangePdfForJco {
- public static Converter newConverter(String name) {
- if (name.equals("wps")) {
- return new Wps();
- } else if (name.equals("pdfcreator")) {
- return new PdfCreator();
- }
- return null;
- }
- public synchronized static boolean convert(String word, String pdf) {
- return newConverter("pdfcreator").convert(word, pdf);
- }
- public abstract static interface Converter {
- public boolean convert(String word, String pdf);
- }
- public static class Wps implements Converter {
- public synchronized boolean convert(String word, String pdf) {
- File pdfFile = new File(pdf);
- File wordFile = new File(word);
- ActiveXComponent wps = null;
- try {
- wps = new ActiveXComponent("wps.application");
- ActiveXComponent doc = wps.invokeGetComponent("Documents").invokeGetComponent("Open", new Variant(wordFile.getAbsolutePath()));
- doc.invoke("ExportPdf", new Variant(pdfFile.getAbsolutePath()));
- doc.invoke("Close");
- doc.safeRelease();
- } catch (Exception ex) {
- Logger.getLogger(DocChangePdfForJco.class.getName()).log(Level.SEVERE, null, ex);
- return false;
- } catch (Error ex) {
- Logger.getLogger(DocChangePdfForJco.class.getName()).log(Level.SEVERE, null, ex);
- return false;
- } finally {
- if (wps != null) {
- wps.invoke("Terminate");
- wps.safeRelease();
- }
- }
- return true;
- }
- }
- public static class PdfCreator implements Converter {
- public static final int STATUS_IN_PROGRESS = 2;
- public static final int STATUS_WITH_ERRORS = 1;
- public static final int STATUS_READY = 0;
- private ActiveXComponent pdfCreator;
- private DispatchEvents dispatcher;
- private volatile int status;
- private Variant defaultPrinter;
- private void init() {
- pdfCreator = new ActiveXComponent("PDFCreator.clsPDFCreator");
- dispatcher = new DispatchEvents(pdfCreator, this);
- pdfCreator.setProperty("cVisible", new Variant(false));
- pdfCreator.invoke("cStart", new Variant[]{new Variant("/NoProcessingAtStartup"), new Variant(true)});
- setCOption("UseAutosave", 1);
- setCOption("UseAutosaveDirectory", 1);
- setCOption("AutosaveFormat", 0);
- defaultPrinter = pdfCreator.getProperty("cDefaultPrinter");
- status = STATUS_IN_PROGRESS;
- pdfCreator.setProperty("cDefaultprinter", "PDFCreator");
- pdfCreator.invoke("cClearCache");
- pdfCreator.setProperty("cPrinterStop", false);
- }
- private void setCOption(String property, Object value) {
- Dispatch.invoke(pdfCreator, "cOption", Dispatch.Put, new Object[]{property, value}, new int[2]);
- }
- private void close() {
- if (pdfCreator != null) {
- pdfCreator.setProperty("cDefaultprinter", defaultPrinter);
- pdfCreator.invoke("cClearCache");
- pdfCreator.setProperty("cPrinterStop", true);
- pdfCreator.invoke("cClose");
- pdfCreator.safeRelease();
- pdfCreator = null;
- }
- if (dispatcher != null) {
- dispatcher.safeRelease();
- dispatcher = null;
- }
- }
- public synchronized boolean convert(String word, String pdf) {
- File pdfFile = new File(pdf);
- File wordFile = new File(word);
- try {
- init();
- setCOption("AutosaveDirectory", pdfFile.getParentFile().getAbsolutePath());
- setCOption("AutosaveFilename", pdfFile.getName());
- pdfCreator.invoke("cPrintfile", wordFile.getAbsolutePath());
- int seconds = 0;
- while (isInProcess()) {
- Thread.sleep(1000);
- seconds++;
- if (seconds > 20) { // timeout
- break;
- }
- }
- if (seconds > 20 || isWithError()) return false;
- } catch (InterruptedException ex) {
- Logger.getLogger(DocChangePdfForJco.class.getName()).log(Level.SEVERE, null, ex);
- return false;
- } catch (Exception ex) {
- Logger.getLogger(DocChangePdfForJco.class.getName()).log(Level.SEVERE, null, ex);
- return false;
- } catch (Error ex) {
- Logger.getLogger(DocChangePdfForJco.class.getName()).log(Level.SEVERE, null, ex);
- return false;
- } finally {
- close();
- }
- return true;
- }
- private boolean isInProcess() {
- return status == STATUS_IN_PROGRESS;
- }
- private boolean isWithError() {
- return status == STATUS_WITH_ERRORS;
- }
- // eReady event
- public void eReady(Variant[] args) {
- status = STATUS_READY;
- }
- // eError event
- public void eError(Variant[] args) {
- status = STATUS_WITH_ERRORS;
- }
- }
- public static void main(String[] args) {
- convert("d:\\Itext\\tt.xls","d:\\Itext\\xssss.pdf");
- }
- }
0 0
- java调用WPS或pdfcreator的com接口实现doc转pdf
- java调用WPS或pdfcreator的com接口实现doc转pdf
- java调用WPS或pdfcreator的com接口实现doc转pdf
- PDFCreator:一款免费,开源的PDF(Tiff,pcx,png,jpeg,bmp,PS,EPS)打印机(VB,GPL),并提供了COM接口,方便使用各种编程语言调用
- 调用WPS服务COM组件转换PDF
- C#中调用PDFCreator生成PDF文件
- window下wps转pdf或html
- 求助:php 用windows COM组件调用openoffice接口实现word转pdf文件报错
- PDFCreator——ps(postscript)转pdf
- Java利用jacob实现wps转换pdf
- java实现txt、excle、pdf、doc文档的在线预览
- 转:JAVA实现word doc docx pdf excel的在线浏览
- Pdf转doc的感受
- php在linux系统下实现百度文库功能 doc或pdf转swf预览功能
- php在linux系统下实现百度文库功能 doc或pdf转swf预览功能
- Android中调用第三方打开本地doc、pdf、ppt、text等文件功能的实现
- Java读取PDF,Doc的方法
- Java读取PDF,Doc的方法
- POJ 1157 动态规划
- Knight Moves--广度优先搜索
- 2014/7/17 对象与类
- Syntax error, insert "}" to complete MethodBody
- 公交线路免费api接口代码
- java调用WPS或pdfcreator的com接口实现doc转pdf
- 关于App Review的加速审核
- 描述:将输入的数字的人民币金额大写
- 页面跳转动画 For_Cocos2d-iphone
- 简单播放器的实现过程
- 搭建Linux下邮件服务器extmail+postfix+clamav所有软件
- [难]hihocoder 线段树 毁灭者问题
- 全球最富有的科技大佬怎么过日子
- Percolator,谷歌如何实现大数据的事务