C#操作PowerPoint的辦法。本站提示廣大學習愛好者:(C#操作PowerPoint的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#操作PowerPoint的辦法正文
本文實例講述了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#法式設計有所贊助。