/// <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; }
/// <summary> /// 将ppt文档转换成pdf格式 /// </summary> /// <param name="sourcePath">源文件路径</param> /// <param name="targetPath">目标文件路径</param> /// <param name="targetFileType"></param> /// <returns></returns> public bool PPTConvertPDF(string sourcePath, string targetPath = null) { if (sourcePath == null) { throw new ArgumentNullException("wpsFilename"); } //保存路径为空时,保存在原始文件目录 if (targetPath == null) { targetPath = Path.ChangeExtension(sourcePath, "pdf"); } bool result; object missing = Type.Missing; Microsoft.Office.Interop.PowerPoint.ApplicationClass application = null; Presentation persentation = null; try { application = new Microsoft.Office.Interop.PowerPoint.ApplicationClass(); //persentation = application.Presentations.Open(sourcePath, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse); //persentation.SaveAs(targetPath, PpSaveAsFileType.ppSaveAsPDF, Microsoft.Office.Core.MsoTriState.msoTrue); result = true; } catch { 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); }
public static string ReadPPT(string pptFileName) { string text = string.Empty; PowerPoint.ApplicationClass app = null; PowerPoint.Presentation pp = null; object readOnly = true; object missing = System.Reflection.Missing.Value; object fileName = pptFileName; try { app = new Microsoft.Office.Interop.PowerPoint.ApplicationClass(); pp = app.Presentations.Open(fileName.ToString(), Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse); foreach (PowerPoint.Slide slide in pp.Slides) { foreach (PowerPoint.Shape shape in slide.Shapes) { text += shape.TextFrame.TextRange.Text.Replace("\r", string.Empty).Replace("\n", string.Empty).Replace("\t", string.Empty) + " "; } } } catch { } finally { pp.Close(); pp = null; app.Quit(); app = null; } return(text); }