3

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • I believe the SlideID property does not relate to the position of the slide in the file - it is set when the slide is created, and as the slide is moved around, it doesn't change. In VBA, there is a separate property "SlideIndex" (order of slides) and "SlideNumber" (number on the slide). I think `SlideIndex` would be what you need... – Floris Feb 19 '13 at 17:39
  • @Floris: cannot find neither `SlideNumber` nor `SlideIndex` in the OpenXML object model ... – marc_s Feb 19 '13 at 17:41

2 Answers2

7

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!
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Presentation presentation = presentationPart.Presentation; // Verify that the slide ID list exists. if (presentation.SlideIdList != null) { IEnumerable slidePartList = presentationPart.SlideParts; foreach (SlidePart slidePart in slidePartList) { //YourCode } we can use in this way also. – madan Sep 18 '15 at 11:50
2

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?

Floris
  • 45,857
  • 6
  • 70
  • 122