Example #1
0
        /// <summary>
        /// Creates the PowerPoint Application Class and sets the default web options
        /// </summary>
        private void CreatePowerPointAppClass()
        {
            if (m_pptApp == null)
            {
                m_pptApp = new Application();
            }

            m_pptApp.DefaultWebOptions.Encoding = Office.MsoEncoding.msoEncodingUTF8;
            m_pptApp.DefaultWebOptions.AlwaysSaveInDefaultEncoding = MS_TRUE;
        }
Example #2
0
        /// <summary>
        /// 转换PPT到PDF
        /// </summary>
        /// <param name="wpsFilename">原始文件</param>
        /// <param name="pdfFilename">转换后pdf文件</param>
        private void ConvertPptToPdf()
        {
            PrintLog("Start Convert PPT Office file To PDF");
            PowerPoint.Application pps        = new PowerPoint.Application();
            Presentation           curPPTFile = null;

            try
            {
                // Is NULL
                if (m_strOfficeName == null || m_strPDFName == null)
                {
                    m_nConvertStatus = EErrorType.OTP_FILENAME_EMPTY;
                }
                else
                {
                    // To PDF
                    //忽略警告提示 此处无法设置,原因不清楚!
                    //pps.Visible = PowerPoint.MsoTriState.msoFalse;
                    pps.DisplayAlerts = PowerPoint.PpAlertLevel.ppAlertsNone;
                    curPPTFile        = pps.Presentations.Open(m_strOfficeName, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
                    m_nTotalPage      = curPPTFile.Slides.Count;
                    PrintLog("ConvertStatus:" + (int)ConvertStatus.START + " " + 0 + " " + m_nTotalPage);
                    for (int i = 1; i <= m_nTotalPage; i++)
                    {
                        Slide slide = curPPTFile.Slides[i];
                        //int nWidth = (int)(slide.Background.Width * (int)m_dQuality);
                        //int nHeight = (int)(slide.Background.Height * (int)m_dQuality);
                        string strJpgName = m_strOutDir + "\\" + Path.GetFileNameWithoutExtension(m_strOfficeName) + "_" + m_nTotalPage + "_" + i + ".jpg";
                        slide.Export(strJpgName, "jpeg" /*, nWidth, nHeight*/);
                        PrintLog("ConvertStatus:" + (int)ConvertStatus.ING + " " + (i).ToString() + " " + m_nTotalPage);
                    }
                    QuitWPP(ref pps, ref curPPTFile);
                    // To JPG
                    //ConvertPDF2ImageO2S(m_strPDFName);
                }
            }
            catch (Exception ex)
            {
                PrintLog(ex.Message.ToString());
                m_nConvertStatus = EErrorType.OTP_EXCEPTION_FAILED;
            }
            finally
            {
                QuitWPP(ref pps, ref curPPTFile);
            }
        }
Example #3
0
        /// <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();
                    }
                }
            }
        }
Example #4
0
        /// <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;
        }
Example #5
0
 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;
     }
 }
Example #6
0
        /// <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;
            }
        }