java word,excel,ppt转pdf

准备工作

1.下载 jacob.jar 
链接:https://pan.baidu.com/s/1TWIGyX9A3xQ6AG9Y3mVlVg 
提取码:abcd

2.下载安装wps
WPS Office-支持多人在线编辑多种文档格式_WPS官方网站

3.添加 jar到项目和ddl文件放在jdk的jre/bin目录下,记得自己系统是64位还是32位的

ddl文件放jdk目录下

系统没有设置javahome的记得设置下

path加上

 4.添加jacob.jar到项目中,我用的是idea springboot maven项目,执行maven打包到项目中

mvn install:install-file -Dfile=d:\jacob.jar -DgroupId=com.jacob -DartifactId=jacob -Dversion=1.19 -Dpackaging=jar

红色字体换成自己的

<dependency>
	<groupId>com.jacob</groupId>
	<artifactId>jacob</artifactId>
	<version>1.19</version>
</dependency>

5.准备工作完成了我们上代码

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

public class ToPdfTest {

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

	public static void main(String[] args) {
		String path = "D:\\222.ppt"; // 自己在对应的位置放上需要转换的文件
		String pdfPath = "D:\\test222.pdf";// pdfPath为生成PDF文件路径
		//boolean bo = ToPdfTest.wordtoPDF(path, pdfPath); //word 转
		//boolean bo = ToPdfTest.exceltoPDF(path, pdfPath); //excel转
		boolean bo = ToPdfTest.ppttoPDF(path, pdfPath);
		if (bo) {
			System.out.println("恭喜您,转换完成!");
		} else {
			System.out.println("转换失败!");
		}
	}

	// word转换为PDF
	public static boolean wordtoPDF(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, "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 exceltoPDF(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", xlsTypePDF, pdfFile);
			Dispatch.call(excel, "Close", false);
			app.invoke("Quit");
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	// PPT转换为PDF
	public static boolean ppttoPDF(String inputFile, String pdfFile) {
		try {
			//ActiveXComponent app = new ActiveXComponent("KWPP.Application");//PowerPoint
			ActiveXComponent app = new ActiveXComponent("PowerPoint.Application");
			Dispatch ppts = app.getProperty("Presentations").toDispatch();

			Dispatch ppt = Dispatch.call(ppts, "Open", inputFile, true, // ReadOnly
					true, // Untitled指定文件是否有标题
					false// WithWindow指定文件是否可见
			).toDispatch();
			Dispatch.call(ppt, "SaveAs", pdfFile, ppSaveAsPDF);
			Dispatch.call(ppt, "Close");
			app.invoke("Quit");
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

}

 注意:原先我不知道wps也可以转,就用的office2016 但是excel转换你需要下载office相应插件SaveAsPDFandXPS,比较麻烦,所以我就用的office  代码我是网上搜的,不知道是谁的代码我忘了,这里说声不好意思

猜你喜欢

转载自blog.csdn.net/weixin_41018853/article/details/128081069