C#操作PowerPoint的方法

所属分类: 软件编程 / C#教程 阅读数: 78
收藏 0 赞 0 分享

本文实例讲述了C#操作PowerPoint的方法。分享给大家供大家参考。具体如下:

这里C#操作PowerPoint的基本代码,包括打开ppt文件、读取幻灯页,插入幻灯片,自动播放等

using System;
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
{
  /// <summary>
  /// PPT文档操作实现类.
  /// </summary>
  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===========操作方法==============
    /// <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);
        //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();
      }
    }
    /// <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.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();
    }
    /// <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();
        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;
    }
    /// <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();
    }
    #endregion
  }
}

希望本文所述对大家的C#程序设计有所帮助。

更多精彩内容其他人还在看

c#开发word批量转pdf源码分享

已经安装有Office环境,借助一些简单的代码即可实现批量Word转PDF,看下面的实例源码吧
收藏 0 赞 0 分享

c# xml API操作的小例子

这篇文章主要介绍了c# xml API操作的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

c#唯一值渲染实例代码

这篇文章主要介绍了c#唯一值渲染实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

淘宝IP地址库采集器c#代码

这篇文章主要介绍了淘宝IP地址库采集器c#代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C#在后台运行操作(BackgroundWorker用法)示例分享

BackgroundWorker类允许在单独的专用线程上运行操作。如果需要能进行响应的用户界面,而且面临与这类操作相关的长时间延迟,则可以使用BackgroundWorker类方便地解决问题,下面看示例
收藏 0 赞 0 分享

c#文本加密程序代码示例

这是一个加密软件,但只限于文本加密,加了窗口控件的滑动效果,详细看下面的代码
收藏 0 赞 0 分享

c#生成站点地图(SiteMapPath)文件示例程序

这篇文章主要介绍了c#生成站点地图(SiteMapPath)文件的示例,大家参考使用
收藏 0 赞 0 分享

C# 键盘Enter键取代Tab键实现代码

这篇文章主要介绍了C# 键盘Enter键取代Tab键实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm导出Excel方法介绍

在.NET应用中,导出Excel是很常见的需求,导出Excel报表大致有以下三种方式:Office PIA,文件流和NPOI开源库,本文只介绍前两种方式
收藏 0 赞 0 分享

C#串口通信程序实例详解

在.NET平台下创建C#串口通信程序,.NET 2.0提供了串口通信的功能,其命名空间是System.IO.Ports,创建C#串口通信程序的具体实现是如何的呢?让我们开始吧
收藏 0 赞 0 分享
查看更多