C#OpenXML PowerPoint如何获得幻灯片持续时间

时间:2017-04-05 07:50:13

标签: c# powerpoint openxml slide duration

如何在PPTX文件中获得总幻灯片持续时间?

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 个答案:

答案 0 :(得分:1)

为了让演示文稿无间断地播放,you need to make the Presentation Self Running。执行此操作后,每张幻灯片的持续时间将存储为名为Advance After Time( advTm )的属性。该值以 Transition 元素中的文本毫秒存储。详细信息可以是referenced here under the Transition Trigger heading near the bottom

示例xml如下所示:

enter image description here

注意:有两种转换 - 一种位于选择元素下,另一种位于后备元素下。你可以忽略Fallback下的那个,因为 advTm 属性在两者中都是相同的。

我写了一个方法,它将返回为 SlidePart 找到的第一个 advTm ,否则返回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;

    }

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

enter image description here

可以找到wpf的代码here

<强>更新

经过更多研究后,我发现transitionsanimations会增加幻灯片时间。在上面讨论的持续时间之后不会缩短这些时间并且也必须考虑。所以我在上面的github链接更新了解决方案代码以考虑这些。幻灯片时间的新屏幕截图显示了故障和总演示时间:

enter image description here

相关问题