一、首先添加應用:COM裏面的Micsosoft Office 12.0 Object Library(VS2013基本都有14.0或者15.0 有的話同樣的添加,由於個人沒有隻有12.0)安全
:app
二、添加程序集(擴展)裏的引用:記住你前面的Micsosoft Office 12.0 Object Library 版本是多少的就選多少的沒有就本身網上下載或者聯繫我給你,我這裏是作例子;iview
如今能夠看到是這樣的工具
三、若是生成解決方案會出問題就點擊Microsoft.Office.Interop.Word/PowerPoint點屬性把嵌入互操做類型改成false學習
四、在程序代碼中添加引用:ui
五、寫ppt,word轉化爲pdf的方法(這裏特別說明一下sourcePath是須要轉換的原文件,targetPath是轉換後的路徑及名稱,因此在轉換後的路徑裏面提早放一個pdf文件讓word,ppt轉換pdf的時候去替換它)this
//// <summary> /// 把Word文件轉換成pdf文件2 /// </summary> /// <param name="sourcePath">須要轉換的文件路徑和文件名稱</param> /// <param name="targetPath">轉換完成後的文件的路徑和文件名名稱</param> /// <returns>成功返回true,失敗返回false</returns> public bool WordToPdf(object sourcePath, string targetPath) { bool result = false; WdExportFormat wdExportFormatPDF = WdExportFormat.wdExportFormatPDF; object missing = Type.Missing; Microsoft.Office.Interop.Word.ApplicationClass applicationClass = null; Microsoft.Office.Interop.Word.Document document = null; try { applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass(); document = applicationClass.Documents.Open(ref sourcePath, 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(targetPath, wdExportFormatPDF, false, WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportAllDocument, 0, 0, WdExportItem.wdExportDocumentContent, true, true, WdExportCreateBookmarks.wdExportCreateWordBookmarks, true, true, false, 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; } //// <summary> /// 把ppt文件轉換成pdf文件2 /// </summary> /// <param name="sourcePath">須要轉換的文件路徑和文件名稱</param> /// <param name="targetPath">轉換完成後的文件的路徑和文件名名稱</param> /// <returns>成功返回true,失敗返回false</returns> public static bool PPTConvertToPDF(string sourcePath, string targetPath) { bool result; PowerPoint.PpSaveAsFileType ppSaveAsFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF;//轉換成pdf object missing = Type.Missing; Microsoft.Office.Interop.PowerPoint.ApplicationClass application = null; PowerPoint.Presentation persentation = null; try { application = new Microsoft.Office.Interop.PowerPoint.ApplicationClass(); persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse); if (persentation != null) { persentation.SaveAs(targetPath, ppSaveAsFileType, MsoTriState.msoTrue); } result = true; } catch { result = false; } finally { if (persentation != null) { persentation.Close(); persentation = null; } if (application != null) { application.Quit(); application = null; } } return result; }
六、轉化成pdf後複製這裏打開pdf文件的代碼spa
/// <summary> /// 打開pdf文件方法 /// </summary> /// <param name="p"></param> /// <param name="inFilePath">文件路徑及文件名</param> public static void Priview(System.Web.UI.Page p, string inFilePath) { p.Response.ContentType = "Application/pdf"; string fileName = inFilePath.Substring(inFilePath.LastIndexOf('\\') + 1); p.Response.AddHeader("content-disposition", "filename=" + fileName); p.Response.WriteFile(inFilePath); p.Response.End(); }
七、調用方法實現預覽功能(我實現的是在線預覽ppt,word而且第一次打開須要轉化pdf,之後打開的文件若是已經轉換過了直接調用)調試
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { SerialID = GetQueryString("SerialID", "10001");//獲取文件主鍵 ShowContent();//預覽的方法 } } public void ShowContent() { string AttachMent2pdf = Server.MapPath(@"\DownLoadAttachment\" + string.Format("{0}.pdf", SerialID));//判斷此文件是否之前打開過 if (File.Exists(AttachMent2pdf)==true) { Priview(this, Server.MapPath(@"\DownLoadAttachment\" + string.Format("{0}.pdf", SerialID)));//此文件打開過找到之前的文件直接打開 return; } else//若是第一次打開 { if (Directory.Exists(Server.MapPath("DownLoadAttachment")) == false)//判斷存放轉換後的文件夾是否存在 { Directory.CreateDirectory(Server.MapPath(@"\DownLoadAttachment\"));//不存在文件夾就建立文件夾 } CopyFile();//複製文件下面有方法(我只想作一次轉換後之後打開不轉換,因此每次轉化的時候都要複製一個fdf文件來準備被替換) string _Ext = System.IO.Path.GetExtension(GetPptOrWordStarPath(SerialID));//獲取擴展名 if (_Ext == ".doc" || _Ext == ".docx")//判斷是ppt仍是word { WordToPdf(GetPptOrWordStarPath(SerialID), Server.MapPath(@"\DownLoadAttachment\" + string.Format("{0}.pdf",SerialID)));//word轉化pdf的方法,上面有特別提醒注意路徑 } if (_Ext == ".ppt") { PPTConvertToPDF(GetPptOrWordStarPath(SerialID), Server.MapPath(@"\DownLoadAttachment\" + string.Format("{0}.pdf", SerialID))); } } Priview(this, Server.MapPath(@"\DownLoadAttachment\" + string.Format("{0}.pdf", SerialID))); }
public void CopyFile() { string sourceFile = Server.MapPath(@"\SourceFile\123456.pdf"); string objectFile = Server.MapPath(@"\DownLoadAttachment\" + string.Format("{0}.pdf", SerialID)); if (System.IO.File.Exists(sourceFile)) { System.IO.File.Copy(sourceFile, objectFile, true); } }
八、轉化過程當中若是報錯:通常都是調試的時候看獲得catch捕捉到異常,我記得是啥啥啥返回錯誤的。(或者預覽出來的pdf是你之前的pdf內容沒有轉換過)code
個人是win7系統打開:控制面板,系統安全,管理工具,而後打開服務,等下還要打開組件
而後開啓動DTC 就是下面的縮寫(用到的多右鍵屬性啓動方式改自動)
打開後繼續回到系統工具打開組件服務:點開組件服務,點開個人電腦,點開Distributes Transaction Coordinator,右鍵本地DTC修改爲如圖的樣子
這樣通常的就能夠了,Micsosoft Office 12.0 Object Library只有12.0的那麼久不要引用上面15.0或者14.0的Word。有須要的我給你 。或者本身上網下載dll文件版本要同樣。關於Excel,其餘格式的是同樣的轉換,具體網上有不少方法,我整了好幾天終於搞懂,我也是新手遇到了就弄出來,之後用到再來看看。大神勿噴,一塊兒學習不懂的Q我:3011 9459