Convertir java word, excel, ppt a pdf

Preparación

1. Descargue 
el enlace jacob.jar: https://pan.baidu.com/s/1TWIGyX9A3xQ6AG9Y3mVlVg 
código de extracción: abcd

2. Descargue e instale wps
WPS Office: admite que varias personas editen múltiples formatos de documentos en línea_Sitio web oficial de WPS

3. Agregue jar al proyecto y coloque el archivo ddl en el directorio jre/bin de jdk. Recuerde si su sistema es de 64 o 32 bits.

El archivo ddl se coloca en el directorio jdk.

Si el sistema no tiene javahome configurado, recuerde configurarlo.

camino más

 4. Agregue jacob.jar al proyecto. Utilizo la idea del proyecto springboot maven y ejecuto maven para empaquetarlo en el proyecto.

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

Reemplaza la fuente roja por la tuya

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

5. Una vez completados los preparativos, escribamos el código.

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

}

 Nota: Originalmente no sabía que WPS también se puede convertir, así que usé Office 2016. Pero para convertir Excel, debes descargar el complemento de Office correspondiente SaveAsPDFandXPS, que es más problemático, así que usé el código de Office. Lo busqué en línea. No sé de quién es el código que olvidé. Lo siento, lamento decir esto.

 

 

Supongo que te gusta

Origin blog.csdn.net/weixin_41018853/article/details/128081069
Recomendado
Clasificación