使用jacob将word转换成pdf格式
 
1.需要安装word2007或以上版本,若安装07版本学确保该版本已安装2downbank0204MicrosoftSaveasPDF_ XPS,否则安装2downbank0204MicrosoftSaveasPDF_ XPS。office10以上版本自带2downbank0204MicrosoftSaveasPDF_ XPS无需手动安装
2.windows32位环境下需要jacob-1.14.3-x86.dll
 windows64位需要jacob-1.14.3-x64 ,放在tomcat的bin目录下
【文件】使用jacob将word转换成pdf格式-LMLPHP
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant; /**
* 将word文件转换成PDF
* 需要 Microsoft Save as PDF or XPS 插件
* @param args
*/ public static void word2pdf(String docfile,String toFile ){
//String docfile = "D:\\TMP\\2014D00021.doc";
//String toFile = "D:\\TMP\\2014D00021.pdf";
ActiveXComponent app = new ActiveXComponent("Word.Application");
try {
app.setProperty("Visible", new Variant(false));
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.invoke(
docs,
"Open",
Dispatch.Method,
new Object[] { docfile, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
File file1 = new File(toFile);
File file2 = new File(toFile + ".pdf");
if (file1.exists()) {
file1.delete();
}
if (file2.exists()) {
file2.delete();
}
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
toFile, new Variant(17) }, new int[1]);
Variant f = new Variant(false);
Dispatch.call(doc, "Close", f);
log.info("-----------------------------------转换成功:" + toFile);
} catch (Exception e) {
e.printStackTrace();
log.info("-------------------------------------转换失败:" + docfile + ", " + e.getMessage());
} finally {
app.invoke("Quit", new Variant[] {});
} }
04-30 08:34