SoFunction
Updated on 2025-03-07

How to operate PowerPoint in C#

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)
      {
        ();
      }
    }
    /// &lt;summary&gt;
    /// Automatically play PPT documents.    /// &lt;/summary&gt;
    /// <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 &lt; 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 ( &gt;= 1) (playTime * 100);
      ();
      ();
    }
    /// &lt;summary&gt;
    /// PPT next page.    /// &lt;/summary&gt;
    public void NextSlide()
    {
      if ( != null)
        ();
    }
    /// &lt;summary&gt;
    ///PPT Previous page.    /// &lt;/summary&gt;
    public void PreviousSlide()
    {
      if ( != null)
        ();
    }
    /// &lt;summary&gt;
    /// Perform image insertion operation on the current PPT page.    /// &lt;/summary&gt;
    /// <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&lt;PPTOBJ&gt; 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;
    }
    /// &lt;summary&gt;
    /// Calculate the offset parameters on the InkCanvas artboard and the parameters of the picture displayed on PPT.    /// Used when loading pictures with PPT    /// &lt;/summary&gt;
    private void SlideParams()
    {
      double slideWidth = ;
      double slideHeight = ;
      double inkCanWidth = ;//;
      double inkCanHeight = ;// ;
      if ((slideWidth / slideHeight) &gt; (inkCanWidth / inkCanHeight))
      {
         = inkCanHeight / slideHeight;
         = 0;
         = (inkCanHeight - slideHeight * ) / 2;
      }
      else
      {
         = inkCanHeight / slideHeight;
         = 0;
         = (inkCanWidth - slideWidth * ) / 2;
      }
    }
    /// &lt;summary&gt;
    /// Close the PPT document.    /// &lt;/summary&gt;
    public void PPTClose()
    {
      //Equipment the PPT program.      if ( != null)
      {
        //Do not use it if you exit the program.        //objSSWs = ;
        //if ( &gt;= 1)
        //{
          if (("Do you save the modified handwriting?", "hint", ) == )
            ();
        //}
        //();
      }
      if ( != null)
        ();
      ();
    }
    #endregion
  }
}

I hope this article will be helpful to everyone's C# programming.