Word轉(zhuǎn)pdf方法小結(jié)
系統(tǒng)
2019-08-12 09:27:30
1827 0
前提條件:安裝
Acrobat Distillr,可以通過(guò)安裝
Acrobat
Professional 版本實(shí)現(xiàn)。
1.在Winform下實(shí)現(xiàn)轉(zhuǎn)換
很簡(jiǎn)單,需要引用COM組件 PDFMake
Code
????????
public
?
static
?
string
?ConvertPdf(
string
?p_sFileSrc,
string
?sTgtFile)
????????{
????????????
object
?missing?
=
?System.Type.Missing;
????????????
try
????????????{
????????????????PDFMAKERAPILib.PDFMakerApp?app?
=
?
new
?PDFMAKERAPILib.PDFMakerApp();
??
????????????????
if
?(File.Exists(sSrcFile))
????????????????{
????????????????????
//
獲取PDF生成路徑
????????????????????
int
?bSuccess?
=
?app.CreatePDF(sSrcFile,?sTgtFile,?PDFMAKERAPILib.PDFMakerSettings.kConvertAllPages,?
false
,?
true
,?
true
,?missing);
????????????????????
return
?Enum.GetName(
typeof
(PDFMAKERAPILib.PDFMakerRetVals),?bSuccess);
????????????????}
????????????????
else
????????????????{
????????????????????
return
?
"
源文件不存在!
"
;
????????????????}
????????????}
????????????
catch
?(Exception?er)
????????????{
????????????????
return
?
"
文件轉(zhuǎn)換失??!
"
;
????????????}
????????}
????}
?
2.在WebForm下轉(zhuǎn)換
比上面復(fù)雜點(diǎn),因?yàn)樯厦娴拇a
在IIS時(shí),就是轉(zhuǎn)換不成功,花了點(diǎn)時(shí)間,發(fā)現(xiàn)原因是兩個(gè)的用戶不同,一個(gè)是管理員,一個(gè)是Asp.net 帳戶或者是network Service 看IIS是5.0 還是6.0。想通過(guò)更改設(shè)置權(quán)限來(lái)解決,改了不少,效果是從一個(gè)錯(cuò)誤,變成了另一個(gè)錯(cuò)誤。
從網(wǎng)上搜索到下面這個(gè)辦法,也是比較簡(jiǎn)單的。
前提條件:
(1)安裝虛擬打印機(jī):
進(jìn)入WINDOWS的控制面板,進(jìn)入打印機(jī),點(diǎn)擊"添加打印機(jī)"圖標(biāo),在安裝對(duì)話框上"按一步",出現(xiàn)選擇打印機(jī)時(shí),在制造商一欄中選擇"Generic",在打印機(jī)一欄中,選擇"MS Publisher Color Printer",然后一路按下一步,知道安裝結(jié)束。
以下代碼并沒有提供當(dāng)前的打印機(jī),而是使用系統(tǒng)默認(rèn)的,即設(shè)置的"MS Publisher Color Printer"。
(2)必須添加引用Acrobat Distiller與WORD,可以從COM組件中添加。
Code
private
?
void
?WordConvert()
{
????oWord.ApplicationClass?word?
=
?
new
?Microsoft.Office.Interop.Word.ApplicationClass();
????Type?wordType?
=
?word.GetType();
????oWord.Documents?docs?
=
?word.Documents;
????Type?docsType?
=
?docs.GetType();
????
object
?objDocName?
=
?
@"
c:\tmp\test.doc
"
;
????oWord.Document?doc?
=
?(oWord.Document)docsType.InvokeMember(
"
Open
"
,?System.Reflection.BindingFlags.InvokeMethod,?
null
,?docs,?
new
?Object[]?{?objDocName,?
true
,?
true
?});
????
//
打印輸出到指定文件
????Type?docType?
=
?doc.GetType();
????
object
?printFileName?
=
?
@"
c:\tmp\test.ps
"
;
????docType.InvokeMember(
"
PrintOut
"
,?System.Reflection.BindingFlags.InvokeMethod,?
null
,?doc,?
new
?
object
[]?{?
false
,?
false
,?oWord.WdPrintOutRange.wdPrintAllDocument,?printFileName?});
????wordType.InvokeMember(
"
Quit
"
,?System.Reflection.BindingFlags.InvokeMethod,?
null
,?word,?
null
);
????
string
?o1?
=
?
"
c:\\tmp\\test.ps
"
;
????
string
?o2?
=
?
"
c:\\tmp\\test.pdf
"
;
????
string
?o3?
=
?
""
;
????
//
引用將PS轉(zhuǎn)換成PDF的對(duì)象
????
try
????{
????????ACRODISTXLib.PdfDistillerClass?pdf?
=
?
new
?ACRODISTXLib.PdfDistillerClass();
????????pdf.FileToPDF(o1,?o2,?o3);
????}
????
catch
?{?}?
????
//
為防止本方法調(diào)用多次時(shí)發(fā)生錯(cuò)誤,必須停止acrodist.exe進(jìn)程
????
foreach
?(Process?proc?
in
?System.Diagnostics.Process.GetProcesses())
????{
????????
int
?begpos;
????????
int
?endpos;
????????
string
?sProcName?
=
?proc.ToString();
????????begpos?
=
?sProcName.IndexOf(
"
(
"
)?
+
?
1
;
????????endpos?
=
?sProcName.IndexOf(
"
)
"
);
????????sProcName?
=
?sProcName.Substring(begpos,?endpos?
-
?begpos);
????????
if
?(sProcName.ToLower().CompareTo(
"
acrodist
"
)?
==
?
0
)
????????{
????????????
try
????????????{
????????????????proc.Kill();?
????????????}
????????????
catch
?{?}??
????????????
break
;
????????}
????}
}
Word轉(zhuǎn)pdf方法小結(jié)
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】 元
喜歡作者