String filename = "D:\\补充保密协议.doc";
String toFilename = "D:\\aa.pdf";
System.out.println("启动Word");
ActiveXComponent app = new ActiveXComponent("Word.Application");
try {
app.setProperty("Visible", new Variant(false)); Dispatch docs = app.getProperty("Documents").toDispatch();
System.out.println("打开文档" + filename);
Dispatch doc = Dispatch.invoke(
docs,
"open",
Dispatch.Method,
new Object[] { filename,}, 
new int[1]).toDispatch(); System.out.println("转换文档到PDF" + toFilename);
File tofile = new File(toFilename);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.invoke(doc, 
"SaveAs", 
Dispatch.Method, 
new Object[] {toFilename}, 
new int[17]); // 设置17,即转为pdf

Variant file = new Variant(false);
Dispatch.call(doc, "Close", file);
} catch (Exception e) {
System.out.println("========Error:文档转换失败:" + e.getMessage());
} finally {
if (app != null)
app.invoke("Quit", wdDoNotSaveChanges);
}
我现在代码改了,不报错了,但是生成的PDF文件打不开。
打开文件时候,弹出窗口信息:“Adobe Reader无法打开aa.pdf,因为不支持此文件类型或者文件已损坏(例如,文件作为电子邮件附件发送但没有正确地解码)   ”

解决方案 »

  1.   

    Dispatch.invoke(doc,  
    "SaveAs",  
    Dispatch.Method,  
    new Object[] {toFilename},  
    new int[17]); // 设置17,即转为pdf这一段代码有问题。invoke看api最后一个参数是表示错误参数,而不是转换格式的参数,正确的应该为
    Dispatch.invoke(doc,  
    "SaveAs",  
    Dispatch.Method,  
    new Object[] {toFilename,new Variant(17)},  
    new int[1]); 
      

  2.   

    new Variant(17)},  Variant是不是可以被任何的数据类型代替啊?
    我这样写: Dispatch.invoke(doc,  
              "SaveAs",  
              Dispatch.Method,  
              new Object[] {toFilename,new Integer(17)},  
              new int[1]);  
    第一次调用的话没有问题,但是session存在时再次调用的就会有问题
      

  3.   

    楼主有解决这个问题了么?我也遇到同样的问题~请教一下~thanks~