http://www.cnblogs.com/hyruur/archive/2011/02/14/1954118.html
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using OFFICECORE = Microsoft.Office.Core; 6 using POWERPOINT = Microsoft.Office.Interop.PowerPoint; 7 using System.Windows; 8 using System.Collections; 9 using System.Windows.Controls; 10 namespace PPTDraw.PPTOperate 11 { 12 /// <summary> 13 /// PPT文档操作实现类. 14 /// </summary> 15 public class OperatePPT 16 { 17 #region=========基本的参数信息======= 18 POWERPOINT.Application objApp = null; 19 POWERPOINT.Presentation objPresSet = null; 20 POWERPOINT.SlideShowWindows objSSWs; 21 POWERPOINT.SlideShowTransition objSST; 22 POWERPOINT.SlideShowSettings objSSS; 23 POWERPOINT.SlideRange objSldRng; 24 bool bAssistantOn; 25 double pixperPoint = 0; 26 double offsetx = 0; 27 double offsety = 0; 28 #endregion 29 #region===========操作方法============== 30 /// <summary> 31 /// 打开PPT文档并播放显示。 32 /// </summary> 33 /// <param name="filePath">PPT文件路径</param> 34 public void PPTOpen(string filePath) 35 { 36 //防止连续打开多个PPT程序. 37 if (this.objApp != null) { return; } 38 try 39 { 40 objApp = new POWERPOINT.Application(); 41 //以非只读方式打开,方便操作结束后保存. 42 objPresSet = objApp.Presentations.Open(filePath, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse); 43 //Prevent Office Assistant from displaying alert messages: 44 bAssistantOn = objApp.Assistant.On; 45 objApp.Assistant.On = false; 46 objSSS = this.objPresSet.SlideShowSettings; 47 objSSS.Run(); 48 } 49 catch (Exception ex) 50 { 51 this.objApp.Quit(); 52 } 53 } 54 /// <summary> 55 /// 自动播放PPT文档. 56 /// </summary> 57 /// <param name="filePath">PPTy文件路径.</param> 58 /// <param name="playTime">翻页的时间间隔.【以秒为单位】</param> 59 public void PPTAuto(string filePath, int playTime) 60 { 61 //防止连续打开多个PPT程序. 62 if (this.objApp != null) { return; } 63 objApp = new POWERPOINT.Application(); 64 objPresSet = objApp.Presentations.Open(filePath, OFFICECORE.MsoTriState.msoCTrue, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse); 65 // 自动播放的代码(开始) 66 int Slides = objPresSet.Slides.Count; 67 int[] SlideIdx = new int[Slides]; 68 for (int i = 0; i < Slides; i++) { SlideIdx[i] = i + 1; }; 69 objSldRng = objPresSet.Slides.Range(SlideIdx); 70 objSST = objSldRng.SlideShowTransition; 71 //设置翻页的时间. 72 objSST.AdvanceOnTime = OFFICECORE.MsoTriState.msoCTrue; 73 objSST.AdvanceTime = playTime; 74 //翻页时的特效! 75 objSST.EntryEffect = POWERPOINT.PpEntryEffect.ppEffectCircleOut; 76 //Prevent Office Assistant from displaying alert messages: 77 bAssistantOn = objApp.Assistant.On; 78 objApp.Assistant.On = false; 79 //Run the Slide show from slides 1 thru 3. 80 objSSS = objPresSet.SlideShowSettings; 81 objSSS.StartingSlide = 1; 82 objSSS.EndingSlide = Slides; 83 objSSS.Run(); 84 //Wait for the slide show to end. 85 objSSWs = objApp.SlideShowWindows; 86 while (objSSWs.Count >= 1) System.Threading.Thread.Sleep(playTime * 100); 87 this.objPresSet.Close(); 88 this.objApp.Quit(); 89 } 90 /// <summary> 91 /// PPT下一页。 92 /// </summary> 93 public void NextSlide() 94 { 95 if (this.objApp != null) 96 this.objPresSet.SlideShowWindow.View.Next(); 97 } 98 /// <summary> 99 /// PPT上一页。100 /// </summary>101 public void PreviousSlide()102 {103 if (this.objApp != null)104 this.objPresSet.SlideShowWindow.View.Previous();105 }106 /// <summary>107 /// 对当前的PPT页面进行图片插入操作。108 /// </summary>109 /// <param name="alImage">图片对象信息数组</param>110 /// <param name="offsetx">插入图片距离左边长度</param>111 /// <param name="pixperPoint">距离比例值</param>112 /// <returns>是否添加成功!</returns>113 public bool InsertToSlide(List<PPTOBJ> listObj)114 {115 bool InsertSlide = false;116 if (this.objPresSet != null)117 {118 this.SlideParams();119 int slipeint = objPresSet.SlideShowWindow.View.CurrentShowPosition;120 foreach (PPTOBJ myobj in listObj)121 {122 objPresSet.Slides[slipeint].Shapes.AddPicture(123 myobj.Path, //图片路径124 OFFICECORE.MsoTriState.msoFalse,125 OFFICECORE.MsoTriState.msoTrue,126 (float)((myobj.X - this.offsetx) / this.pixperPoint), //插入图片距离左边长度127 (float)(myobj.Y / this.pixperPoint), //插入图片距离顶部高度128 (float)(myobj.Width / this.pixperPoint), //插入图片的宽度129 (float)(myobj.Height / this.pixperPoint) //插入图片的高度130 );131 }132 InsertSlide = true;133 }134 return InsertSlide;135 }136 /// <summary>137 /// 计算InkCanvas画板上的偏移参数,与PPT上显示图片的参数。138 /// 用于PPT加载图片时使用139 /// </summary>140 private void SlideParams()141 {142 double slideWidth = this.objPresSet.PageSetup.SlideWidth;143 double slideHeight = this.objPresSet.PageSetup.SlideHeight;144 double inkCanWidth = SystemParameters.PrimaryScreenWidth;//inkCan.ActualWidth;145 double inkCanHeight = SystemParameters.PrimaryScreenHeight;//inkCan.ActualHeight ;146 if ((slideWidth / slideHeight) > (inkCanWidth / inkCanHeight))147 {148 this.pixperPoint = inkCanHeight / slideHeight;149 this.offsetx = 0;150 this.offsety = (inkCanHeight - slideHeight * this.pixperPoint) / 2;151 }152 else153 {154 this.pixperPoint = inkCanHeight / slideHeight;155 this.offsety = 0;156 this.offsetx = (inkCanWidth - slideWidth * this.pixperPoint) / 2;157 }158 }159 /// <summary>160 /// 关闭PPT文档。161 /// </summary>162 public void PPTClose()163 {164 //装备PPT程序。165 if (this.objPresSet != null)166 {167 //判断是否退出程序,可以不使用。168 //objSSWs = objApp.SlideShowWindows;169 //if (objSSWs.Count >= 1)170 //{171 if (MessageBox.Show("是否保存修改的笔迹!", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)172 this.objPresSet.Save();173 //}174 //this.objPresSet.Close();175 }176 if (this.objApp != null)177 this.objApp.Quit();178 GC.Collect();179 }180 #endregion181 }182 }