当前位置:我的异常网» 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

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐