Пример #1
0
        public static void exportImg2(string path)
        {
            Log.Info("PPT.exportImg  path=" + path);
            string name = Path.GetFileName(path);

            string dir = Application.StartupPath + "\\" + DateTime.Now.ToString("yyyyMMdd") + "\\";

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            if (name.IndexOf("ppt") < 0 && name.IndexOf("PPT") < 0)
            {
                return;
            }

            string imgpath2 = Application.StartupPath + "\\" + DateTime.Now.ToString("yyyyMMdd") + "\\" + name + "_" + 2 + ".jpg";
            string imgpath3 = Application.StartupPath + "\\" + DateTime.Now.ToString("yyyyMMdd") + "\\" + name + "_" + 3 + ".jpg";

            if (File.Exists(imgpath2) && File.Exists(imgpath3))
            {
                return;
            }

            POWERPOINT.Application  app = null;
            POWERPOINT.Presentation pre = null;
            try
            {
                app = new POWERPOINT.Application();

                #region check ppt or pptx
                try
                {
                    pre = app.Presentations.Open(path, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse);
                }
                catch (Exception e1)
                {
                    Log.Info("Presentations.Open exception. " + e1 + ", " + path);
                }
                #endregion


                POWERPOINT.Slides slides = pre.Slides;
                int pageTotal            = pre.Slides.Count;

                IEnumerator e = slides.GetEnumerator();
                int         i = 0;

                while (e.MoveNext())
                {
                    i++;
                    PPT.Slide slide   = (PPT.Slide)e.Current;
                    string    imgpath = Application.StartupPath + "\\" + DateTime.Now.ToString("yyyyMMdd") + "\\" + name + "_" + i + ".jpg";
                    slide.Export(imgpath, "jpg", 400, 300);
                }
            }
            catch (Exception e1)
            {
                Log.Info("exportImg1 exception: " + e1.Message);
            }
            finally
            {
                try
                {
                    if (pre != null)
                    {
                        pre.Close();
                        pre = null;
                    }
                }
                catch (Exception e2)
                {
                    Log.Info("exportImg2 exception: " + e2.Message);
                }
                app = null;
            }

            Log.Info("PPT.exportImg over.  name=" + name);
        }
Пример #2
0
        public static void exportImg(string path)
        {
            Log.Info("PPT.exportImg  path=" + path);
            string name = Path.GetFileName(path);

            string dir = Application.StartupPath + "\\" + DateTime.Now.ToString("yyyyMMdd") + "\\";

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            if (name.IndexOf("ppt") < 0 && name.IndexOf("PPT") < 0)
            {
                return;
            }



            if (g_fileImgStatus.Contains(name))
            {
                g_fileImgStatus.Remove(name);
            }
            g_fileImgStatus.Add(name, 0);//正在导入

            POWERPOINT.Application  app = null;
            POWERPOINT.Presentation pre = null;
            try
            {
                app = new POWERPOINT.Application();

                #region check ppt or pptx
                try{
                    pre = app.Presentations.Open(path, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse);
                }catch (Exception e1) {
                    Log.Info("Presentations.Open exception. " + e1 + "," + path);
                    if (g_fileImgStatus.Contains(name))
                    {
                        g_fileImgStatus.Remove(name);
                        g_fileImgStatus.Add(name, 1);
                    }
                    Form1.DeleteFile(name);
                    MessageBox.Show("打开" + path + "失败,请检查该文件!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    app = null;
                }
                #endregion

                Log.Info("Presentations.Open succeed. " + name);
                POWERPOINT.Slides slides = pre.Slides;
                int pageTotal            = pre.Slides.Count;

                bool bOver = true;
                for (int k = 1; k <= pageTotal; k++)
                {
                    string imgpath1 = Application.StartupPath + "\\" + DateTime.Now.ToString("yyyyMMdd") + "\\" + name + "_" + k + ".jpg";
                    if (!File.Exists(imgpath1))
                    {
                        bOver = false;
                    }
                }

                if (bOver)
                {
                    try
                    {
                        if (pre != null)
                        {
                            pre.Close();
                            pre = null;
                        }
                    }
                    catch (Exception e2)
                    {
                        Log.Info("exportImg2 exception: " + e2.Message);
                    }
                    app = null;

                    return;
                }



                IEnumerator e = slides.GetEnumerator();
                int         i = 0;

                while (e.MoveNext())
                {
                    i++;
                    PPT.Slide slide   = (PPT.Slide)e.Current;
                    string    imgpath = Application.StartupPath + "\\" + DateTime.Now.ToString("yyyyMMdd") + "\\" + name + "_" + i + ".jpg";
                    slide.Export(imgpath, "jpg", 400, 300);
                }
            }
            catch (Exception e1)
            {
                Log.Info("exportImg1 exception: " + e1.Message);
            }
            finally
            {
                if (g_fileImgStatus.Contains(name))
                {
                    g_fileImgStatus.Remove(name);
                    g_fileImgStatus.Add(name, 1);
                }

                try
                {
                    if (pre != null)
                    {
                        pre.Close();
                        pre = null;
                    }
                }
                catch (Exception e2)
                {
                    Log.Info("exportImg2 exception: " + e2.Message);
                }
                app = null;
            }

            Log.Info("PPT.exportImg over.  name=" + name);
        }