Get the titles of all the slides in a presentation

  • OpenXML SDK
  • Spire.Presentation
  • Download Sample Code

class Program
    {
        static void Main(string[] args)
        {
            foreach (string s in GetSlideTitles(@"..\..\Documents\Myppt10.pptx"))
                Console.WriteLine(s);
        }
        // Get a list of the titles of all the slides in the presentation.
        public static IList GetSlideTitles(string presentationFile)
        {
            // Open the presentation as read-only.
            using (PresentationDocument presentationDocument =
                PresentationDocument.Open(presentationFile, false))
            {
                return GetSlideTitles(presentationDocument);
            }
        }

        // Get a list of the titles of all the slides in the presentation.
        public static IList GetSlideTitles(PresentationDocument presentationDocument)
        {
            if (presentationDocument == null)
            {
                throw new ArgumentNullException("presentationDocument");
            }

            // Get a PresentationPart object from the PresentationDocument object.
            PresentationPart presentationPart = presentationDocument.PresentationPart;

            if (presentationPart != null &&
                presentationPart.Presentation != null)
            {
                // Get a Presentation object from the PresentationPart object.
                Presentation presentation = presentationPart.Presentation;

                if (presentation.SlideIdList != null)
                {
                    List titlesList = new List();

                    // Get the title of each slide in the slide order.
                    foreach (var slideId in presentation.SlideIdList.Elements())
                    {
                        SlidePart slidePart = presentationPart.GetPartById(slideId.RelationshipId) as SlidePart;

                        // Get the slide title.
                        string title = GetSlideTitle(slidePart);

                        // An empty title can also be added.
                        titlesList.Add(title);
                    }

                    return titlesList;
                }

            }

            return null;
        }

        // Get the title string of the slide.
        public static string GetSlideTitle(SlidePart slidePart)
        {
            if (slidePart == null)
            {
                throw new ArgumentNullException("presentationDocument");
            }

            // Declare a paragraph separator.
            string paragraphSeparator = null;

            if (slidePart.Slide != null)
            {
                // Find all the title shapes.
                var shapes = from shape in slidePart.Slide.Descendants()
                             where IsTitleShape(shape)
                             select shape;

                StringBuilder paragraphText = new StringBuilder();

                foreach (var shape in shapes)
                {
                    // Get the text in each paragraph in this shape.
                    foreach (var paragraph in shape.TextBody.Descendants())
                    {
                        // Add a line break.
                        paragraphText.Append(paragraphSeparator);

                        foreach (var text in paragraph.Descendants())
                        {
                            paragraphText.Append(text.Text);
                        }

                        paragraphSeparator = "\n";
                    }
                }

                return paragraphText.ToString();
            }

            return string.Empty;
        }

        // Determines whether the shape is a title shape.
        private static bool IsTitleShape(Shape shape)
        {
            var placeholderShape = shape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.GetFirstChild();
            if (placeholderShape != null && placeholderShape.Type != null && placeholderShape.Type.HasValue)
            {
                switch ((PlaceholderValues)placeholderShape.Type)
                {
                    // Any title shape.
                    case PlaceholderValues.Title:

                    // A centered title.
                    case PlaceholderValues.CenteredTitle:
                        return true;

                    default:
                        return false;
                }
            }
            return false;
        }

    }

class Program
    {
        static void Main(string[] args)
        {
            foreach (string s in GetSlideTitles(@"..\..\Documents\Myppt10.pptx"))
                Console.WriteLine(s);
            Console.ReadKey();
        }
        // Get a list of the titles of all the slides in the presentation.
        public static IList GetSlideTitles(string presentationFile)
        {
            // Create a new linked list of strings.
            List texts = new List();

            //Instantiate presentation class 
            using (Presentation pres = new Presentation(presentationFile, FileFormat.Pptx2010))
            {

                //foreach all the slides
                foreach (ISlide sld in pres.Slides)
                {

                    //Iterate through shapes to find the text
                    foreach (Shape shp in sld.Shapes)
                    {
                        //get title text
                        if (IsTitleShape(shp))
                        {
                            texts.Add(((IAutoShape)shp).TextFrame.Text);
                        }
                    }
                }
            }
            return texts;
        }
        // Determines whether the shape is a title shape.
        private static bool IsTitleShape(Shape shape)
        {
            switch (shape.Placeholder.Type)
            {
                case PlaceholderType.Title:
                case PlaceholderType.CenteredTitle:
                    return true;
                default:
                    return false;
            }
        }
    }