? ? ? 業(yè)務(wù)中,經(jīng)常需要將office 文件上傳并通過網(wǎng)頁進行在線閱讀,一種最普遍的方法就是轉(zhuǎn)換office文件到pdf格式,以pdf文件方式進行在線預(yù)覽。
? ? ? ?但將office 文件轉(zhuǎn)換為Pdf的方法很多,各有利弊。
方法一:利用office自帶的COM類型庫組件實現(xiàn)轉(zhuǎn)換Pdf功能。只要安裝了office的服務(wù)器上都可以調(diào)用,不需要額外的第三方組件,功能也更加豐富和強大,幾乎可以不受限制的操作office所有類型文件。缺點是部署問題多,發(fā)布到客戶服務(wù)器進行調(diào)試的話問題很多。禁忌:1,開發(fā)的時候調(diào)用,不同office版本的COM組件,比如Microsoft.Office.Interop.Word是v14,那ppt、excel等組件都要統(tǒng)一版本,不然問題很多;2,部署的服務(wù)器上只能安裝一個版本的office,比如開發(fā)時調(diào)用的Office 2010,那部署的服務(wù)器就只能裝 office 2010,建議不要混裝各種版本來匹配組件型號,最終會導(dǎo)致哪個都不能用;3,常見的故障問題和解決方法附錄在該節(jié)末尾。
(1)利用Microsoft.Office.Interop.Word實現(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(string sourcePath, string targetPath)
??????? {
???????????
??????????? bool result = false;
??????????? Microsoft.Office.Interop.Word.WdExportFormat wdExportFormatPDF = 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.ApplicationClass applicationClass = null;
??????????? Document document = null;
??????????? try
??????????? {
??????????????? applicationClass = new Microsoft.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)換完成后是否打開
??????????????? WdExportOptimizeFor wdExportOptimizeForPrint = WdExportOptimizeFor.wdExportOptimizeForPrint;//導(dǎo)出方式1.wdExportOptimizeForPrint針對打印進行導(dǎo)出,質(zhì)量較高,生成的文件大小較大。2.wdExportOptimizeForOnScreen 針對屏幕顯示進行導(dǎo)出,質(zhì)量較差,生成的文件大小較小。
??????????????? WdExportRange wdExportAllDocument = WdExportRange.wdExportAllDocument;//導(dǎo)出全部內(nèi)容(枚舉)
??????????????? int from = 0;//起始頁碼
??????????????? int to = 0;//結(jié)束頁碼
??????????????? WdExportItem wdExportDocumentContent = 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;//
??????????????? WdExportCreateBookmarks wdExportCreateWordBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;//1.wdExportCreateNoBookmarks:不要在導(dǎo)出文件中創(chuàng)建書簽,2.wdExportCreateHeadingBookmarks:標(biāo)題和文本框?qū)С龅奈募袆?chuàng)建一個書簽,3.wdExportCreateWordBookmarks每個字的書簽,其中包括除包含頁眉和頁腳中的所有書簽導(dǎo)出的文件中創(chuàng)建一個書簽。
??????????????? bool docStructureTags = true;
??????????????? bool bitmapMissingFonts = true;
??????????????? bool UseISO19005_1 = false;//生成的文檔是否符合 ISO 19005-1 (PDF/A)
??????????????? document = applicationClass.Documents.Open(ref inputfileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
??????????????? 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, ref missing, 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實現(xiàn)ppt轉(zhuǎn)換pdf.
首先安裝office 2010或其他更高版本。
添加引用Microsoft.Office.Interop. PowerPoint:
(3)利用Microsoft.Office.Interop.*組件實現(xiàn)轉(zhuǎn)換pdf的常見問題和解決方法。
?? 未加載錯誤
引用錯誤
調(diào)試正常,發(fā)布到部署服務(wù)器時無法轉(zhuǎn)換
方法二:利用Aspose類庫將word,ppt文檔轉(zhuǎn)換為Pdf。這個方法不需要在執(zhí)行轉(zhuǎn)換任務(wù)的服務(wù)器上安裝office軟件,也不會引起各種各樣office權(quán)限的奇怪問題;缺點是需要收費,同時與office的后臺耦合功能上還有所不足。
(1)利用Aspose. Words.dll將本地word文檔轉(zhuǎn)化成pdf
將Aspose. Words.dll??拷貝到bin目錄。
添加引用
創(chuàng)建WordToPdf方法:
public static bool WordToPdf(string sourcePath, string targetPath)
????? {
????????? try
????????? {
????????????? //讀取doc文檔
????????????? Document doc = new Document(sourcePath);
????????????? //保存為PDF文件,此處的SaveFormat支持很多種格式,如圖片,epub,rtf 等等
????????????? doc.Save(targetPath, SaveFormat.Pdf);
????????????? return true;
???????? ?}
????????? catch
????????? {
????????????? return false;
????????? }
????? }
在項目中,將文件上傳后進行調(diào)用:
CommonCls.ConvertToPDF.WordToPdf(sourcefilepath, targetfilepath);
(2)利用Aspose.Slides.dll將本地ppt文檔轉(zhuǎn)化成pdf
將Aspose.Slides.dll??拷貝到bin目錄。
添加引用
?文章來源地址http://www.zghlxwxcb.cn/news/detail-401196.html
C#代碼中引用:
- using System.IO;
- using Aspose.Slides;
創(chuàng)建PptToPdf方法:
public static bool PptToPdf(string sourcePath, string targetPath)
????? {
????????? try
????????? {
????????????? Crack();//調(diào)用Crack方法實現(xiàn)Aspose.Slides軟破解
????????????? //實例化ppt文件
????????????? Presentation pres = new Presentation(sourcePath);
????????????? //保存
????????????? pres.Save(targetPath, Aspose.Slides.Export.SaveFormat.Pdf);
????????????? return true;
????????? }
????????? catch
????????? {
????????????? return false;
????????? }
????? }
代碼中的Crack()方法,是用來對Aspose.Slides進行破解。
調(diào)用該方法:
CommonCls.ConvertToPDF. PptToPdf (sourcefilepath, targetfilepath);
方法三:利用Spire類庫實現(xiàn)office文件轉(zhuǎn)換pdf.
(1)利用Microsoft.Office.Interop.Word實現(xiàn)word轉(zhuǎn)換pdf.
待續(xù)....
資源截圖:
下載地址:https://download.csdn.net/download/lanhai96/86744033文章來源:http://www.zghlxwxcb.cn/news/detail-401196.html
?
到了這里,關(guān)于C# 將word/ppt文檔轉(zhuǎn)換為Pdf的三種方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!