public static void wordToPdf(String wordPath,String pdfPath){
File wordFile = new File(wordPath);
ActiveXComponent axc = new ActiveXComponent("Word.Application");
//设置word可见性
axc.setProperty("Visible", new Variant(false));
try{
axc.setProperty("Visible", new Variant(false));
Dispatch docs = axc.getProperty("Documents").toDispatch();
// 打开word文档
Dispatch doc = Dispatch.invoke(
docs,
"Open",
Dispatch.Method,
new Object[] { wordPath, new Variant(false),
new Variant(true) }, new int[1])
.toDispatch();
// 另存为指定格式的文档 17 代表 PDF
Dispatch.invoke(doc, "SaveAs", Dispatch.Method,
new Object[] { pdfPath,
new Variant(17) },
new int[1]);
Variant file = new Variant(false);
// 关闭文档
Dispatch.call(doc, "Close", file);
}catch (Exception e) {
e.printStackTrace();
System.out.println("文件转换失败!");
}finally{
axc.invoke("Quit",new Variant[]{});
}
}
文档转换成PDF格式
最新推荐文章于 2020-01-08 22:36:32 发布