Retrieve the number of slides

  • OpenXML SDK
  • Spire.Presentation
  • Download Sample Code

class Program
    {
        static void Main(string[] args)
        {
            string DEMOPATH = @"..\..\Documents\Myppt14.pptx";
            // Retrieve the number of slides, excluding the hidden slides.
            Console.WriteLine(RetrieveNumberOfSlides(DEMOPATH, false));
            // Retrieve the number of slides, including the hidden slides.
            Console.WriteLine(RetrieveNumberOfSlides(DEMOPATH));
            Console.ReadKey();
        }
        public static int RetrieveNumberOfSlides(string fileName, bool includeHidden = true)
        {
            int slidesCount = 0;

            using (PresentationDocument doc = PresentationDocument.Open(fileName, false))
            {
                // Get the presentation part of the document.
                PresentationPart presentationPart = doc.PresentationPart;
                if (presentationPart != null)
                {
                    if (includeHidden)
                    {
                        slidesCount = presentationPart.SlideParts.Count();
                    }
                    else
                    {
                        // Each slide can include a Show property, which if hidden 
                        // will contain the value "0". The Show property may not 
                        // exist, and most likely will not, for non-hidden slides.
                        var slides = presentationPart.SlideParts.Where(
                            (s) => (s.Slide != null) &&
                              ((s.Slide.Show == null) || (s.Slide.Show.HasValue &&
                              s.Slide.Show.Value)));
                        slidesCount = slides.Count();
                    }
                }
            }
            return slidesCount;
        }

    }

class Program
    {
        static void Main(string[] args)
        {
            string DEMOPATH = @"..\..\Documents\Myppt14.pptx";
            Console.WriteLine(RetrieveNumberOfSlides(DEMOPATH));
            Console.ReadKey();
        }
        public static int RetrieveNumberOfSlides(string fileName)
        {
            //Instantiate a PresentationEx object that represents a PPTX file
            using (Presentation pres = new Presentation(fileName, FileFormat.Pptx2010))
            {

                return pres.Slides.Count;
            }
        }
    }