/// <summary> /// PPT转PDF /// </summary> /// <param name="wpsFilename">文件名</param> /// <param name="pdfFilename"></param> public void PptToPdf(string wpsFilename, string pdfFilename = null) { PowerPoint.Application pps = new PowerPoint.Application(); try { if (wpsFilename == null) { throw new ArgumentNullException("wpsFilename"); } //保存路径为空时,保存在原始文件目录 if (pdfFilename == null) { pdfFilename = Path.ChangeExtension(wpsFilename, "pdf"); } //忽略警告提示 此处无法设置,原因不清楚! //pps.DisplayAlerts = WPP.WpAlertLevel.wpAlertsAll; //应用程序不可见 // pps. // pps.Visible=MsoTriState. pps.Visible = MsoTriState.msoTrue; //pps.DisplayAlerts = PpAlertLevel.ppAlertsNone; pps.Presentations.Open(wpsFilename); pps.ActivePresentation.ExportAsFixedFormat(pdfFilename, PpFixedFormatType.ppFixedFormatTypePDF); pps.ActivePresentation.Close(); } catch (Exception ex) { } finally { //无论是否成功,都退出 if (pps != null) { pps.Quit(); //强制关闭所有wpp进程 System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("POWERPNT"); foreach (System.Diagnostics.Process prtemp in process) { prtemp.Kill(); } } } }
/// <summary> /// Quits the PowerPoint Application. /// </summary> public void QuitPowerPoint() { if (m_pptApp == null) { return; } // closing the presentation would not close the application, so we // close it explicitly if there are no more presentations. Since // only one instance of PowerPoint can be running at a time, we need // to make sure that there are no more presentations before quiting the // application if (m_pptApp.Presentations != null && m_pptApp.Presentations.Count == 0) { try { m_pptApp.Quit(); } catch (Exception e) { Logger.LogError("Failed to quit powerpoint", e); } } m_pptApp = null; }
private void QuitWPP(ref PowerPoint.Application pps, ref Presentation curPPTFile) { try { if (curPPTFile != null) { //curPPTFile.Save(); curPPTFile.Close(); } //无论是否成功,都退出 if (pps != null) { PrintLog("Convert PPT file To PDF end"); if (pps.Presentations.Count == 0) { pps.Quit(); int calcID = 0, calcTD = 0; calcTD = GetWindowThreadProcessId((IntPtr)pps.HWND, out calcID); System.Diagnostics.Process process = System.Diagnostics.Process.GetProcessById(calcID); if (process != null) { process.Kill(); } } } } catch (Exception ex) { PrintLog(ex.Message.ToString()); //m_nConvertStatus = EErrorType.OTP_EXCEPTION_FAILED; } finally { //pps = null; curPPTFile = null; } }
/// <summary> /// Convert Presentation Document. /// </summary> /// <param name="strPath"></param> /// <param name="savePath"></param> /// <param name="msg"></param> /// <returns></returns> private bool ConvertPresentationDocument(string strPath, string savePath,ref DocumentSharing msg) { try { PowerPoint.Application ppApp; PowerPoint.PpSaveAsFileType format; format= PowerPoint.PpSaveAsFileType.ppSaveAsJPG;//.ppSaveAsJPG; try { ppApp= new PowerPoint.Application(); ppApp.Presentations.Open(strPath,Microsoft.Office.Core.MsoTriState.msoCTrue,Microsoft.Office.Core.MsoTriState.msoCTrue,Microsoft.Office.Core.MsoTriState.msoFalse); } catch(Exception ee) { Console.Write("Unable to convert PowerPoint document " + ee.Message); return false; } try { ppApp.Presentations.Item(1).SaveAs(savePath,format,Microsoft.Office.Core.MsoTriState.msoCTrue); ppApp.Presentations.Item(1).Close(); //ConvertBMPToJPG(savePath); ppApp.Quit(); System.Threading.Thread.Sleep(500); string[] files = Directory.GetFiles(savePath,"*.jpg"); if(files != null) { msg.TotalPages = files.Length; //Console.WriteLine("No of Files in presentation ::::"+files.Length.ToString()); } else { msg.CurrentPage = 1; msg.TotalPages =1; } //Thread.Sleep(500); return true; } catch(Exception ee) { string errorText = "Unable to save specified Presentation. Access is denied. "+ ee.Message; Console.Write("Unable to convert Powerpoint Document " + errorText); return false; } } catch(Exception ee) { Console.Write("Unable to Convert PowerPoint Document. " + ee.Message); return false; } }