67,515
社区成员
发帖
与我相关
我的任务
分享
private void wordToPdf(String formYear,String code){
//String filename = "d:/2013上半年个人总结(黄秋稳).doc";
String templatePath = Thread.currentThread().getContextClassLoader().getResource("").toString().replace("classes/", "formFile/file/");
String filename = templatePath + formYear + "-" + code + ".doc.doc";
String toFilename = filename.replace("doc.doc", "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,false, true).toDispatch();
System.out.println("转换文档到PDF..." + toFilename);
File tofile = new File(toFilename);
if (tofile.exists()) {
tofile.delete();
}
// Dispatch.call(doc,"SaveAs", toFilename,17);
//Dispatch.invoke(doc,"SaveAs",17, new Object[]{toFilename,new Integer(1)},new int[1]);
Dispatch.invoke(doc,
"SaveAs",
Dispatch.Method,
new Object[] {toFilename,new Integer(17)},
new int[1]);
Dispatch.call(doc, "Close", false);
long end = System.currentTimeMillis();
System.out.println("转换完成用时:" + (end - start) + "ms.");
} catch (Exception e) {
System.out.println("========Error:文档转换失败:" + e.getMessage());
} finally {
if (app != null){
app.invoke("Quit", 0);
}
} }
[quote=引用 17 楼 tongbo0329 的回复:] jacob,doc转PDF,多线程处理的效果是串行的,并非并行,提高不了效率,而OpenOffice转换虽然速度快,可有些内容转不出来,一般处理几个没问题,如果有几十上百个则save as与Close都可能有问题,试过多进程,可没法判断是否转换成功,因为有的即使没报错,也不一定成功了,想请教各位神们,有没有好的推荐与方法处理,不胜感激...
jacob,doc转PDF,多线程处理的效果是串行的,并非并行,提高不了效率,而OpenOffice转换虽然速度快,可有些内容转不出来,一般处理几个没问题,如果有几十上百个则save as与Close都可能有问题,试过多进程,可没法判断是否转换成功,因为有的即使没报错,也不一定成功了,想请教各位神们,有没有好的推荐与方法处理,不胜感激...
jacob,doc转PDF,多线程处理的效果是串行的,并非并行,提高不了效率,而OpenOffice转换虽然速度快,可有些内容转不出来,一般处理几个没问题,如果有几十上百个则save as与Close都可能有问题,试过多进程,可没法判断是否转换成功,因为有的即使没报错,也不一定成功了,想请教各位神们,有没有好的推荐与方法处理,不胜感激...
话说save as 转出来的pdf word里的图片感觉发虚,不知道可不可以通过调节参数什么的解决这个问题
我建议楼主还是用openoffice转换吧,安全的多。而且不像调用com转换这样只能一个一个来,不能并发,速度慢。
package com.huang;
import java.io.File;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
public class TestWord2Pdf {
/**
* @param args
*/
static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。
static final int wdFormatPDF = 17;// PDF 格式
public static void main(String[] args) {
// TODO Auto-generated method stub
String filename = "D:/test.docx";
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.callN(doc,//
"SaveAs", //
toFilename, // FileName
17);
Dispatch.call(doc, "Close", false);
long end = System.currentTimeMillis();
System.out.println("转换完成..用时:" + (end - start) + "ms.");
} catch (Exception e) {
System.out.println("========Error:文档转换失败:" + e.getMessage());
} finally {
if (app != null)
app.invoke("Quit", 0);
}
}
}
没有任何问题,不论执行多少遍。但是如果是走服务器就会出现上面的问题。
启动Word...
打开文档...file:/D:/Projects/pangjc/java/workspace/gwyc/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/gwyc/WEB-INF/formFile/file/2013-00104.doc.doc
转换文档到PDF...file:/D:/Projects/pangjc/java/workspace/gwyc/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/gwyc/WEB-INF/formFile/file/2013-00104.pdf
========Error:文档转换失败:Invoke of: SaveAs
Source: Microsoft Word
Description: 命令失败
从头到尾,没看到你啥问题。报的异常呢?谁愿意一步步的问你 还有没有遇到过这样的问题的同胞啊?给说一下呗!急切等到支援啊
启动Word...
打开文档...file:/D:/Projects/pangjc/java/workspace/gwyc/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/gwyc/WEB-INF/formFile/file/2013-00104.doc.doc
转换文档到PDF...file:/D:/Projects/pangjc/java/workspace/gwyc/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/gwyc/WEB-INF/formFile/file/2013-00104.pdf
========Error:文档转换失败:Invoke of: SaveAs
Source: Microsoft Word
Description: 命令失败
还有没有遇到过这样的问题的同胞啊?给说一下呗!急切等到支援啊