java代码doc转pdf提高效率的方法

使用了jacob.jar来调用activex控件,本机需安装WPS或pdfcreator

001 package experiments;

002

003 import com.jacob.activeX.ActiveXComponent;

004 import com.jacob.com.Dispatch;

005 import com.jacob.com.DispatchEvents;

006 import com.jacob.com.Variant;

007 import java.io.File;

008 import java.util.logging.Level;

009 import java.util.logging.Logger;

010

011 public class Doc2Pdf {

012

013 public static Converter newConverter(String name) {

014 if (name.equals("wps")) {

015 return new Wps();

016 } else if (name.equals("pdfcreator")) {

017 return new PdfCreator();

018 }

019 return null;

020 }

021

022 public synchronized static boolean convert(String word, String pdf) {

023 return newConverter("pdfcreator").convert(word, pdf);

024 }

025

026 public static interface Converter {

027

028 public boolean convert(String word, String pdf);

029 }

030

031 public static class Wps implements Converter {

032

033 public synchronized boolean convert(String word, String pdf) {

034 File pdfFile = new File(pdf);

035 File wordFile = new File(word);

036 ActiveXComponent wps = null;

037 try {

038 wps = new ActiveXComponent("wps.application");

039 ActiveXComponent doc = wps.invokeGetComponent("Documents").invokeGetComponent("Open", newVariant(wordFile.getAbsolutePath()));

040 doc.invoke("ExportPdf", new Variant(pdfFile.getAbsolutePath()));

041 doc.invoke("Close");

042 doc.safeRelease();

043 } catch (Exception ex) {

044 Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex);

045 return false;

046 } catch (Error ex) {

047 Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex);

048 return false;

049 } finally {

050 if (wps != null) {

051 wps.invoke("Terminate");

052 wps.safeRelease();

053 }

054 }

055 return true;

056 }

057 }

058

059 public static class PdfCreator implements Converter {

060

061 public static final int STATUS_IN_PROGRESS = 2;

062 public static final int STATUS_WITH_ERRORS = 1;

063 public static final int STATUS_READY = 0;

064 private ActiveXComponent pdfCreator;

065 private DispatchEvents dispatcher;

066 private volatile int status;

067 private Variant defaultPrinter;

068

069 private void init() {

070 pdfCreator = new ActiveXComponent("PDFCreator.clsPDFCreator");

071 dispatcher = new DispatchEvents(pdfCreator, this);

072 pdfCreator.setProperty("cVisible", new Variant(false));

073 pdfCreator.invoke("cStart", new Variant[]{newVariant("/NoProcessingAtStartup"), new Variant(true)});

074 setCOption("UseAutosave", 1);

075 setCOption("UseAutosaveDirectory", 1);

076 setCOption("AutosaveFormat", 0); // 0 = PDF

077 defaultPrinter = pdfCreator.getProperty("cDefaultPrinter");

078 status = STATUS_IN_PROGRESS;

079 pdfCreator.setProperty("cDefaultprinter", "PDFCreator");

080 pdfCreator.invoke("cClearCache");

081 pdfCreator.setProperty("cPrinterStop", false);

082 }

083

084 private void setCOption(String property, Object value) {

085 Dispatch.invoke(pdfCreator, "cOption", Dispatch.Put, new Object[]{property, value}, new int[2]);

086 }

087

088 private void close() {

089 if (pdfCreator != null) {

090 pdfCreator.setProperty("cDefaultprinter", defaultPrinter);

091 pdfCreator.invoke("cClearCache");

092 pdfCreator.setProperty("cPrinterStop", true);

093 pdfCreator.invoke("cClose");

094 pdfCreator.safeRelease();

095 pdfCreator = null;

096 }

097 if (dispatcher != null) {

098 dispatcher.safeRelease();

099 dispatcher = null;

100 }

101 }

102

103 public synchronized boolean convert(String word, String pdf) {

104 File pdfFile = new File(pdf);

105 File wordFile = new File(word);

106 try {

107 init();

108 setCOption("AutosaveDirectory", pdfFile.getParentFile().getAbsolutePath());

109 if (pdfFile.exists()) {

110 pdfFile.delete();

111 }

112 pdfCreator.invoke("cPrintfile", wordFile.getAbsolutePath());

113 int seconds = 0;

114 while (isInProcess()) {

115 seconds++;

116 if (seconds > 30) { // timeout

117 throw new Exception("convertion timeout!");

118 }

119 Thread.sleep(1000);

120 }

121 if (isWithErrors()) return false;

122 // 由于转换前设置cOption的AutosaveFilename不能保证输出的文件名与设置的相同(pdfcreator会加入/修改后缀名)

123 // 所以这里让pdfcreator使用自动生成的文件名进行输出,然后在保存后将其重命名为目标文件名

124 File outputFile = newFile(pdfCreator.getPropertyAsString("cOutputFilename"));

125 if (outputFile.exists()) {

126 outputFile.renameTo(pdfFile);

127 }

128 } catch (InterruptedException ex) {

129 Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex);

130 return false;

131 } catch (Exception ex) {

132 Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex);

133 return false;

134 } catch (Error ex) {

135 Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex);

136 return false;

137 } finally {

138 close();

139 }

140 return true;

141 }

142

143 private boolean isInProcess() {

144 return status == STATUS_IN_PROGRESS;

145 }

146

147 private boolean isWithErrors() {

148 return status == STATUS_WITH_ERRORS;

149 }

150

151 // eReady event

152 public void eReady(Variant[] args) {

153 status = STATUS_READY;

154 }

155

156 // eError event

157 public void eError(Variant[] args) {

158 status = STATUS_WITH_ERRORS;

159 }

160 }

161

162 public static void main(String[] args) {

163 convert("e:\\test.doc", "e:\\output.pdf");

164 }

165 }