一、实现方式
java采用jacob包的功能,把ppt演示文稿转换为pdf。
支持文件格式:pptx,ppt
二、事先准备
1、依赖于office,需安装office办公软件
2、需要下载一个jacob-1.20-x64.dll的文件,放到java的bin目录下。
文件可以网上搜索下载。也可以点击百度网盘下载链接:
https://pan.baidu.com/s/16y-N03KPQJkne6g4sMLAmg?pwd=ix4j
三、Java转换代码
1、maven包依赖:
<dependency><groupId>com.jacob</groupId><artifactId>jacob</artifactId><version>1.20</version></dependency>
2、java转换类:JacobUtil.java
主要看ppt2PDF方法。
package com.lan.fts.util;import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;/*** excel word ppt转pdf* 未完全完善* @author LAN* @date 2021年07月01日*/
public class JacobUtil {private static Logger log = LoggerFactory.getLogger(JacobUtil.class);private static final Integer WORD_TO_PDF_OPERAND = 17;private static final Integer PPT_TO_PDF_OPERAND = 32;private static final Integer EXCEL_TO_PDF_OPERAND = 0;public static void word2PDF(String inputFile, String pdfFile) {ComThread.InitSTA();ActiveXComponent app = new ActiveXComponent("Word.Application");try {app.setProperty("Visible", false);Dispatch docs = app.getProperty("Documents").toDispatch();Dispatch doc = Dispatch.call(docs, "Open", new Object[]{inputFile, false, true}).toDispatch();Dispatch.call(doc, "ExportAsFixedFormat", new Object[]{pdfFile, WORD_TO_PDF_OPERAND});Dispatch.call(doc, "Close", new Object[]{false});} catch (Exception e) {e.printStackTrace();System.out.println("转换出错:" + pdfFile);} finally {app.invoke("Quit");ComThread.Release();}}public static void excel2PDF(String inputFile, String pdfFile) {ComThread.InitSTA(true);ActiveXComponent app = new ActiveXComponent("Excel.Application");try {app.setProperty("Visible", false);app.setProperty("AutomationSecurity", new Variant(3));// 禁用宏Dispatch excels = app.getProperty("Workbooks").toDispatch();Object[] param1 = new Object[]{inputFile, new Variant(false), new Variant(false)};Dispatch excel = Dispatch.invoke(excels, "Open", Dispatch.Method, param1, new int[9]).toDispatch();//转换Object[] param2 = new Object[]{new Variant(EXCEL_TO_PDF_OPERAND), // PDF格式=0pdfFile,new Variant(0) //0=标准 (生成的PDF图片不会变模糊) ; 1=最小文件};Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, param2, new int[1]);//关闭Dispatch.call(excel, "Close", new Object[]{false});} catch (Exception e) {e.printStackTrace();System.out.println("转换出错:" + pdfFile);} finally {app.invoke("Quit");ComThread.Release();}}private static ActiveXComponent ppt_app = new ActiveXComponent("PowerPoint.Application");public static void ppt2PDF(String inputFile, String pdfFile) {// ComThread.InitSTA();Dispatch ppts = ppt_app.getProperty("Presentations").toDispatch();try {/*** call* param 4: ReadOnly* param 5: Untitled指定文件是否有标题* param 6: WithWindow指定文件是否可见* */Dispatch ppt = Dispatch.call(ppts, "Open", new Object[]{inputFile, true, true, false}).toDispatch();Dispatch.call(ppt, "SaveAs", new Object[]{pdfFile, PPT_TO_PDF_OPERAND});//关闭Dispatch.call(ppt, "Close");} catch (Exception e) {log.error("转换出错:" + inputFile, e);try{ppt_app.invoke("Quit");}catch (Exception e1){log.error("ppt_app.invoke(\"Quit\")关闭出错!",e);}finally{ppt_app = new ActiveXComponent("PowerPoint.Application");}} finally {// ppt_app.invoke("Quit");// ComThread.Release();}}
}
四、运行测试
public static void main(String[] args) {JacobUtil.ppt2PDF("D:\\data\\out\\lanhezhong文件转换.pptx", "D:\\data\\out\\lanhezhong文件转换.pptx.pdf");}
运行结果:
总结:ppt转pdf转换的结果比较好,基本上保持ppt的原本内容格式。
***********************************************************************************************
author:蓝何忠
email:lanhezhong@163.com
***********************************************************************************************