利用jacob将Word转成PDF时候报错:Invoke of: SaveAs

ghb5371548 2011-12-07 02:05:32
这是我的源代码:

String filename = "d:\\补充保密协议.doc";
String toFilename = filename + ".pdf";
System.out.println("启动Word...");
long start = System.currentTimeMillis();
ActiveXComponent app = null;
try {
app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", false);

Dispatch docs = app.getProperty("Documents").toDispatch();
System.out.println("打开文档..." + filename);
Dispatch doc = Dispatch.call(docs,//
"Open", //
filename,// FileName
false,// ConfirmConversions
true // ReadOnly
).toDispatch();

System.out.println("转换文档到PDF..." + toFilename);
File tofile = new File(toFilename);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(doc,//
"SaveAs", //
toFilename, // FileName
wdFormatPDF);

Dispatch.call(doc, "Close", false);
long end = System.currentTimeMillis();
System.out.println("转换完成..用时:" + (end - start) + "ms.");
} catch (Exception e) {
System.out.println("========Error:文档转换失败:" + e.getMessage());
e.printStackTrace();
} finally {
if (app != null)
app.invoke("Quit", wdDoNotSaveChanges);
}
...全文
1343 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
inno 2014-05-04
  • 打赏
  • 举报
回复
楼主有解决这个问题了么?我也遇到同样的问题~请教一下~thanks~
来到我身边 2013-12-31
  • 打赏
  • 举报
回复
引用 2 楼 Eden_Ming 的回复:
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]);
new Variant(17)}, Variant是不是可以被任何的数据类型代替啊? 我这样写: Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {toFilename,new Integer(17)}, new int[1]); 第一次调用的话没有问题,但是session存在时再次调用的就会有问题
Eden_Ming 2012-03-06
  • 打赏
  • 举报
回复
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]);
ghb5371548 2011-12-07
  • 打赏
  • 举报
回复
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,因为不支持此文件类型或者文件已损坏(例如,文件作为电子邮件附件发送但没有正确地解码) ”
jacob-1.18 包含jacob-1.18-x64.dll jacob-1.18-x86.dll 亲测可用: 下面为转pdf使用方法 package com.pdf.doctopdf.pdf; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; import java.io.File; public class TestJacob { public static void main(String args[]) { ActiveXComponent app = null; String wordFile = "C:\\Users\\admin\\Desktop\\jar\\年会系统优化_20210820_V0.2.docx"; String pdfFile = "C:\\Users\\admin\\Desktop\\jar\\测试pdf.pdf"; System.out.println("开始转换..."); // 开始时间 long start = System.currentTimeMillis(); try { // 打开word app = new ActiveXComponent("Word.Application"); // 获得word中所有打开的文档 Dispatch documents = app.getProperty("Documents").toDispatch(); System.out.println("打开文件: " + wordFile); // 打开文档 Dispatch document = Dispatch.call(documents, "Open", wordFile, false, true).toDispatch(); // 如果文件存在的话,不会覆盖,会直接报错,所以我们需要判断文件是否存在 File target = new File(pdfFile); if (target.exists()) { target.delete(); } System.out.println("另存为: " + pdfFile); // 另存为,将文档报错pdf,其中word保存为pdf的格式宏的值是17 Dispatch.call(document, "SaveAs", pdfFile, 17); // 关闭文档 Dispatch.call(document, "Close", false); // 结束时间 long end = System.currentTimeMillis(); System.out.println("转换成功,用时:" + (end - start) + "ms"); }catch(Exception e) { e.getMessage(); System.out.println("转换失败"+e.getMessage()); }finally { // 关闭office app.invoke("Quit", 0); } } }

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧