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

{

/// <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

}

}

本文出自 “我的笔记” 博客,请务必保留此出处http://9891814.blog.51cto.com/9881814/1627298

c#如何操作ppt的播放 【转】的更多相关文章

  1. winfrom 操作PPT

    ///winfrom 操作PPT using System; using System.Collections.Generic; using System.Linq; using System.Tex ...

  2. JAVA通过COM接口操作PPT

    一. 背景说明 在Eclipse环境下,开发JAVA代码操作PPT,支持对PPT模板的修改.包括修改文本标签.图表.表格.满足大多数软件生成PPT报告的要求,即先收工创建好模板,在程序中修改模板数据. ...

  3. C++通过COM接口操作PPT

    一. 背景 在VS环境下,开发C++代码操作PPT,支持对PPT模板的修改.包括修改文本标签.图表.表格.满足大多数软件生成PPT报告的要求,先手工创建好PPT模板,在程序中修改模板数据. 二. 开发 ...

  4. java poi 操作ppt

    java poi 操作ppt 可以参考: https://www.w3cschool.cn/apache_poi_ppt/apache_poi_ppt_installation.html http:/ ...

  5. 实现ppt幻灯片播放倒计时

    需求:为控制会议时间,采取ppt幻灯片播放倒计时的办法,倒计时5分钟. 分析:用EnumWindows枚举窗口,发现PPT窗口类名有三种:PP12FrameClass.MS-SDIb.screenCl ...

  6. Jacob操作ppt

    前几天使用Apache 的POI操作ppt,后来发现转成的图片出现乱码,而且处理了之后,还会有遗留 因此决定换一种处理方式 Jacob 是 JAVA-COM Bridge的缩写,是一个中间件,能够提供 ...

  7. poi 操作 PPT,针对 PPTX--图表篇

    poi 操作 PPT,针对 PPTX--图表篇 目录 poi 操作 PPT,针对 PPTX--图表篇 1.读取 PPT 模板 2.替换标题 4.替换图表数据 接下来对 ppt 内的图表进行操作,替换图 ...

  8. C# 操作PPt,去掉文本框的边框

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using OFFICECO ...

  9. (转)C#操作PPT

    原文地址:http://blog.163.com/loveyingchun_1314/blog/static/2382425120124312627530/ 引用Microsoft.Office.Co ...

随机推荐

  1. 在 Windows 上进行 Laravel Homestead 安装、配置及测试

    软件环境:在 Windows 7 64位 上基于 VirtualBox 5.2.12 + Vagrant 2.1.1 使用 Laravel Homestead. 1.准备 先下载VirtualBox- ...

  2. ref:Java安全之反序列化漏洞分析(简单-朴实)

    ref:https://mp.weixin.qq.com/s?__biz=MzIzMzgxOTQ5NA==&mid=2247484200&idx=1&sn=8f3201f44e ...

  3. 转:linux关闭防火墙iptables

    ref:https://jingyan.baidu.com/article/066074d64f433ec3c21cb000.html Linux系统下面自带了防火墙iptables,iptables ...

  4. Python并发编程-信号量

    信号量保证同一资源同一时间只能有限定的进程去访问 一套资源,同一时间,只能被n个人访问 某一段代码,同一时间,只能被n个进程执行 from multiprocessing import Process ...

  5. springMVC接收参数 xml/json

    springMVC参数接收 作为web层框架,可以接受复杂的类型,且很简单 1.接收字符串可以直接写参数类型,参数名称,跟前端传过来的name值一样即可 如果不一样也可以,通过@RequestPara ...

  6. 洛谷——P2683 小岛

    P2683 小岛 题目背景 西伯利亚北部的寒地,坐落着由 N 个小岛组成的岛屿群,我们把这些小岛依次编号为 1 到 N . 题目描述 起初,岛屿之间没有任何的航线.后来随着交通的发展,逐渐出现了一些连 ...

  7. 选择器(UIPickerView)

    Apple提供了两种形式的选择器:日期选择器(UIDatePicker)与自定义选择器(UIPickerView). 当用户使用日期选择器选定一个时间后,调用datePickerID.date可以直接 ...

  8. set集合玩法、三目运算

    set是无序的,无法用下标获取值 创建set二种方式 1.第一种 s1=set()   #创建一个空的set,看下面就知道为什么要这么创建一个空的集合 2.第二种 s2={11,22,33,44} # ...

  9. HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]

    题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点 ...

  10. 【BZOJ 3106】 3106: [cqoi2013]棋盘游戏 (对抗搜索)

    3106: [cqoi2013]棋盘游戏 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 544  Solved: 233 Description 一个 ...