原文連結:http://blog.csdn.net/gavin_sw/archive/2007/04/11/1561254.aspx
本文內容參考:
http://www.matrix.org.cn/thread.shtml;jsessionid=B1E4B57897D51B59802D353CB6B32ACC?topicId=29594&forumId=17
準備工作:
1. 安裝 "Adobe Acrobat 7.0 Professional" 並升級到"7.0.5"
2.安裝"gs811w32.rar" (預設安裝就可以了,它是一個PDF轉換時所需要的指令碼)
3.安裝"postscript.rar" (預設安裝就可以,它其實是個PDF虛擬印表機的驅動)
4.虛擬印表機配置,參考http://www.matrix.org.cn/thread.shtml;jsessionid=B1E4B57897D51B59802D353CB6B32ACC?topicId=29594&forumId=17
一點需要稍微留意:wordCom.setProperty("ActivePrinter", new Variant("MS Publisher Color Printer"));
這行代碼中的"MS Publisher Color Printer"對應安裝的虛擬印表機名稱,請用以下代碼測試。
/** *//**
* @author XuMing Li
*
* @version 1.00, 2007-4-9
*
*/
public class D2P ...{
private ActiveXComponent wordCom = null;
private Object wordDoc = null;
private final Variant False = new Variant(false);
private final Variant True = new Variant(true);
/** *//**
* 開啟word文檔
*
* @param filePath
* word文檔
* @return 返回word文檔對象
*/
public boolean openWord(String filePath) ...{
//建立ActiveX組件
wordCom = new ActiveXComponent("Word.Application");
try ...{
//返回wrdCom.Documents的Dispatch
Dispatch wrdDocs = wordCom.getProperty("Documents").toDispatch();
//調用wrdCom.Documents.Open方法開啟指定的word文檔,返回wordDoc
wordDoc = Dispatch.invoke(wrdDocs, "Open", Dispatch.Method,
new Object[] ...{ filePath }, new int[1]).toDispatch();
return true;
} catch (Exception ex) ...{
ex.printStackTrace();
}
return false;
}
/** *//**
* 關閉word文檔
*/
public void closeWord() ...{
//關閉word檔案
wordCom.invoke("Quit", new Variant[] ...{});
}
/** *//**
* * 將word文檔列印為PS檔案後,使用Distiller將PS檔案轉換為PDF檔案 *
*
* @param sourceFilePath
* 源檔案路徑 *
* @param destinPSFilePath
* 首先產生的PS檔案路徑 *
* @param destinPDFFilePath
* 產生PDF檔案路徑
*/
public void docToPDF(String sourceFilePath, String destinPSFilePath,
String destinPDFFilePath) ...{
if (!openWord(sourceFilePath)) ...{
closeWord();
return;
}
//建立Adobe Distiller的com對象
ActiveXComponent distiller = new ActiveXComponent(
"PDFDistiller.PDFDistiller.1");
try ...{
//設定當前使用的印表機,我的Adobe Distiller印表機名字為"Adobe PDF"
wordCom.setProperty("ActivePrinter", new Variant("MS Publisher Color Printer"));
//設定printout的參數,將word文檔列印為postscript文檔。目前只使用了前5個參數,如果要使用更多的話可以參考MSDN的office開發相關api
//是否在後台運行
Variant Background = False;
//是否追加列印
Variant Append = False;
//列印所有文檔
int wdPrintAllDocument = 0;
Variant Range = new Variant(wdPrintAllDocument);
//輸出的postscript檔案的路徑
Variant OutputFileName = new Variant(destinPSFilePath);
Dispatch.callN((Dispatch) wordDoc, "PrintOut", new Variant[] ...{
Background, Append, Range, OutputFileName });
System.out.println("由word文檔列印為ps文檔成功!");
//調用Distiller對象的FileToPDF方法所用的參數,詳細內容參考Distiller Api手冊
//作為輸入的ps文檔路徑
Variant inputPostScriptFilePath = new Variant(destinPSFilePath);
//作為輸出的pdf文檔的路徑
Variant outputPDFFilePath = new Variant(destinPDFFilePath);
//定義FileToPDF方法要使用adobe pdf設定檔案的路徑,在這裡沒有賦值表示並不使用pdf設定檔
Variant PDFOption = new Variant("");
//調用FileToPDF方法將ps文檔轉換為pdf文檔
Dispatch.callN(distiller, "FileToPDF", new Variant[] ...{
inputPostScriptFilePath, outputPDFFilePath, PDFOption });
System.out.println("由ps文檔轉換為pdf文檔成功!");
} catch (Exception ex) ...{
ex.printStackTrace();