1、首先添加應用:COM里面的Micsosoft Office 12.0 Object Library(VS2013基本都有14.0或者15.0 有的話一樣的添加,因為我的沒有只有12.0)
:
2、添加程序集(擴展)里的引用:記住你前面的Micsosoft Office 12.0 Object Library 版本是多少的就選多少的沒有就自己網上下載或者聯系我給你,我這里是做例子;
現在可以看到是這樣的
3、如果生成解決方案會出問題就點擊Microsoft.Office.Interop.Word/PowerPoint點屬性把嵌入互操作類型改為false
4、在程序代碼中添加引用:
5、寫ppt,word轉化為pdf的方法(這里特別說明一下sourcePath是需要轉換的原文件,targetPath是轉換后的路徑及名稱,所以在轉換后的路徑里面提前放一個pdf文件讓word,ppt轉換pdf的時候去替換它)
//// <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; }
6、轉化成pdf后復制這里打開pdf文件的代碼
/// <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(); }
7、調用方法實現預覽功能(我實現的是在線預覽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); } }
8、轉化過程中如果報錯:一般都是調試的時候看得到catch捕捉到異常,我記得是啥啥啥返回錯誤的。(或者預覽出來的pdf是你以前的pdf內容沒有轉換過)
我的是win7系統打開:控制面板,系統安全,管理工具,然后打開服務,等下還要打開組件
然后開啟動DTC 就是下面的縮寫(用到的多右鍵屬性啟動方式改自動)
打開后繼續回到系統工具打開組件服務:點開組件服務,點開我的電腦,點開Distributes Transaction Coordinator,右鍵本地DTC修改成如圖的樣子
這樣一般的就可以了,Micsosoft Office 12.0 Object Library只有12.0的那么久不要引用上面15.0或者14.0的Word。有需要的我給你 。或者自己上網下載dll文件版本要一樣。關於Excel,其他格式的是一樣的轉換,具體網上有很多方法,我整了好幾天終於搞懂,我也是新手遇到了就弄出來,以后用到再來看看。大神勿噴,一起學習不懂的Q我:3011 9459