(轉)C#操做PPT

     原文地址:http://blog.163.com/loveyingchun_1314/blog/static/2382425120124312627530/數組

 

       引用Microsoft.Office.Core時須要在com選項中添加Microsoft Office 12.0 Object Library(個人Office版本是2007),若是沒有這個須要修改Office添加開發支持(開始-程序-Office更改)ide

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using POWERPOINT =Microsoft.Office.Interop.PowerPoint;
using OFFICECORE = Microsoft.Office.Core;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Forms.ComponentModel;


namespace TestPPT
{
    public class clsOPPPT
    {
        POWERPOINT.Application objApp = null;
        POWERPOINT.Presentation objPresSet = null;
        POWERPOINT.SlideShowWindows objSSWs;
        POWERPOINT.SlideShowTransition objSST;
        POWERPOINT.SlideShowSettings objSSS;
        POWERPOINT.SlideRange objSldRng;

        bool bOpenState = false;
        bool bAssistantOn;
        double pixperPoint = 0;
        double offsetx = 0;
        double offsety = 0;

        public bool BOpenState
        {
            set { bOpenState = value;}
            get { return bOpenState;}
        }

         /// <summary>
        /// 打開PPT文檔並播放顯示。
        /// </summary>
        /// <param name="filePath">PPT文件路徑</param>
        public void PPTOpen(string filePath)
        {
            //防止連續打開多個PPT程序.
            if (this.objApp != null) 
            { 
                return; 
            }
            try
            {
                objApp = new POWERPOINT.Application();
                //以非只讀方式打開,方便操做結束後保存.
                objPresSet = objApp.Presentations.Open(filePath, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse);
                bAssistantOn = objApp.Assistant.On;//Prevent Office Assistant from displaying alert messages:
                objApp.Assistant.On = false;

                objSSS = this.objPresSet.SlideShowSettings;
                objSSS.Run();
                BOpenState = true;
            }
            catch (Exception ex)
            {
                this.objApp.Quit();
            }
        }

          /// <summary>
        /// 自動播放PPT文檔.
        /// </summary>
        /// <param name="filePath">PPTy文件路徑.</param>
        /// <param name="playTime">翻頁的時間間隔.【以秒爲單位】</param>
        public void PPTAuto(string filePath, int playTime)
        {
            //防止連續打開多個PPT程序.
            if (this.objApp != null) { return; }

            objApp = new POWERPOINT.Application();
            objPresSet = objApp.Presentations.Open(filePath, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse);

            // 自動播放的代碼(開始)
            int Slides = objPresSet.Slides.Count;
            int[] SlideIdx = new int[Slides];
            for (int i = 0; i < Slides; i++) { SlideIdx[i] = i + 1; };
            objSldRng = objPresSet.Slides.Range(SlideIdx);
            objSST = objSldRng.SlideShowTransition;
            //設置翻頁的時間.
            objSST.AdvanceOnTime = OFFICECORE.MsoTriState.msoCTrue;
            objSST.AdvanceTime = playTime;
            //翻頁時的特效!
            objSST.EntryEffect = POWERPOINT.PpEntryEffect.ppEffectCircleOut;

            //Prevent Office Assistant from displaying alert messages:
            bAssistantOn = objApp.Assistant.On;
            objApp.Assistant.On = false;

            //Run the Slide show from slides 1 thru 3.
            objSSS = objPresSet.SlideShowSettings;
            objSSS.StartingSlide = 1;
            objSSS.EndingSlide = Slides;
            objSSS.Run();

            //Wait for the slide show to end.
            objSSWs = objApp.SlideShowWindows;
            while (objSSWs.Count >= 1) System.Threading.Thread.Sleep(playTime * 100);

            this.objPresSet.Close();
            this.objApp.Quit();
        }
        /// <summary>
        /// PPT下一頁。
        /// </summary>
        public void NextSlide()
        {
            if (this.objApp != null)
                this.objPresSet.SlideShowWindow.View.Next();
        }
        /// <summary>
        /// PPT上一頁。
        /// </summary>
        public void PreviousSlide()
        {
            if (this.objApp != null)
                this.objPresSet.SlideShowWindow.View.Previous();
        }
        /// <summary>
        /// 對當前的PPT頁面進行圖片插入操做。
        /// </summary>
        /// <param name="alImage">圖片對象信息數組</param>
        /// <param name="offsetx">插入圖片距離左邊長度</param>
        /// <param name="pixperPoint">距離比例值</param>
        /// <returns>是否添加成功!</returns>
        public bool InsertToSlide(List<PPTOBJ> listObj)
        {
            bool InsertSlide = false;
            if (this.objPresSet != null)
            {
                this.SlideParams();
                //objPresSet.Slides[slipeint].Shapes.Count;
                objPresSet.SlideShowWindow.View.Last();
                int slipeint = objPresSet.SlideShowWindow.View.CurrentShowPosition;

                Microsoft.Office.Interop.PowerPoint.CustomLayout pCustomLayout = this.objPresSet.Slides[1].CustomLayout;
                foreach (PPTOBJ myobj in listObj)
                {
                    objPresSet.Slides[slipeint].Shapes.AddPicture(
                         myobj.Path,           //圖片路徑
                         OFFICECORE.MsoTriState.msoFalse,
                         OFFICECORE.MsoTriState.msoTrue,
                         (float)((myobj.X - this.offsetx) / this.pixperPoint),      //插入圖片距離左邊長度
                         (float)(myobj.Y / this.pixperPoint),                       //插入圖片距離頂部高度
                         (float)(myobj.Width / this.pixperPoint),                   //插入圖片的寬度
                         (float)(myobj.Height / this.pixperPoint)                   //插入圖片的高度
                      );

                    objPresSet.Slides.AddSlide(slipeint+1, pCustomLayout);
                    objPresSet.SlideShowWindow.View.Next();
                    slipeint = objPresSet.SlideShowWindow.View.CurrentShowPosition;
                }
                InsertSlide = true;
            }
            return InsertSlide;
        }
        /// <summary>
        /// 計算InkCanvas畫板上的偏移參數,與PPT上顯示圖片的參數。
        /// 用於PPT加載圖片時使用
        /// </summary>
        private void SlideParams()
        {
            double slideWidth = this.objPresSet.PageSetup.SlideWidth;
            double slideHeight = this.objPresSet.PageSetup.SlideHeight;
            double inkCanWidth = SystemParameters.PrimaryScreenWidth;//inkCan.ActualWidth;
            double inkCanHeight = SystemParameters.PrimaryScreenHeight;//inkCan.ActualHeight ;

            if ((slideWidth / slideHeight) > (inkCanWidth / inkCanHeight))
            {
                this.pixperPoint = inkCanHeight / slideHeight;
                this.offsetx = 0;
                this.offsety = (inkCanHeight - slideHeight * this.pixperPoint) / 2;
            }
            else
            {
                this.pixperPoint = inkCanHeight / slideHeight;
                this.offsety = 0;
                this.offsetx = (inkCanWidth - slideWidth * this.pixperPoint) / 2;
            }
        }
        /// <summary>
        /// 關閉PPT文檔。
        /// </summary>
        public void PPTClose()
        {
            //裝備PPT程序。
            if (this.objPresSet != null)
            {
                //判斷是否退出程序,能夠不使用。
                //objSSWs = objApp.SlideShowWindows;
                //if (objSSWs.Count >= 1)
                //{
                //if (MessageBox.Show("是否保存修改的筆跡!", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    this.objPresSet.Save();
                }
                //}
                //this.objPresSet.Close();
            }
            if (this.objApp != null)
                this.objApp.Quit();

            GC.Collect();
        }
    }

    public class PPTOBJ
    {
        string path = "";
        float x;
        float y;
        float width;
        float height;

        public float X
        {
            set { x = value;}
            get{return x;}
        }
        public float Y
        {
            set { y = value; }
            get { return y; }
        }
        public float Width
        {
            set { width = value; }
            get { return width; }
        }
        public float Height
        {
            set { height = value; }
            get { return height; }
        }
        public string Path
        {
            set { path = value;}
            get { return path; }
        }
    }
}

     附ui

          https://msdn.microsoft.com/zh-cn/library/ff746586.aspxthis

相關文章
相關標籤/搜索