问题描述
- 求大神 帮我看看问题 Java中 调用虚拟打印机 把word转存为PDF
-
public class SaveWordAspdf { public static void saveWordAspdf(String filePath, String outFile) throws Exception { ComThread.InitSTA(); ActiveXComponent actcom = new ActiveXComponent("Word.Application"); Dispatch documents = null; Dispatch document = null; try { actcom.setProperty("Visible", new Variant(false)); documents = actcom.getProperty("Documents").toDispatch(); document = Dispatch.invoke(documents,"Open", Dispatch.Method, new Object[] { filePath, new Variant(false), new Variant(false) }, new int[1]).toDispatch(); Dispatch.invoke(document, "SaveAs", Dispatch.Method, new Object[] { outFile,new Variant(false)},new int[1]); Dispatch.call(document, "Close", new Object[] { new Variant(false) }); if (actcom != null) { actcom.invoke("Quit", new Variant[] {}); actcom = null; } ComThread.Release(); } catch (Exception e) { e.printStackTrace(); } finally { if (actcom != null) { actcom.invoke("Quit", new Variant[] {}); actcom = null; } ComThread.Release(); } } public static void print(String path)throws Exception { String PRINT_NAME="pdfFactory Pro";//打印机名称 //初始化COM线程 ComThread.InitSTA(); ActiveXComponent doc=new ActiveXComponent("Word.Application"); try { //设置是否显示打开word Dispatch.put(doc, "Visible", new Variant(true)); //打开具体的工作簿 Dispatch documents = doc.getProperty("Documents").toDispatch(); Dispatch word = Dispatch.call(documents,"Open",path).toDispatch(); //设置打印属性并打印 Dispatch.callN(word,"PrintOut",new Object[]{Variant.VT_MISSING, Variant.VT_MISSING, new Integer(1), new Boolean(false),PRINT_NAME, new Boolean(true),Variant.VT_MISSING, ""}); //关闭文档 Dispatch.call(word, "Close", new Variant(false)); } catch (Exception e) { e.printStackTrace(); throw new Exception(); } finally{ doc.invoke("Quit",new Variant[0]); //始终释放资源 ComThread.Release(); } } public static void main(String[] args) { try { System.out.println(0); saveWordAspdf("d:/1234.doc","d:/1234.pdf"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
时间: 2024-08-13 01:18:04