大家晚上好啊! 我是尋覓!
前幾天,網(wǎng)友遇到一個(gè)用Java實(shí)現(xiàn) word 轉(zhuǎn)換成 pdf 的問題。悲哀,本人解決不了!
后來網(wǎng)友解決了問題,找到本人,本人在這里謝謝那個(gè)網(wǎng)友!下面會(huì)介紹他的blog希望
大家沒事去看看,支持支持!謝謝!
匿名網(wǎng)友的blog:
http://blog.csdn.net/gavin_sw/archive/2007/04/11/1561254.aspx
好了,進(jìn)入正題 :
其實(shí),本人之所以失敗,和代碼沒關(guān)系;問題出在下面的設(shè)置上,本人因?yàn)闆]有
仔細(xì)閱讀方法中的設(shè)置部分,導(dǎo)致失敗!希望大家看完這個(gè)文,能避免我的錯(cuò)誤

;
本人把問題的解決分成多步,用最詳細(xì)的內(nèi)容讓大家一步成功!!!祝大家好運(yùn)!!!!!
1》
要用到的軟件:
(1)Adobe Acrobat 8 Professional (最低版本7.03)
(個(gè)人非商業(yè)使用)8.0破解版下載地址: http://green.crsky.com/soft/2205.html (記得下載補(bǔ)丁)
安裝文件 http://down1.greendown.cn//200611/AcroPro80_efg.rar
破解 http://soft.greendown.cn//200611/AcroPro80_Crack.rar
(2)gs811w32.rar (PDF轉(zhuǎn)換時(shí)所需要的腳本ps)
http://www.allmail.com.cn/gs811w32.rar
(3)postscript.rar (PDF虛擬打印機(jī)的驅(qū)動(dòng))
http://www.pdfhome.com.cn/Resource/DownLoad/postscript.rar
(4)jacob.jar
jacob_1.9.zip
(5)office 2003
2》
原理:
jacob.jar
doc -> ps ->pdf >>>> office 2003 ->gs811w32->Adobe Acrobat 8->postscript->打印機(jī)
(其中關(guān)于jacob,jar的安裝請(qǐng)看:
jacob使用入門及問題解析)
3》
安裝運(yùn)行:
(1)安裝 Adobe Acrobat 8 Professional
(2)安裝 gs811w32.rar
(3)配置打印機(jī)(這里不需要真實(shí)的打印機(jī))
控制面板》 打印機(jī)及其他硬件》打印機(jī)和傳真》添加打印機(jī)
(如果添加時(shí)顯示“操作無法完成。打印后臺(tái)程序服務(wù)沒有運(yùn)行。”
請(qǐng)打開控制面板》性能和維護(hù)》管理工具》服務(wù)》找到“Print Spooler”
》右擊屬性》啟動(dòng))》選擇本地打印機(jī)(如果沒有打印機(jī)請(qǐng)將“檢測(cè)并安裝
即插打印機(jī)”的鉤去掉)》下一步》選擇“使用以下端口”
(My Document/*.pdf (Adobe PDF Port))》下一步 選擇打印機(jī)》我選擇
的是Apple的 Color LaserWriter 12/600(
工作后,有錢一定要買個(gè)Apple hp)
》下一步(記住打印機(jī)的名字:Apple Color LaserWriter 12/600)
》下一步(沒有打印機(jī)的朋友請(qǐng)選擇:不測(cè)試)
(4)安裝 postscript.rar (安裝時(shí),注意每一步,選擇與前面設(shè)置相關(guān)的選項(xiàng))
(5)設(shè)置Adobe Acrobat 8 Professional:選擇一個(gè)pdf文件,右擊打開方式選擇
使用 打開Adobe Acrobat 8 Professional》選擇file菜單》Print Setup...》打印選項(xiàng)
屬性“Apple Color LaserWriter 12/600”》確定
(6)運(yùn)行下面的代碼:
代碼:(相信大家已經(jīng)看到成功的曙光了

,有問題請(qǐng)留言,或找本人,QQ:492006004
歡迎加入我管理的群:37486623)

/** *//**
* @author XuMing Li
*
* @version 1.00, 2007-4-9
*
*/
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

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文檔對(duì)象
*/

public boolean openWord(String filePath)
{
//建立ActiveX部件
wordCom = new ActiveXComponent("Word.Application");


try
{
//返回wrdCom.Documents的Dispatch
Dispatch wrdDocs = wordCom.getProperty("Documents").toDispatch();
//調(diào)用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;
}


/** *//** *//** *//**
* 關(guān)閉word文檔
*/

public void closeWord()
{
//關(guān)閉word文件

wordCom.invoke("Quit", new Variant[]
{});
}


/** *//** *//** *//**
* * 將word文檔打印為PS文件后,使用Distiller將PS文件轉(zhuǎn)換為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對(duì)象
ActiveXComponent distiller = new ActiveXComponent(
"PDFDistiller.PDFDistiller.1");

try
{
//設(shè)置當(dāng)前使用的打印機(jī),我的Adobe Distiller打印機(jī)名字為"Adobe PDF"
wordCom.setProperty("ActivePrinter", new Variant("MS Publisher Color Printer"));
//設(shè)置printout的參數(shù),將word文檔打印為postscript文檔。目前只使用了前5個(gè)參數(shù),如果要使用更多的話可以參考MSDN的office開發(fā)相關(guān)api
//是否在后臺(tái)運(yùn)行
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文檔成功!");
//調(diào)用Distiller對(duì)象的FileToPDF方法所用的參數(shù),詳細(xì)內(nèi)容參考Distiller Api手冊(cè)
//作為輸入的ps文檔路徑
Variant inputPostScriptFilePath = new Variant(destinPSFilePath);
//作為輸出的pdf文檔的路徑
Variant outputPDFFilePath = new Variant(destinPDFFilePath);
//定義FileToPDF方法要使用adobe pdf設(shè)置文件的路徑,在這里沒有賦值表示并不使用pdf配置文件
Variant PDFOption = new Variant("");
//調(diào)用FileToPDF方法將ps文檔轉(zhuǎn)換為pdf文檔

Dispatch.callN(distiller, "FileToPDF", new Variant[]
{
inputPostScriptFilePath, outputPDFFilePath, PDFOption });
System.out.println("由ps文檔轉(zhuǎn)換為pdf文檔成功!");

} catch (Exception ex)
{
ex.printStackTrace();

} finally
{
closeWord();
wordCom=null;
//釋放在程序線程中引用的其它c(diǎn)om,比如Adobe PDFDistiller
ComThread.Release();
}
}


public static void main(String[] argv)
{
D2P d2p = new D2P();
d2p.docToPDF("d:/12.doc", "d:/1p.ps", "d:/1p.pdf");
//這里是你建一個(gè)叫12.doc的word文檔,生成的文檔將在D盤下
//1p.ps和1p.pdf(這是我們要的)
}
}
地震讓大伙知道:居安思危,才是生存之道。
posted on 2007-04-12 20:08
小尋 閱讀(1884)
評(píng)論(4) 編輯 收藏 所屬分類:
j2se/j2ee/j2me