首页 > 编程语言 >java调用WPS或pdfcreator的com接口实现doc转pdf

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

时间:2022-11-14 23:32:36浏览次数:39  
标签:java String doc WPS return pdfCreator new null public


使用了jacob.jar来调用activex控件,本机需安装WPS或pdfcreator。
还需要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");
}
}




标签:java,String,doc,WPS,return,pdfCreator,new,null,public
From: https://blog.51cto.com/u_688107/5851227

相关文章

  • java——接口作为方法的参数和返回值
    接口作为方法的参数和返回值packagecn.itcast.day11.demo07;importjava.util.ArrayList;importjava.util.List;/*java.util.List正是ArrayList所实现的接口。......
  • java-io基础
    一、流的分类1、按操作的数据单位或传输方式:字符流,字节流。字节是给计算机看的,字符是给人看的2、数据的流向:输入流,输出流3、流的角色:文件流(节点流),处理流从数据来源或操......
  • 【Javaweb】了解link标签
    link标签的属性标签就是定义文档和外部的关系,常见用途是链接样式表。通常指存在于head部分。规定被连接文档的位置<linkrel='stylesheet'href='./ease.css'type='t......
  • 【Java复健指南13】OOP高级04【告一段落】-四大内部类
    四大内部类一个类的内部又完整的嵌套了另一个类结构。classOuter{//外部类classlnner{//内部类}}classOther{//外部其他类}被嵌套的类称为内......
  • java——继承与多态——内部类001
    内部类的概念与分类:           成员内部类的定义:                 成员内部类的使用:      ......
  • java 预科
    #Markdown学习##标题###三级标题 ##字体**Hello,Word!***Hello,Word!****Hello,Word!***~~Hello,Word!~~ ##引用>选择狂神说java,走向人生巅峰 ##......
  • Do a good job: When boss tell you to print a doc file.
    Bosstellmetoprinterprintafile,buthedon'tsaydetaileddetail.Whenshopmanageraskmeprintonbothsides?Ihavetoaskbossafterhehadassigned......
  • JAVA-继承
    packagecom.itheima04;importjavax.swing.*;publicclassUserLoginFrameextendsJFrame{publicUserLoginFrame(){//窗体初始化in......
  • HTML+CSS+JavaScript实现tab切换栏
    HTML+CSS+JavaScript实现tab栏切换制作准备部分HTML部分:切换栏由一个列表组成每个内容写在分别一个div盒子里CSS部分:列表:去除序列点,给每个修改样式;内容:让整个内容......
  • javascript实现封装
    //构造函数functionDog(){ this.leg=4; this.bark=function(){ alert("汪汪"); }}//创建一个黑狗varhuzi=newDog();此处我们并没有完成面向对象的......