C# OpenXML PowerPoint How to get Slide Duration

寵の児 提交于 2021-01-05 09:14:31

问题


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();
        }

回答1:


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:

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.

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:



来源:https://stackoverflow.com/questions/43225050/c-sharp-openxml-powerpoint-how-to-get-slide-duration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!