C#OpenXML PowerPoint如何获取幻灯片持续时间 [英] C# OpenXML PowerPoint How to get Slide Duration

查看:81
本文介绍了C#OpenXML PowerPoint如何获取幻灯片持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取PPTX文件的inculde效果中幻灯片的总持续时间?

 使用(PresentationDocument doc = PresentationDocument.Open(@"C:\ powerpoint \ sample.pptx",true)){//获取文档的演示文稿部分.PresentationPartpresentationPart = doc.PresentationPart;//没有演示部分?该文档有问题.如果(presentationPart == null){抛出新的ArgumentException("fileName");}Console.WriteLine(presentationPart.SlideParts.Count()+"count");//我想像这样presentationPart.SlidePart.Duration();} 

解决方案

为使演示文稿不间断播放,请

注意:有两个过渡-一个在 Choice 元素下,另一个在 Fallback 元素下.您可以忽略后备广告下的一个,因为 advTm 属性在这两个属性中始终相同.

我编写了一种方法,该方法将返回为 SlidePart 找到的第一个 advTm ,否则返回0.

 私有字符串GetSlideDuration(SlidePart slidePart){字符串returnDuration ="0";尝试{幻灯片slide1 = slidePart.Slide;var transitions = slide1.Descendants< Transition>();foreach(转换中的var转换){如果(transition.AdvanceAfterTime.HasValue)返回transition.AdvanceAfterTime;休息;}}抓住(前例外){//没做什么}return returnDuration;} 

我用它来编写一个简单的WPF应用程序,以显示演示文稿的总时间.

可以在

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

解决方案

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:

这篇关于C#OpenXML PowerPoint如何获取幻灯片持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆