(转)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更改)
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 = ;
double offsetx = ;
double offsety = ; 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 = ; i < Slides; i++) { SlideIdx[i] = i + ; };
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 = ;
objSSS.EndingSlide = Slides;
objSSS.Run(); //Wait for the slide show to end.
objSSWs = objApp.SlideShowWindows;
while (objSSWs.Count >= ) System.Threading.Thread.Sleep(playTime * ); 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[].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+, 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 = ;
this.offsety = (inkCanHeight - slideHeight * this.pixperPoint) / ;
}
else
{
this.pixperPoint = inkCanHeight / slideHeight;
this.offsety = ;
this.offsetx = (inkCanWidth - slideWidth * this.pixperPoint) / ;
}
}
/// <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; }
}
}
}
附
https://msdn.microsoft.com/zh-cn/library/ff746586.aspx
(转)C#操作PPT的更多相关文章
- JAVA通过COM接口操作PPT
一. 背景说明 在Eclipse环境下,开发JAVA代码操作PPT,支持对PPT模板的修改.包括修改文本标签.图表.表格.满足大多数软件生成PPT报告的要求,即先收工创建好模板,在程序中修改模板数据. ...
- C++通过COM接口操作PPT
一. 背景 在VS环境下,开发C++代码操作PPT,支持对PPT模板的修改.包括修改文本标签.图表.表格.满足大多数软件生成PPT报告的要求,先手工创建好PPT模板,在程序中修改模板数据. 二. 开发 ...
- java poi 操作ppt
java poi 操作ppt 可以参考: https://www.w3cschool.cn/apache_poi_ppt/apache_poi_ppt_installation.html http:/ ...
- winfrom 操作PPT
///winfrom 操作PPT using System; using System.Collections.Generic; using System.Linq; using System.Tex ...
- Jacob操作ppt
前几天使用Apache 的POI操作ppt,后来发现转成的图片出现乱码,而且处理了之后,还会有遗留 因此决定换一种处理方式 Jacob 是 JAVA-COM Bridge的缩写,是一个中间件,能够提供 ...
- poi 操作 PPT,针对 PPTX--图表篇
poi 操作 PPT,针对 PPTX--图表篇 目录 poi 操作 PPT,针对 PPTX--图表篇 1.读取 PPT 模板 2.替换标题 4.替换图表数据 接下来对 ppt 内的图表进行操作,替换图 ...
- C#操作PPT表格
1.激活组件 AxFramerControl改控件的dll自己再网上百度下下载这里不多讲 /// <summary> /// 检测是否注册控件 /// < ...
- C# 操作PPt,去掉文本框的边框
using System; using System.Collections.Generic; using System.Linq; using System.Text; using OFFICECO ...
- Java 利用POI操作PPT
解析PPT文件中的图片 import java.io.File; import java.io.FileOutputStream; import org.apache.poi.hslf.HSLFSli ...
随机推荐
- MYSQL 分组排名
今天遇到一个MYSQL排序的问题,要求按某列进行分组,组内进行排序. 百度一下发现MYSQL不支持row_number(),rank()等函数. 采用的办法如下,我们首先创建一个测试表: --创建表 ...
- 日常维护管理-DBA运维交接清单
序号 交接内容 交接目标与要点 交接物 交接状态 交接开始时间 交接结束时间 负责人 备注 1 人事关系 与开发项目组成员互识,并了解其职责 开发项目组成员清单 2016/2/29 2016/2/29 ...
- [activiti] Activiti 5.18 的Mybatis版本依赖问题
测试activiti 是查询Task时抛出一个异常: org.apache.ibatis.exceptions.PersistenceException: ### Error querying dat ...
- Eclipse的中文字体设置
打开eclipse中文字体很小,简直难以辨认.在网上搜索发现这是由于Eclipse 用的字体是 Consolas,显示中文的时候默认太小了.解决方式有两种:一.把字体设置为Courier New 操 ...
- object-单例设计模式
1.什么是单例? 单例是一种数据共享的方法,其实就是等同于c语言中的全局变量.在整个程序生命周期内,该对象只有一份存在内存中,可以在多个对象之间共享数据 单例其实就相当于一个类只能有一个实 ...
- for循环内 执行$ajax(){}
真是郁闷,在for 循环里添加了ajax异步传输之后,for循环是单线程处理,就是里面执行的是ajax,也不异步处理数据.而是执行完for循环的次数后,一起把ajax的数据处理掉. 解决办法.分开吧! ...
- Koala logoJava EE 应用开发平台 Koala
Koala (考拉) 是一款应用在 Java EE 企业级应用开发领域,用于帮助架构师简化系统设计,降低框架耦合度,提高系统灵活性,提供开发工程师工作效率,降低成本的平台工具. 为什么使用 Koala ...
- perl语言书籍教程推荐
互动出版网计算机频道.为您推荐关于perl语言的书籍教程.包括perl push.perl chomp以及perl python等perl语言内容. perl语言书籍一.<Perl语言编程 第四 ...
- [linux basic 基础]----同步互斥量
互斥量,运行程序元锁住某个对象,使得每次只能有一个线程访问它:为了控制对关键代码的访问,必须在进入这段代码之前锁住一个互斥量,然后在完成操作之后解锁它 :基本函数与用于信号量的函数非常相似#inclu ...
- C#使用ConditionalAttribute特性来实现代码调试
转自:http://www.csharpwin.com/csharpspace/10729r8541.shtml #if/#endif条件编译常用来由同一份源代码生成不同的结果文件,最常见的有debu ...