欧美一区2区三区4区公司二百,国产精品婷婷午夜在线观看,自拍偷拍亚洲精品,国产美女诱惑一区二区

C# 將word/ppt文檔轉換為Pdf的三種方法

? ? ? 利用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的常見問題和解決方法。

?? 未加載錯誤

引用錯誤

調試正常,發布到部署服務器時無法轉換

?

C# 將word/ppt文檔轉換為Pdf的三種方法

?

文章鏈接: http://www.qzkangyuan.com/11578.html

文章標題:C# 將word/ppt文檔轉換為Pdf的三種方法

文章版權:夢飛科技所發布的內容,部分為原創文章,轉載請注明來源,網絡轉載文章如有侵權請聯系我們!

聲明:本站所有文章,如無特殊說明或標注,均為本站原創發布。任何個人或組織,在未征得本站同意時,禁止復制、盜用、采集、發布本站內容到任何網站、書籍等各類媒體平臺。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。

給TA打賞
共{{data.count}}人
人已打賞
建站教程投稿分享

虛擬主機應用場景

2022-10-24 15:28:48

建站教程投稿分享

DNS分離解析技術練習

2022-10-25 16:44:30

0 條回復 A文章作者 M管理員
    暫無討論,說說你的看法吧
?
個人中心
購物車
優惠劵
今日簽到
有新私信 私信列表
搜索
主站蜘蛛池模板: 平度市| 鄂州市| 绥中县| 博罗县| 酒泉市| 赫章县| 清丰县| 东丽区| 会泽县| 永顺县| 长岭县| 铅山县| 上思县| 宁南县| 桂林市| 邳州市| 萍乡市| 政和县| 贵南县| 咸阳市| 安化县| 丹巴县| 大姚县| 漳州市| 玉门市| 兰溪市| 凉城县| 子洲县| 贡山| 兴化市| 兴国县| 卓资县| 江孜县| 桦甸市| 河津市| 上林县| 库车县| 宿迁市| 多伦县| 宁陕县| 清远市|