日期:2014-05-17 浏览次数:20729 次
package com.sinobpo.hsda.util;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.DispatchEvents;
import com.jacob.com.Variant;
import java.io.File;
public class NewOfficeDocumentConverter {
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("wps").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(DocChangePdfFo