一乘陪、 Jacob 環(huán)境配置
① 下載Office套件
② 下載Jacob-1.1.8.jar包
鏈接:https://sourceforge.net/projects/jacob-project/
解壓后獲得3個(gè)文件:
jacob.jar
jacob-1.1.8-x64.dll
jacob-1.1.8-x86.dll
(dll文件:Java應(yīng)用程序中調(diào)用COM組件和Win32程序庫(kù))
③ 拷貝dll文件
根據(jù)JDK的版本(32位/64位)佳遂,選擇對(duì)應(yīng)版本的dll文件,將文件拷貝到%JAVA_HOME%\jre\bin目錄下
例:C:\Program Files\Java\jdk1.8.0_121\bin\jacob-1.18-x64.dll
④ 項(xiàng)目中引用jacob.jar
二、Jacob示例
word/excel/ppt 轉(zhuǎn) PDF
import java.io.File;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
/**
* COM組件[Word\Excel\PPT]轉(zhuǎn)換PDF 轉(zhuǎn)換器
* @author Seven
*
*/
public class PdfConverter {
/**
* 轉(zhuǎn)換類型
*/
public static final int TYPE_WORD = 0;
public static final int TYPE_EXCEL = 1;
public static final int TYPE_PPT = 2;
/**
* 轉(zhuǎn)換類型
*/
public static Integer converterType ;
/**
* 源文件(路徑 + 文件名)
*/
public static String source;
/**
* 目標(biāo)文件(路徑 + 文件名)
*/
public static String dest;
public static void main(String[] args) {
try {
init(); //初始化參數(shù)配置
converter();//啟動(dòng)轉(zhuǎn)換功能
} catch (Exception e) {
e.printStackTrace();
}
}
private static void init(){
converterType = PdfConverter.TYPE_PPT;
source = "C:\\Users\\Seven\\Desktop\\測(cè)試demo.pptx" ;
dest = "C:\\Users\\Seven\\Desktop\\測(cè)試demo.pdf" ;
}
private static void converter() throws Exception{
File file = new File(source);
if(!file.exists()){
throw new Exception("轉(zhuǎn)換文件不存在");
}
switch(converterType){
case TYPE_WORD :
word2Pdf(source,dest);
break;
case TYPE_EXCEL :
excel2Pdf(source,dest);
break;
case TYPE_PPT :
ppt2Pdf(source,dest);
break;
default:
throw new Exception("文件轉(zhuǎn)換類型不存在");
}
}
public static void word2Pdf(String sourcePath, String destPath)
throws Exception {
ActiveXComponent word = null;
Dispatch documents = null;
try {
//初始化線程
ComThread.InitMTA(true);
//初始化應(yīng)用
word = new ActiveXComponent("Word.Application");
//設(shè)置應(yīng)用屬性
Dispatch.put(word.getObject(), "Visible", new Variant(false));
Dispatch.put(word.getObject(), "DisplayAlerts", new Variant(0));
//通過(guò)應(yīng)用打開(kāi)文檔
documents = word.invokeGetComponent("Documents")
.invokeGetComponent("Open", new Variant(sourcePath));
//調(diào)用對(duì)應(yīng)的方法
Dispatch.invoke(documents,
"SaveAs",
Dispatch.Method,
new Object[] {destPath , new Variant(17) },
new int[1]);
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
try {
//退出應(yīng)用
if (word != null) {
word.invoke("Quit", new Variant(false));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//關(guān)閉文檔和應(yīng)用
if (documents != null)
documents.safeRelease();
if (word != null)
word.safeRelease();
//釋放線程
ComThread.Release();
}
}
}
public static void excel2Pdf(String sourcePath, String destPath)
throws Exception {
ActiveXComponent excel = null;
Dispatch workBooks = null;
try {
ComThread.InitMTA(true);
excel = new ActiveXComponent("Excel.Application");
Dispatch.put(excel.getObject(), "Visible", new Variant(false));
Dispatch.put(excel.getObject(), "DisplayAlerts", new Variant(false));
workBooks = excel.invokeGetComponent("Workbooks")
.invokeGetComponent("Open", new Variant(sourcePath));
Dispatch.invoke(workBooks,
"ExportAsFixedFormat",
Dispatch.Method,
new Variant[]{new Variant(0),new Variant(destPath)},
new int[1]);
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
try {
if (excel != null) {
excel.invoke("Quit");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (workBooks != null)
workBooks.safeRelease();
if (excel != null)
excel.safeRelease();
ComThread.Release();
}
}
}
public static void ppt2Pdf(String sourcePath, String destPath)
throws Exception {
ActiveXComponent ppt = null;
Dispatch presentations = null;
try {
ComThread.InitMTA(true);
ppt = new ActiveXComponent("PowerPoint.application");
Dispatch.put(ppt.getObject(), "DisplayAlerts", new Variant(false));
presentations = ppt.invokeGetComponent("Presentations")
.invokeGetComponent("Open", new Variant(sourcePath));
Dispatch.invoke(presentations,
"SaveAs",
Dispatch.Method,
new Object[] {destPath , new Variant(32) },
new int[1]);
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
try {
if (ppt != null) {
ppt.invoke("Quit");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (presentations != null)
presentations.safeRelease();
if (ppt != null)
ppt.safeRelease();
ComThread.Release();
}
}
}
}