利用jacob包实现将office文档转换为pdf格式

1,264 阅读2分钟

jacob解释

jacob官方解释为JavaComBridge,java和COM组件之间的桥梁。由于office本身是一个基于windows操作系统的软件,除了windows的宏,基本没有直接能操作office的了。在windows平台上像这样缺乏通用api来操作软件,于是就有了com的解决方案。

我们使用dll中的一组或多组相关的函数存取组件数据,总的合称为接口,具体到每个细节的实现称为方法;如果我们要调用接口里的方法,唯一的途径就是,调用指向接口的指针;

所以总的来说使用就是dll完成api的转换;

jacob.jar是我们要用的包

jacob.dll就是我前面说的com组件

包里的jacob.dll放到c:/windows/system32下

值得注意的是,不同的版本的系统使用不同的dll文件

所以如果你编译成功,但运行失败一般是dll文件问题

遇到这种情况,可以到

downloads.sourceforge.net/jacob-proje…

下载其他的版本的 dll 文件。

文档转换代码

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

public class MsOffice2Pdf {

	private static final int wdFormatPDF = 17;
	private static final int xlTypePDF = 0;
	private static final int ppSaveAsPDF = 32;
	

	public boolean convert2PDF(String inputFile, String pdfFile) {
		String suffix = getFileSufix(inputFile);
		File file = new File(inputFile);
		if (!file.exists()) {
			return false;
		}
		if (suffix.equals("pdf")) {
			return false;
		}
		if (suffix.equals("doc") || suffix.equals("docx") || suffix.equals("wps")) {
			return word2PDF(inputFile, pdfFile);
		} else if (suffix.equals("ppt") || suffix.equals("pptx")) {
			return ppt2PDF(inputFile, pdfFile);
		} else if (suffix.equals("xls") || suffix.equals("xlsx")) {
			return excel2PDF(inputFile, pdfFile);
		} else {
			return false;
		}
	}

	public static String getFileSufix(String fileName) {
		int splitIndex = fileName.lastIndexOf(".");
		return fileName.substring(splitIndex + 1);
	}

	// word转换为pdf
	public static boolean word2PDF(String inputFile, String pdfFile) {
		try {
			// 打开word应用程序
			ActiveXComponent app = new ActiveXComponent("Word.Application");
			// 设置word不可见
			app.setProperty("Visible", false);
			// 获得word中所有打开的文档,返回Documents对象
			Dispatch docs = app.getProperty("Documents").toDispatch();
			// 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
			Dispatch doc = Dispatch.call(docs, "Open", inputFile, false, true).toDispatch();
			// 调用Document对象的SaveAs方法,将文档保存为pdf格式
			/*
			 * Dispatch.call(doc, "SaveAs", pdfFile, wdFormatPDF
			 * //word保存为pdf格式宏,值为17 );
			 */
			Dispatch.call(doc, "ExportAsFixedFormat", pdfFile, wdFormatPDF);// word保存为pdf格式宏,值为17
			// 关闭文档
			Dispatch.call(doc, "Close", false);
			// 关闭word应用程序
			app.invoke("Quit", 0);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	// excel转换为pdf
	public static boolean excel2PDF(String inputFile, String pdfFile) {
		try {
			ActiveXComponent app = new ActiveXComponent("Excel.Application");
			app.setProperty("Visible", false);
			Dispatch excels = app.getProperty("Workbooks").toDispatch();
			Dispatch excel = Dispatch.call(excels, "Open", inputFile, false, true).toDispatch();
			Dispatch.call(excel, "ExportAsFixedFormat", xlTypePDF, pdfFile);
			Dispatch.call(excel, "Close", false);
			app.invoke("Quit");
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	// ppt转换为pdf
	public static boolean ppt2PDF(String inputFile, String pdfFile) {
		try {
			ActiveXComponent app = new ActiveXComponent("PowerPoint.Application");
			app.setProperty("Visible", true);
			Dispatch ppts = app.getProperty("Presentations").toDispatch();

			/*Dispatch ppt = Dispatch.call(ppts, "Open", inputFile, true, // ReadOnly
					true, // Untitled指定文件是否有标题
					false// WithWindow指定文件是否可见
			).toDispatch();*/
			Dispatch ppt = Dispatch.call(ppts, "Open", inputFile, false, true).toDispatch();
			Dispatch.call(ppt, "SaveAs", pdfFile, ppSaveAsPDF);

			Dispatch.call(ppt, "Close");

			app.invoke("Quit");
			return true;
		} catch (Exception e) {
			return false;
		}
	}

}