当前位置:我的异常网» Java Web开发 » 急java调用WPS或pdfcreator的com接口实现doc转pdf解

急java调用WPS或pdfcreator的com接口实现doc转pdf解决方法(2)

www.myexceptions.net  网友分享于:2014-01-22  浏览:12次

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()) {

文章评论

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐