C#操作PowerPoint的方法
本文实例讲述了C#操作PowerPoint的方法。分享给大家供大家参考。具体如下:
这里C#操作PowerPoint的基本代码,包括打开ppt文件、读取幻灯页,插入幻灯片,自动播放等
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingOFFICECORE=Microsoft.Office.Core;
usingPOWERPOINT=Microsoft.Office.Interop.PowerPoint;
usingSystem.Windows;
usingSystem.Collections;
usingSystem.Windows.Controls;
namespacePPTDraw.PPTOperate
{
///<summary>
///PPT文档操作实现类.
///</summary>
publicclassOperatePPT
{
#region=========基本的参数信息=======
POWERPOINT.ApplicationobjApp=null;
POWERPOINT.PresentationobjPresSet=null;
POWERPOINT.SlideShowWindowsobjSSWs;
POWERPOINT.SlideShowTransitionobjSST;
POWERPOINT.SlideShowSettingsobjSSS;
POWERPOINT.SlideRangeobjSldRng;
boolbAssistantOn;
doublepixperPoint=0;
doubleoffsetx=0;
doubleoffsety=0;
#endregion
#region===========操作方法==============
///<summary>
///打开PPT文档并播放显示。
///</summary>
///<paramname="filePath">PPT文件路径</param>
publicvoidPPTOpen(stringfilePath)
{
//防止连续打开多个PPT程序.
if(this.objApp!=null){return;}
try
{
objApp=newPOWERPOINT.Application();
//以非只读方式打开,方便操作结束后保存.
objPresSet=objApp.Presentations.Open(filePath,OFFICECORE.MsoTriState.msoFalse,OFFICECORE.MsoTriState.msoFalse,OFFICECORE.MsoTriState.msoFalse);
//PreventOfficeAssistantfromdisplayingalertmessages:
bAssistantOn=objApp.Assistant.On;
objApp.Assistant.On=false;
objSSS=this.objPresSet.SlideShowSettings;
objSSS.Run();
}
catch(Exceptionex)
{
this.objApp.Quit();
}
}
///<summary>
///自动播放PPT文档.
///</summary>
///<paramname="filePath">PPTy文件路径.</param>
///<paramname="playTime">翻页的时间间隔.【以秒为单位】</param>
publicvoidPPTAuto(stringfilePath,intplayTime)
{
//防止连续打开多个PPT程序.
if(this.objApp!=null){return;}
objApp=newPOWERPOINT.Application();
objPresSet=objApp.Presentations.Open(filePath,OFFICECORE.MsoTriState.msoCTrue,OFFICECORE.MsoTriState.msoFalse,OFFICECORE.MsoTriState.msoFalse);
//自动播放的代码(开始)
intSlides=objPresSet.Slides.Count;
int[]SlideIdx=newint[Slides];
for(inti=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;
//PreventOfficeAssistantfromdisplayingalertmessages:
bAssistantOn=objApp.Assistant.On;
objApp.Assistant.On=false;
//RuntheSlideshowfromslides1thru3.
objSSS=objPresSet.SlideShowSettings;
objSSS.StartingSlide=1;
objSSS.EndingSlide=Slides;
objSSS.Run();
//Waitfortheslideshowtoend.
objSSWs=objApp.SlideShowWindows;
while(objSSWs.Count>=1)System.Threading.Thread.Sleep(playTime*100);
this.objPresSet.Close();
this.objApp.Quit();
}
///<summary>
///PPT下一页。
///</summary>
publicvoidNextSlide()
{
if(this.objApp!=null)
this.objPresSet.SlideShowWindow.View.Next();
}
///<summary>
///PPT上一页。
///</summary>
publicvoidPreviousSlide()
{
if(this.objApp!=null)
this.objPresSet.SlideShowWindow.View.Previous();
}
///<summary>
///对当前的PPT页面进行图片插入操作。
///</summary>
///<paramname="alImage">图片对象信息数组</param>
///<paramname="offsetx">插入图片距离左边长度</param>
///<paramname="pixperPoint">距离比例值</param>
///<returns>是否添加成功!</returns>
publicboolInsertToSlide(List<PPTOBJ>listObj)
{
boolInsertSlide=false;
if(this.objPresSet!=null)
{
this.SlideParams();
intslipeint=objPresSet.SlideShowWindow.View.CurrentShowPosition;
foreach(PPTOBJmyobjinlistObj)
{
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;
}
returnInsertSlide;
}
///<summary>
///计算InkCanvas画板上的偏移参数,与PPT上显示图片的参数。
///用于PPT加载图片时使用
///</summary>
privatevoidSlideParams()
{
doubleslideWidth=this.objPresSet.PageSetup.SlideWidth;
doubleslideHeight=this.objPresSet.PageSetup.SlideHeight;
doubleinkCanWidth=SystemParameters.PrimaryScreenWidth;//inkCan.ActualWidth;
doubleinkCanHeight=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>
publicvoidPPTClose()
{
//装备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#程序设计有所帮助。