? ? ? 利用office自帶的COM類型庫組件實(shí)現(xiàn)轉(zhuǎn)換Pdf功能。只要安裝了office的服務(wù)器上都可以調(diào)用,不需要額外的第三方組件,功能也更加豐富和強(qiáng)大,幾乎可以不受限制的操作office所有類型文件。缺點(diǎn)是部署問題多,發(fā)布到客戶服務(wù)器進(jìn)行調(diào)試的話問題很多。禁忌:1,開發(fā)的時(shí)候調(diào)用,不同office版本的COM組件,比如Microsoft.Office.Interop.Word是v14,那ppt、excel等組件都要統(tǒng)一版本,不然問題很多;2,部署的服務(wù)器上只能安裝一個(gè)版本的office,比如開發(fā)時(shí)調(diào)用的Office 2010,那部署的服務(wù)器就只能裝 office 2010,建議不要混裝各種版本來匹配組件型號(hào),最終會(huì)導(dǎo)致哪個(gè)都不能用;3,常見的故障問題和解決方法附錄在該節(jié)末尾。
(1)利用Microsoft.Office.Interop.Word實(shí)現(xiàn)word轉(zhuǎn)換pdf.
首先安裝office 2010或其他更高版本。
添加引用Microsoft.Office.Interop. Word:
C#代碼中添加引用:
using System.Text;
?
using Microsoft.Office.Interop.Word;
using WdExportFormat = Microsoft.Office.Interop.Word.WdExportFormat;
?
創(chuàng)建WordToPdf方法:
/// <summary>
??????? /// 把Word文件轉(zhuǎn)換成pdf文件
??????? /// </summary>
??????? /// <param name="sourcePath">需要轉(zhuǎn)換的文件路徑和文件名稱</param>
??????? /// <param name="targetPath">轉(zhuǎn)換完成后的文件的路徑和文件名名稱</param>
??????? /// <returns>成功返回true,失敗返回false</returns>
??????? public static bool WordToPdf(stringsourcePath, string targetPath)
??????? {
???????????
??????????? bool result = false;
??????????? Microsoft.Office.Interop.Word.WdExportFormatwdExportFormatPDF = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;//轉(zhuǎn)換格式1.wdExportFormatPDF轉(zhuǎn)換成pdf格式 2.wdExportFormatXPS轉(zhuǎn)換成xps格式
??????????? object missing = Type.Missing;
??????????? Microsoft.Office.Interop.Word.ApplicationClassapplicationClass = null;
??????????? Document document = null;
??????????? try
??????????? {
??????????????? applicationClass = newMicrosoft.Office.Interop.Word.ApplicationClass();
??????????????? object inputfileName = sourcePath;//需要轉(zhuǎn)格式的文件路徑
??????????????? string outputFileName = targetPath;//轉(zhuǎn)換完成后PDF或XPS文件的路徑和文件名名稱
??????????????? WdExportFormat exportFormat = wdExportFormatPDF;//導(dǎo)出文件所使用的格式
??????????????? bool openAfterExport = false;//轉(zhuǎn)換完成后是否打開
??????????????? WdExportOptimizeForwdExportOptimizeForPrint = WdExportOptimizeFor.wdExportOptimizeForPrint;//導(dǎo)出方式1.wdExportOptimizeForPrint針對(duì)打印進(jìn)行導(dǎo)出,質(zhì)量較高,生成的文件大小較大。2.wdExportOptimizeForOnScreen 針對(duì)屏幕顯示進(jìn)行導(dǎo)出,質(zhì)量較差,生成的文件大小較小。
??????????????? WdExportRange wdExportAllDocument = WdExportRange.wdExportAllDocument;//導(dǎo)出全部?jī)?nèi)容(枚舉)
??????????????? int from = 0;//起始頁碼
??????????????? int to = 0;//結(jié)束頁碼
??????????????? WdExportItemwdExportDocumentContent = WdExportItem.wdExportDocumentContent;//指定導(dǎo)出過程中是否只包含文本或包含文本的標(biāo)記.1.wdExportDocumentContent:導(dǎo)出文件沒有標(biāo)記,2.導(dǎo)出文件有標(biāo)記
??????????????? bool includeDocProps = true;//指定是否包含新導(dǎo)出的文件在文檔屬性
??????????????? bool keepIRM = true;//
??????????????? WdExportCreateBookmarkswdExportCreateWordBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;//1.wdExportCreateNoBookmarks:不要在導(dǎo)出文件中創(chuàng)建書簽,2.wdExportCreateHeadingBookmarks:標(biāo)題和文本框?qū)С龅奈募袆?chuàng)建一個(gè)書簽,3.wdExportCreateWordBookmarks每個(gè)字的書簽,其中包括除包含頁眉和頁腳中的所有書簽導(dǎo)出的文件中創(chuàng)建一個(gè)書簽。
??????????????? bool docStructureTags = true;
??????????????? bool bitmapMissingFonts = true;
??????????????? bool UseISO19005_1 = false;//生成的文檔是否符合 ISO 19005-1 (PDF/A)
??????????????? document = applicationClass.Documents.Open(refinputfileName, ref missing, ref missing, refmissing, ref missing, ref missing, ref missing, refmissing, ref missing, ref missing, ref missing, refmissing, ref missing, ref missing, ref missing, refmissing);
??????????????? if (document != null)
??????????????? {
??????????????????? document.ExportAsFixedFormat(outputFileName, exportFormat, openAfterExport, wdExportOptimizeForPrint, wdExportAllDocument, from, to, wdExportDocumentContent, includeDocProps, keepIRM, wdExportCreateWordBookmarks, docStructureTags, bitmapMissingFonts, UseISO19005_1, ref missing);
??????????????? }
??????????????? result = true;
??????????? }
??????????? catch
??????????? {
??????????????? result = false;
??????????? }
??????????? finally
??????????? {
??????????????? if (document != null)
??????????????? {
??????????????????? document.Close(ref missing, refmissing, ref missing);
??????????????????? document = null;
??????????????? }
??????????????? if (applicationClass != null)
??????????????? {
???? ???????????????applicationClass.Quit(ref missing, ref missing, ref missing);
??????????????????? applicationClass = null;
??????????????? }
??????????? }
??????????? return result;
??????? }
調(diào)用該方法:
CommonCls.ConvertPdf.WordToPdf(sourcefilepath, targetfilepath);
(2)利用Microsoft.Office.Interop.PowerPoint實(shí)現(xiàn)ppt轉(zhuǎn)換pdf.
首先安裝office 2010或其他更高版本。
添加引用Microsoft.Office.Interop. PowerPoint:
?
?
?
?
?
?
(3)利用Microsoft.Office.Interop.*組件實(shí)現(xiàn)轉(zhuǎn)換pdf的常見問題和解決方法。
?? 未加載錯(cuò)誤
引用錯(cuò)誤
調(diào)試正常,發(fā)布到部署服務(wù)器時(shí)無法轉(zhuǎn)換
?
?