? ? ? 利用office自帶的COM類型庫組件實現轉換Pdf功能。只要安裝了office的服務器上都可以調用,不需要額外的第三方組件,功能也更加豐富和強大,幾乎可以不受限制的操作office所有類型文件。缺點是部署問題多,發布到客戶服務器進行調試的話問題很多。禁忌:1,開發的時候調用,不同office版本的COM組件,比如Microsoft.Office.Interop.Word是v14,那ppt、excel等組件都要統一版本,不然問題很多;2,部署的服務器上只能安裝一個版本的office,比如開發時調用的Office 2010,那部署的服務器就只能裝 office 2010,建議不要混裝各種版本來匹配組件型號,最終會導致哪個都不能用;3,常見的故障問題和解決方法附錄在該節末尾。
(1)利用Microsoft.Office.Interop.Word實現word轉換pdf.
首先安裝office 2010或其他更高版本。
添加引用Microsoft.Office.Interop. Word:
C#代碼中添加引用:
using System.Text;
?
using Microsoft.Office.Interop.Word;
using WdExportFormat = Microsoft.Office.Interop.Word.WdExportFormat;
?
創建WordToPdf方法:
/// <summary>
??????? /// 把Word文件轉換成pdf文件
??????? /// </summary>
??????? /// <param name="sourcePath">需要轉換的文件路徑和文件名稱</param>
??????? /// <param name="targetPath">轉換完成后的文件的路徑和文件名名稱</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;//轉換格式1.wdExportFormatPDF轉換成pdf格式 2.wdExportFormatXPS轉換成xps格式
??????????? object missing = Type.Missing;
??????????? Microsoft.Office.Interop.Word.ApplicationClassapplicationClass = null;
??????????? Document document = null;
??????????? try
??????????? {
??????????????? applicationClass = newMicrosoft.Office.Interop.Word.ApplicationClass();
??????????????? object inputfileName = sourcePath;//需要轉格式的文件路徑
??????????????? string outputFileName = targetPath;//轉換完成后PDF或XPS文件的路徑和文件名名稱
??????????????? WdExportFormat exportFormat = wdExportFormatPDF;//導出文件所使用的格式
??????????????? bool openAfterExport = false;//轉換完成后是否打開
??????????????? WdExportOptimizeForwdExportOptimizeForPrint = WdExportOptimizeFor.wdExportOptimizeForPrint;//導出方式1.wdExportOptimizeForPrint針對打印進行導出,質量較高,生成的文件大小較大。2.wdExportOptimizeForOnScreen 針對屏幕顯示進行導出,質量較差,生成的文件大小較小。
??????????????? WdExportRange wdExportAllDocument = WdExportRange.wdExportAllDocument;//導出全部內容(枚舉)
??????????????? int from = 0;//起始頁碼
??????????????? int to = 0;//結束頁碼
??????????????? WdExportItemwdExportDocumentContent = WdExportItem.wdExportDocumentContent;//指定導出過程中是否只包含文本或包含文本的標記.1.wdExportDocumentContent:導出文件沒有標記,2.導出文件有標記
??????????????? bool includeDocProps = true;//指定是否包含新導出的文件在文檔屬性
??????????????? bool keepIRM = true;//
??????????????? WdExportCreateBookmarkswdExportCreateWordBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;//1.wdExportCreateNoBookmarks:不要在導出文件中創建書簽,2.wdExportCreateHeadingBookmarks:標題和文本框導出的文件中創建一個書簽,3.wdExportCreateWordBookmarks每個字的書簽,其中包括除包含頁眉和頁腳中的所有書簽導出的文件中創建一個書簽。
??????????????? 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;
??????? }
調用該方法:
CommonCls.ConvertPdf.WordToPdf(sourcefilepath, targetfilepath);
(2)利用Microsoft.Office.Interop.PowerPoint實現ppt轉換pdf.
首先安裝office 2010或其他更高版本。
添加引用Microsoft.Office.Interop. PowerPoint:
?
?
?
?
?
?
(3)利用Microsoft.Office.Interop.*組件實現轉換pdf的常見問題和解決方法。
?? 未加載錯誤
引用錯誤
調試正常,發布到部署服務器時無法轉換
?
?