/// <summary> /// PPT转XPS /// </summary> /// <param name="filename">PPT文件位置</param> /// <returns>xps文件位置</returns> private string ConvertPPtToXps(string filename) { var app = new Microsoft.Office.Interop.PowerPoint.ApplicationClass(); //app.Visible = Microsoft.Office.Core.MsoTriState.msoFalse; try { var p = app.Presentations.Open(filename, MsoTriState.msoTrue); var xpsFile = filename.Substring(0, filename.LastIndexOf('.')) + ".xps"; p.SaveAs(xpsFile, PpSaveAsFileType.ppSaveAsXPS); p.Close(); p = null; app.Quit(); return(GetFileName(xpsFile)); } catch (Exception) { return(""); } finally { ClearProcesses("POWERPNT"); GC.Collect(); GC.WaitForPendingFinalizers(); } }
/// <summary> /// 把PowerPoing文件转换成PDF格式文件 /// </summary> /// <param name="sourcePath">源文件路径</param> /// <param name="targetPath">目标文件路径</param> /// <returns>true=转换成功</returns> public static bool PPTConvertToPDF(string sourcePath, string targetPath) { bool result; PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF; object missing = Type.Missing; PowerPoint.ApplicationClass application = null; PowerPoint.Presentation persentation = null; try { application = new PowerPoint.ApplicationClass(); persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse); persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue); result = true; } catch (Exception ex) { result = false; } finally { if (persentation != null) { persentation.Close(); persentation = null; } if (application != null) { application.Quit(); application = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } return(result); }
/// <summary> /// PowerPoint格式转换(默认转成PDF) /// </summary> /// <param name="sourcePath">源路径</param> /// <param name="targetPath">目标路径</param> /// <param name="targetFileType">转换类型</param> /// <returns></returns> public static bool PowerPointConvert(string sourcePath, string targetPath, PpSaveAsFileType targetFileType = PpSaveAsFileType.ppSaveAsPDF) { Microsoft.Office.Interop.PowerPoint.ApplicationClass pptApplication = null; Presentation pptPersentation = null; try { pptApplication = new Microsoft.Office.Interop.PowerPoint.ApplicationClass(); pptPersentation = pptApplication.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse); pptPersentation.SaveAs(targetPath, targetFileType, MsoTriState.msoTrue); } catch (Exception e) { Console.WriteLine(e.Message); throw; } finally { if (pptPersentation != null) { pptPersentation.Close(); } if (pptApplication != null) { int k; var t = new IntPtr(pptApplication.HWND); GetWindowThreadProcessId(t, out k); var p = System.Diagnostics.Process.GetProcessById(k); pptApplication.Quit(); p.Kill(); } GC.Collect(); GC.WaitForPendingFinalizers(); } return(true); }
/// <summary> /// 把PowerPoing文件转换成PDF格式文件 /// </summary> /// <param name="sourcePath">源文件路径</param> /// <param name="targetPath">目标文件路径</param> /// <returns>true=转换成功</returns> public static bool PPTConvertToPDF(string sourcePath, string targetPath) { bool result; PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF; object missing = Type.Missing; PowerPoint.ApplicationClass application = null; PowerPoint.Presentation persentation = null; try { application = new PowerPoint.ApplicationClass(); persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse); persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue); result = true; } catch (Exception ex) { result = false; } finally { if (persentation != null) { persentation.Close(); persentation = null; } if (application != null) { application.Quit(); application = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } return result; }