This article describes the method of C# operating PowerPoint. Share it for your reference. The details are as follows:
Here, the basic code of C# operates PowerPoint, including opening a ppt file, reading a slide page, inserting a slide, auto-playing, etc.
using System; using ; using ; using ; using OFFICECORE = ; using POWERPOINT = ; using ; using ; using ; namespace { /// <summary> /// PPT document operation implementation class. /// </summary> public class OperatePPT { #region=========== Basic parameter information======== objApp = null; objPresSet = null; objSSWs; objSST; objSSS; objSldRng; bool bAssistantOn; double pixperPoint = 0; double offsetx = 0; double offsety = 0; #endregion #region================ Operation method================== /// <summary> /// Open the PPT document and play it. /// </summary> /// <param name="filePath">PPT file path</param> public void PPTOpen(string filePath) { //Prevent multiple PPT programs from being opened continuously. if ( != null) { return; } try { objApp = new (); //Open in a non-read-only manner, which is convenient for saving after the operation is finished. objPresSet = (filePath, , , ); //Prevent Office Assistant from displaying alert messages: bAssistantOn = ; = false; objSSS = ; (); } catch (Exception ex) { (); } } /// <summary> /// Automatically play PPT documents. /// </summary> /// <param name="filePath">PPTy file path.</param> /// <param name="playTime">The time interval for page turn. [in seconds]</param> public void PPTAuto(string filePath, int playTime) { //Prevent multiple PPT programs from being opened continuously. if ( != null) { return; } objApp = new (); objPresSet = (filePath, , , ); // Automatic playback code (start) int Slides = ; int[] SlideIdx = new int[Slides]; for (int i = 0; i < Slides; i++) { SlideIdx[i] = i + 1; }; objSldRng = (SlideIdx); objSST = ; //Set the page turn time. = ; = playTime; //Special effects when turning pages! = ; //Prevent Office Assistant from displaying alert messages: bAssistantOn = ; = false; //Run the Slide show from slides 1 thru 3. objSSS = ; = 1; = Slides; (); //Wait for the slide show to end. objSSWs = ; while ( >= 1) (playTime * 100); (); (); } /// <summary> /// PPT next page. /// </summary> public void NextSlide() { if ( != null) (); } /// <summary> ///PPT Previous page. /// </summary> public void PreviousSlide() { if ( != null) (); } /// <summary> /// Perform image insertion operation on the current PPT page. /// </summary> /// <param name="alImage">Image object information array</param> /// <param name="offsetx">Insert the length to the left of the picture</param> /// <param name="pixperPoint">Distance ratio value</param> /// <returns> Whether it was successfully added! </returns> public bool InsertToSlide(List<PPTOBJ> listObj) { bool InsertSlide = false; if ( != null) { (); int slipeint = ; foreach (PPTOBJ myobj in listObj) { [slipeint].( , //Picture path , , (float)(( - ) / ), //Insert the image to the left length (float)( / ), //Insert the image from the top height (float)( / ), //Insert the width of the picture (float)( / ) //Insert the height of the picture ); } InsertSlide = true; } return InsertSlide; } /// <summary> /// Calculate the offset parameters on the InkCanvas artboard and the parameters of the picture displayed on PPT. /// Used when loading pictures with PPT /// </summary> private void SlideParams() { double slideWidth = ; double slideHeight = ; double inkCanWidth = ;//; double inkCanHeight = ;// ; if ((slideWidth / slideHeight) > (inkCanWidth / inkCanHeight)) { = inkCanHeight / slideHeight; = 0; = (inkCanHeight - slideHeight * ) / 2; } else { = inkCanHeight / slideHeight; = 0; = (inkCanWidth - slideWidth * ) / 2; } } /// <summary> /// Close the PPT document. /// </summary> public void PPTClose() { //Equipment the PPT program. if ( != null) { //Do not use it if you exit the program. //objSSWs = ; //if ( >= 1) //{ if (("Do you save the modified handwriting?", "hint", ) == ) (); //} //(); } if ( != null) (); (); } #endregion } }
I hope this article will be helpful to everyone's C# programming.