Wiki
Clone wikiAspose for OpenXML / Get the titles of all the slides
OpenXML SDK
class Program { static void Main(string[] args) { foreach (string s in GetSlideTitles("Get the titles of all the slides.pptx")) Console.WriteLine(s); Console.ReadKey(); } // Get a list of the titles of all the slides in the presentation. public static IList<string> 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<string> 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<string> titlesList = new List<string>(); // Get the title of each slide in the slide order. foreach (var slideId in presentation.SlideIdList.Elements<SlideId>()) { 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<Shape>() 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<D.Paragraph>()) { // Add a line break. paragraphText.Append(paragraphSeparator); foreach (var text in paragraph.Descendants<D.Text>()) { 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<PlaceholderShape>(); 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; } }
Aspose.Slides
class Program { static void Main(string[] args) { foreach (string s in GetSlideTitles("Get the titles of all the slides.pptx")) Console.WriteLine(s); Console.ReadKey(); } // Get a list of the titles of all the slides in the presentation. public static IList<string> GetSlideTitles(string presentationFile) { // Create a new linked list of strings. List<string> texts = new List<string>(); //Instantiate PresentationEx class that represents PPTX using (PresentationEx pres = new PresentationEx(presentationFile)) { //Access all the slides foreach (SlideEx sld in pres.Slides) { //Iterate through shapes to find the placeholder foreach (ShapeEx shp in sld.Shapes) if (shp.Placeholder != null) { if (IsTitleShape(shp)) { //get the text of placeholder texts.Add(((AutoShapeEx)shp).TextFrame.Text); } } } } // Return an array of strings. return texts; } // Determines whether the shape is a title shape. private static bool IsTitleShape(ShapeEx shape) { switch (shape.Placeholder.Type) { case PlaceholderTypeEx.Title: case PlaceholderTypeEx.CenteredTitle: return true; default: return false; } } }
Download
Updated