一、實現方式
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
***********************************************************************************************