import java.io.File;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class WFileToPdf {
private ActiveXComponent wordCom = null;
private Dispatch 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文件
if (wordCom != null) {
int save = 0;
Variant doNotSaveChanges = new Variant(save);
wordCom.invoke("Quit", new Variant[] { doNotSaveChanges });
wordCom = null;
ComThread.Release();
}
}
/**
* 刪除臨時文件
*/
public void deletetmpFile(String psfileName,String logFileName){
File psfile = new File(psfileName);
String s [] = new String(logFileName).split(".pdf");
File logfile = new File(s[0]+".log");
System.out.println(logfile.getName());
if (psfile.exists()) {
psfile.delete();
System.out.println("刪除臨時PS文件");
}
if(logfile.exists()){
logfile.delete();
System.out.println("刪除臨時日誌文件");
}
}
/**
* 將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("Adobe PDF"));
// 是否在後臺運行
Variant Background = False;
// 是否追加打印
Variant Append = False;
// 打印所有文檔
int wdPrintAllDocument = 0;
Variant Range = new Variant(wdPrintAllDocument);
// 輸出的postscript文件的路徑
Variant OutputFileName = new Variant(destinPSFilePath);
// 調用word文件對象的PrintOut方法:將word文檔打印爲postscript文檔,簡稱ps文檔
Dispatch.callN(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();
} finally {
closeWord();
deletetmpFile(destinPSFilePath,destinPDFFilePath);
}
}
public static void main(String[] argv) {
WFileToPdf d=new WFileToPdf();
d.docToPDF("d:\\補充保密協議.doc","d:\\ww.ps","d:\\www.pdf");
}
}
另外 ,要 下載 Adobe Acrobat軟件,安裝到本機上,在打印機屬性----打印首選項-----Adobe PDF設置---- “僅依靠系統字體;不使用文檔字體” 這個選項關閉,即不勾上