---
title: "word,ppt,excel转pdf（只需用office自带功能）"
author: "josico"
date: "2012-07-03T10:11:47Z"
tags: ["Java", "PDF", "Word", "文档转换"]
categories: ["编程语言"]
slug: "word-ppt-excel-pdf-office"
draft: false
lastmod: "2012-07-03T13:55:08Z"
id: 1575258
url: "https://josico.iteye.com/blog/1575258"
---

之前也用过其他的一些方式，比如调工具。但那只能转部分格式

还用过Adobe Acrobat 8 Professional，但用java代码转格式的时候，这个软件会打开，造成速率较慢

 

最终的解决方案，放弃一切其他工具和软件，只用office自带的功能实现word,ppt,excel到pdf的转变

PS.你的office必须是07及以上，并且需要在文件另存为选项中有，“另存为pdf或XPS” 的一项

如果没有，你需要下载office相应插件SaveAsPDFandXPS

 

这里有个疑问，想问下大牛，希望大牛们不吝赐教，哞~~

安装完插件后在word的帮助里面，选搜索**→**点脱机开发人员帮助

再点 Word 2007 开发人员参考**→**新增内容**→**新的成员和常量**→**WdSaveFormat **→**wdFormatPDF

同样 在ppt里面，我也找得到对象ppt到pdf的常量

但在excel里面找了很长时间都没找到， 既然excel的文件另存为那里有“另存为pdf”，那就应该有这个常量啊。请大神们赐教啊![](/images/smiles/icon_idea.gif)

工程必备文件

需要jar包

jacob.jar

 

需要dll文件（放在system32下）

jacob-1.15-M4-x86.dll

 

 

好了，准备工作做完，废话不多说，我们代码揍起来

 

```
import java.io.File;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class Word2Pdf {

	static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。
	static final int wdFormatPDF = 17;// word转PDF 格式
	static final int ppSaveAsPDF = 32;// ppt 转PDF 格式

	public static void main(String[] args) {
		String source1 = "D:\\test.doc";
		String source2 = "D:\\a.xls";
		String source3 = "D:\\aa.ppt";
		String target1 = "D:\\test1.pdf";
		String target2 = "D:\\test2.pdf";
		String target3 = "D:\\test3.pdf";
		
		Word2Pdf pdf = new Word2Pdf();
		
		
		pdf.word2pdf(source1, target1);
		pdf.excel2pdf(source2, target2);
		pdf.ppt2pdf(source3, target3);
		
		
	}

	public void word2pdf(String source,String target){
		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("打开文档" + source);
			Dispatch doc = Dispatch.call(docs,//
					"Open", //
					source,// FileName
					false,// ConfirmConversions
					true // ReadOnly
					).toDispatch();

			System.out.println("转换文档到PDF " + target);
			File tofile = new File(target);
			if (tofile.exists()) {
				tofile.delete();
			}
			Dispatch.call(doc,//
					"SaveAs", //
					target, // 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());
		} finally {
			if (app != null)
				app.invoke("Quit", wdDoNotSaveChanges);
		}
	}

	public void ppt2pdf(String source,String target){
		System.out.println("启动PPT");
		long start = System.currentTimeMillis();
		ActiveXComponent app = null;
		try {
			app = new ActiveXComponent("Powerpoint.Application");
			Dispatch presentations = app.getProperty("Presentations").toDispatch();
			System.out.println("打开文档" + source);
			Dispatch presentation = Dispatch.call(presentations,//
					"Open", 
					source,// FileName
					true,// ReadOnly
					true,// Untitled 指定文件是否有标题。
					false // WithWindow 指定文件是否可见。
					).toDispatch();

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

			Dispatch.call(presentation, "Close");
			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");
		}
	}

	public void excel2pdf(String source, String target) {
		System.out.println("启动Excel");
		long start = System.currentTimeMillis();
		ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动excel(Excel.Application)
		try {
		app.setProperty("Visible", false);
		Dispatch workbooks = app.getProperty("Workbooks").toDispatch();
		System.out.println("打开文档" + source);
		Dispatch workbook = Dispatch.invoke(workbooks, "Open", Dispatch.Method, new Object[]{source, new Variant(false),new Variant(false)}, new int[3]).toDispatch();
		Dispatch.invoke(workbook, "SaveAs", Dispatch.Method, new Object[] {
		target, new Variant(57), new Variant(false),
		new Variant(57), new Variant(57), new Variant(false),
		new Variant(true), new Variant(57), new Variant(true),
		new Variant(true), new Variant(true) }, new int[1]);
		Variant f = new Variant(false);
		System.out.println("转换文档到PDF " + target);
		Dispatch.call(workbook, "Close", f);
		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", new Variant[] {});
			}
		}
	}
}
```

  代码里面，命名很清楚，word2pdf，ppt2pdf，excel2pdf，不用解释是什么意思吧

  按需求，大家各取所需。

   PS.第一次在eye发帖，好紧张，好羞射