jacob实现将word转为其他文件格式,但是不知道为什么转pdf的时候就报错,求高手指点!!!!!
启动Word
打开文档D:\aa.doc
转换文档到PDF D:\aa.pdf
========Error:文档转换失败:Invoke of: SaveAs
Source: Microsoft Word
Description:
转txt,html,dot,xml都可以,唯独转pdf时候出现这个异常,求高手指点!!!!!
jacob.jar和jacob.dll都已弄好,可编译运行的代码:
import java.io.File;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class Word2Pdf {
static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。
static final int wdFormatPDF = 17;// PDF 格式
public static void main(String[] args) {
String filename = "D:\\aa.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 Variant(false)
,new Variant(true)}, 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 Variant(wdFormatPDF)}, new int[1]); //设置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);
}
}
}