Retrieve the number of slides

2016-01-19 06:16:56 Written by  support iceblue
Rate this item
(0 votes)

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;
        }

    }