There can be many types of titles such as centered title, subtitle and title on PowerPoint slides. This article demonstrates how to get such titles from all the slides of a PowerPoint document by using Spire.Presentation and C#.
This is a PowerPoint document which contains a centered title and a subtitle on the first slide and a title on the second slide.
Follow below steps to get the titles:
Step 1: Instantiate a Presentation object and load the PowerPoint document.
Presentation ppt = new Presentation(); ppt.LoadFromFile("test.pptx");
Step 2: Instantiate a list of IShape objects.
List<IShape> shapelist = new List<IShape>();
Step 3: Loop through all sildes in the document and all shapes on each slide, add the shape which placeholder type is title or centered title or subtitle to the list.
foreach (ISlide slide in ppt.Slides) { foreach (IShape shape in slide.Shapes) { if (shape.Placeholder != null) { switch (shape.Placeholder.Type) { case PlaceholderType.Title: shapelist.Add(shape); break; case PlaceholderType.CenteredTitle: shapelist.Add(shape); break; case PlaceholderType.Subtitle: shapelist.Add(shape); break; } } } }
Step 4: Loop through the list and print out the inner text of all shapes in the list.
for (int i = 0; i < shapelist.Count; i++) { IAutoShape shape1 = shapelist[i] as IAutoShape; Console.WriteLine(shape1.TextFrame.Text); }
Output:
Full code:
using System; using System.Collections.Generic; using Spire.Presentation; namespace Get_the_Titles_of_All_Slides { class Program { static void Main(string[] args) { Presentation ppt = new Presentation(); ppt.LoadFromFile("test.pptx"); List<IShape> shapelist = new List<IShape>(); foreach (ISlide slide in ppt.Slides) { foreach (IShape shape in slide.Shapes) { if (shape.Placeholder != null) { switch (shape.Placeholder.Type) { case PlaceholderType.Title: shapelist.Add(shape); break; case PlaceholderType.CenteredTitle: shapelist.Add(shape); break; case PlaceholderType.Subtitle: shapelist.Add(shape); break; } } } } for (int i = 0; i < shapelist.Count; i++) { IAutoShape shape1 = shapelist[i] as IAutoShape; Console.WriteLine(shape1.TextFrame.Text); } Console.ReadKey(); } } }