Convert java word, excel, ppt to pdf

Preparation

1. Download jacob.jar 
link: https://pan.baidu.com/s/1TWIGyX9A3xQ6AG9Y3mVlVg 
extraction code: abcd

2. Download and install wps
WPS Office - supports multiple people to edit multiple document formats online_WPS official website

3. Add jar to the project and place the ddl file in the jre/bin directory of jdk. Remember whether your system is 64-bit or 32-bit.

The ddl file is placed in the jdk directory

If the system does not have javahome set up, remember to set it up.

path plus

 4. Add jacob.jar to the project. I use the idea springboot maven project and execute maven to package it into the project.

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

Replace the red font with your own

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

5. After the preparations are completed, let’s write the code

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;
		}
	}

}

 Note: Originally I didn’t know that WPS can also be converted, so I used office2016. But to convert excel, you need to download the corresponding office plug-in SaveAsPDFandXPS, which is more troublesome, so I used the office code. I searched it online. I don’t know whose code I forgot. Sorry, I'm sorry to say this.

 

 

Guess you like

Origin blog.csdn.net/weixin_41018853/article/details/128081069