Search code examples
c#-4.0openxml-sdkpowerpoint-2010

Enumerating PowerPoint slides in order


I'm trying to analyze an existing PowerPoint 2010 .pptx file using the OpenXML SDK 2.0.

What I'm trying to achieve is to

  • enumerate the slides in order (as they appear in the PPTX)
  • extracting all textual bits from each slide

I've started and gotten so far - I can enumerate the SlideParts from the PresentationPart - but I cannot seem to find a way to make this an ordered enumeration - the slides are being returned in pretty much arbitrary order...

Any trick to get these slides in the order defined in the PPTX file?

using (PresentationDocument doc = PresentationDocument.Open(fileName, false))
{
   // Get the presentation part of the document.
   PresentationPart presentationPart = doc.PresentationPart;

   foreach (var slide in presentationPart.SlideParts)
   {
        ...
   }
}

I was hoping to find something like a SlideID or Sequence number or something - some item or property I could use in a Linq expression like

.OrderBy(s => s.SlideID)

on that slideparts collection.


Solution

  • It's a bit more involved than I had hoped for - and the docs are a bit sketchy at times....

    Basically, I had to enumerate the SlideIdList on the PresentationPart and do some XML-foo to get from that SlideId to the actual slide in the OpenXML presentation.

    Something along the lines of:

    using (PresentationDocument doc = PresentationDocument.Open(fileName, false))
    {
        // Get the presentation part of the document.
        PresentationPart presentationPart = doc.PresentationPart;
    
        // get the SlideIdList
        var items = presentationPart.Presentation.SlideIdList;
    
        // enumerate over that list
        foreach (SlideId item in items)
        {
            // get the "Part" by its "RelationshipId"
            var part = presentationPart.GetPartById(item.RelationshipId);
    
            // this part is really a "SlidePart" and from there, we can get at the actual "Slide"
            var slide = (part as SlidePart).Slide;
    
            // do more stuff with your slides here!
        }
    }