marc_s
marc_s

Reputation: 754488

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

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.

Upvotes: 4

Views: 3001

Answers (2)

marc_s
marc_s

Reputation: 754488

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

Upvotes: 7

Floris
Floris

Reputation: 46375

The closest I found was this snippet:

[ISO/IEC 29500-1 1st Edition]

sld (Presentation Slide)

This element specifies a slide within a slide list. The slide list is used to specify an ordering of slides.

[Example: Consider the following custom show with an ordering of slides.

<p:custShowLst>
  <p:custShow name="Custom Show 1" id="0">
    <p:sldLst>
      <p:sld r:id="rId4"/>
      <p:sld r:id="rId3"/>
      <p:sld r:id="rId2"/>
      <p:sld r:id="rId5"/>
    </p:sldLst>
  </p:custShow>
</p:custShowLst>In the above example the order specified to present the slides is slide 4, then 3, 2 and finally 5. end example]

In the MSDN documentation for the slide class

It seems that slides have an r:id of the form rId## where ## is the number of the slide. Maybe that's enough to get you going again?

Upvotes: 2

Related Questions