package com.merge.rui;  

import java.io.File;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;

public class Office2Pdf {
	
public static void main(String[] args) {

		String filename = "C:\\Users\\rui\\Desktop\\test\\Swing.docx";
		Word2PDF(filename);
	}

/**
 * word转pdf,路径+文件名
 */
static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。
static final int wdFormatPDF = 17;// PDF 格式
public static void Word2PDF(String filename){
	String toFilename = filename.substring(0,filename.indexOf(".")) + ".pdf";
	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);
		System.out.println(filename+" convert pdf is done");
//		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", wdDoNotSaveChanges);
	}
	
}

}
最近下载更多
gshnlj  LV15 2022年8月24日
2252536772  LV21 2022年2月18日
thornton2011  LV2 2021年9月2日
aihui523  LV34 2021年3月23日
wupujian  LV17 2020年12月30日
18321000850  LV14 2020年6月24日
EdgarLi  LV14 2020年5月16日
kkalpha  LV5 2020年3月20日
qq371348836  LV7 2020年3月12日
xuyongff  LV24 2019年11月4日
最近浏览更多
syd1988  LV7 3月14日
孤留光乩 2023年12月3日
暂无贡献等级
chen影 2023年10月31日
暂无贡献等级
zj0010722  LV2 2023年3月2日
G你太美  LV9 2022年10月13日
PaymentCodeSystem  LV11 2022年8月4日
funcrit  LV2 2022年7月18日
mengfanyun  LV9 2022年7月4日
xytthy  LV3 2022年4月25日
Yanxigul 2022年3月7日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友