C#控制ppt的播放读取打开插入等操作代码

C#控制ppt的代码
包括打开ppt文件、读取幻灯页,插入幻灯片,自动播放等
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OFFICECORE = Microsoft.Office.Core;
using POWERPOINT = Microsoft.Office.Interop.PowerPoint;
using System.windows;
using System.Collections;
using System.windows.Controls;
namespace PPTDraw.PPTOperate
{
  ///
  /// PPT文档操作实现类.
  ///
  public class OperatePPT
  {
    #region=========基本的参数信息=======
    POWERPOINT.Application objApp = null;
    POWERPOINT.Presentation objPresSet = null;
    POWERPOINT.SlideShowwindows objSSWs;
    POWERPOINT.SlideShowTransition objSST;
    POWERPOINT.SlideShowSettings objSSS;
    POWERPOINT.SlideRange objSldRng;
    bool bAssistantOn;
    double pixperPoint = 0;
    double offsetx = 0;
    double offsety = 0;
    #endregion
    #region===========操作方法==============
    ///
    /// 打开PPT文档并播放显示。
    ///
    /// PPT文件路径
    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);
        //Prevent Office Assistant from displaying alert messages:
        bAssistantOn = objApp.Assistant.On;
        objApp.Assistant.On = false;
        objSSS = this.objPresSet.SlideShowSettings;
        objSSS.Run();
      }
      catch (Exception ex)
      {
        this.objApp.Quit();
      }
    }
    ///
    /// 自动播放PPT文档.
    ///
    /// PPTy文件路径.
    /// 翻页的时间间隔.【以秒为单位】
    public void PPTAuto(string filePath, int playTime)
    {
      //防止连续打开多个PPT程序.
      if (this.objApp != null) { return; }
      objApp = new POWERPOINT.Application();
      objPresSet = objApp.Presentations.Open(filePath, OFFICECORE.MsoTriState.msoCTrue, 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();
    }
    ///
    /// PPT下一页。
    ///
    public void NextSlide()
    {
      if (this.objApp != null)
        this.objPresSet.SlideShowwindow.View.Next();
    }
    ///
    /// PPT上一页。
    ///
    public void PreviousSlide()
    {
      if (this.objApp != null)
        this.objPresSet.SlideShowwindow.View.Previous();
    }
    ///
    /// 对当前的PPT页面进行图片插入操作。
    ///
    /// 图片对象信息数组
    /// 插入图片距离左边长度
    /// 距离比例值
    /// 是否添加成功!
    public bool InsertToSlide(List listObj)
    {
      bool InsertSlide = false;
      if (this.objPresSet != null)
      {
        this.SlideParams();
        int slipeint = objPresSet.SlideShowwindow.View.CurrentShowPosition;
        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)  //插入图片的高度
           );
        }
        InsertSlide = true;
      }
      return InsertSlide;
    }
    ///
    /// 计算InkCanvas画板上的偏移参数,与PPT上显示图片的参数。
    /// 用于PPT加载图片时使用
    ///
    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;
      }
    }
    ///
    /// 关闭PPT文档。
    ///
    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();
    }
    #endregion
  }
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30120277/viewspace-1481530/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/30120277/viewspace-1481530/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值