Teppei  Abe
Teppei Abe

Reputation: 113

C# OpenXML PowerPoint How to get Slide Duration

How can I get total slide duration time in PPTX file inculde effect?

using (PresentationDocument doc = PresentationDocument.Open(@"C:\powerpoint\sample.pptx", true))
        {
            // Get the presentation part of the document.
            PresentationPart presentationPart = doc.PresentationPart;
            // No presentation part? Something is wrong with the document.
            if (presentationPart == null)
            {
                throw new ArgumentException("fileName");
            }

            Console.WriteLine(presentationPart.SlideParts.Count() + "count");
            // I want to get like this
            presentationPart.SlidePart.Duration();
        }

Upvotes: 1

Views: 1130

Answers (1)

Taterhead
Taterhead

Reputation: 5951

In order for a Presentation to play without pause, you need to make the Presentation Self Running. Once you do this, the duration of each slide is stored as an attribute named Advance After Time (advTm). The value is stored as text milliseconds in the Transition element. The details can be referenced here under the Transition Trigger heading near the bottom.

Example xml is shown here:

enter image description here

Note: there are two transitions - one under the Choice element and the other under the Fallback element. You can ignore the one under Fallback because the advTm attribute will always be the same in both.

I wrote a method that will return the first advTm found for a SlidePart, otherwise it returns 0.

private string GetSlideDuration(SlidePart slidePart)
    {
        string returnDuration = "0";
        try
        {
            Slide slide1 = slidePart.Slide;

            var transitions = slide1.Descendants<Transition>(); 
            foreach (var transition in transitions)
            {
                if (transition.AdvanceAfterTime.HasValue)
                    return transition.AdvanceAfterTime;
                break;
            }
        }
        catch (Exception ex)
        {
            //Do nothing
        }

        return returnDuration;

    }

I used this to write a simple WPF application that displays the total time of the Presentation.

enter image description here

The code for the wpf can be found here.

Update

After more research, I've discovered that transitions and animations will add to the slide time. Advance After Time duration discussed above does not cut these times short and must be accounted for also. So I have updated the solution code at the github link above to take account of these. A new screen shot for the slide times with breakdowns and total presentation time is here:

enter image description here

Upvotes: 4

Related Questions