在演示文稿中的幻灯片之间添加切换

本主题演示如何使用 Open XML SDK 中的类以编程方式在演示文稿中的所有幻灯片之间添加转换。

获取 Presentation 对象

在 Open XML SDK 中 PresentationDocument , 类表示演示文稿文档包。 若要处理演示文稿文档,请先创建 类的 PresentationDocument 实例,然后使用该实例。 若要从文档创建类实例,请调用 Open 方法,该方法使用文件路径和布尔值作为第二个参数来指定文档是否可编辑。 若要打开文档进行读/写,请指定此参数的值 true ,如以下 using 语句所示。 在此代码中,file 参数是一个字符串,表示要从中打开文档的文件的路径。

using (PresentationDocument presentationDocument = PresentationDocument.Open(filePath, true))

在 v3.0.0+ 中, Close() 已删除 方法,转而依赖于 using 语句。 这可确保在 Dispose() 到达右大括号时自动调用 方法。 语句后面的 using 块为在 语句中创建 using 或命名的对象建立作用域,在本例 ppt中为 。

转换的结构

Transition 元素 <transition> 指定应用于从上一张幻灯片过渡到当前幻灯片的幻灯片切换类型。 也就是说,切换信息存储在切换完成后显示的幻灯片中。

下表列出了转换的属性以及每个属性的说明。

属性 说明
advClick (单击) 时前进 指定鼠标单击是否推进幻灯片。 如果未指定此属性,则假定值为 true。
advTm (在时间) 后前进 指定转换应开始的时间(以毫秒为单位)。 此设置可与 advClick 属性结合使用。 如果未指定此属性,则假定不会发生自动前进。
spd (转换速度) 指定从当前幻灯片过渡到下一张幻灯片时要使用的切换速度。

[示例:请考虑以下示例

      <p:transition spd="slow" advClick="1" advTm="3000">
        <p:randomBar dir="horz"/>
      </p:transition>

在上面的示例中,转换速度 <speed> 设置为慢 (可用选项:慢速、med、快速) 。 单击 <advClick> 时前进设置为 true,时间后 <advTm> 前进设置为 3000 毫秒。 RandomBar 子元素 <randomBar> 描述 randomBar 幻灯片切换效果,该效果使用一组随机放置的水平 <dir="horz"> 或垂直 <dir="vert"> 条在幻灯片上继续添加,直到新幻灯片完全显示。 示例结束]

可在此处查看 Transition 子元素的完整列表: Transition

备用内容的结构

Office Open XML 定义了一种用于存储不受 ISO/IEC 29500 Office Open XML 规范定义的内容的机制,例如由利用 Office Open XML 格式的未来软件应用程序开发的扩展。 此机制允许存储一系列内容的替代表示形式,使用方应用程序可以从这些表示形式中使用满足其要求的第一个替代项。

请考虑一个应用程序,该应用程序创建一个新的转换对象,用于指定转换的持续时间。 此功能未在 Office Open XML 规范中定义。 按如下所示使用 AlternateContent 块可以指定持续时间 <p14:dur> (以毫秒为单位)。

[示例

  <mc:AlternateContent xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
   xmlns:p14="http://schemas.microsoft.com/office/powerpoint/2010/main">
    <mc:Choice Requires="p14">
      <p:transition spd="slow" p14:dur="2000" advClick="1" advTm="3000">
        <p:randomBar/>
      </p:transition>
    </mc:Choice>
    <mc:Fallback>
      <p:transition spd="slow" advClick="1" advTm="3000">
        <p:randomBar/>
      </p:transition>
    </mc:Fallback>
  </mc:AlternateContent>

上述示例中的 Choice 元素需要 dur 属性来指定转换的持续时间,而 Fallback 元素允许不支持此命名空间的客户端看到适当的备用表示形式。 示例结束]

可在此处找到有关 P14 类的更多详细信息: P14

示例代码的工作方式

在 using 语句中打开演示文稿文件进行读/写访问后,代码将从演示文稿文档中获取演示文稿部件。 然后,它会检索演示文稿中所有幻灯片的关系 ID,并从关系 ID 中获取幻灯片部分。 然后,代码会检查幻灯片上是否没有设置现有切换效果,并将其替换为新的 RandomBarTransition。

   // Define the transition start time and duration in milliseconds
   string startTransitionAfterMs = "3000", durationMs = "2000";

   // Set to true if you want to advance to the next slide on mouse click
   bool advanceOnClick = true;

   // Iterate through each slide ID to get slides parts
   foreach (SlideId slideId in slidesIds)
   {
       // Get the relationship ID of the slide
       string? relId = slideId!.RelationshipId!.ToString();

       if (relId == null)
       {
           throw new NullReferenceException("RelationshipId not found");
       }

       // Get the slide part using the relationship ID
       SlidePart? slidePart = (SlidePart)presentationDocument.PresentationPart.GetPartById(relId);

       // Remove existing transitions if any
       if (slidePart.Slide.Transition != null)
       {
           slidePart.Slide.Transition.Remove();
       }

       // Check if there are any AlternateContent elements
       if (slidePart!.Slide.Descendants<AlternateContent>().ToList().Count > 0)
       {
           // Get all AlternateContent elements
           List<AlternateContent> alternateContents = [.. slidePart.Slide.Descendants<AlternateContent>()];
           foreach (AlternateContent alternateContent in alternateContents)
           {
               // Remove transitions in AlternateContentChoice within AlternateContent
               List<OpenXmlElement> childElements = alternateContent.ChildElements.ToList();

               foreach (OpenXmlElement element in childElements)
               {
                   List<Transition> transitions = element.Descendants<Transition>().ToList();
                   foreach (Transition transition in transitions)
                   {
                       transition.Remove();
                   }
               }
               // Add new transitions to AlternateContentChoice and AlternateContentFallback
               alternateContent!.GetFirstChild<AlternateContentChoice>();
               Transition choiceTransition = new Transition(new RandomBarTransition()) { Duration = durationMs, AdvanceAfterTime = startTransitionAfterMs, AdvanceOnClick = advanceOnClick, Speed = TransitionSpeedValues.Slow };
               Transition fallbackTransition = new Transition(new RandomBarTransition()) {AdvanceAfterTime = startTransitionAfterMs, AdvanceOnClick = advanceOnClick, Speed = TransitionSpeedValues.Slow };
               alternateContent!.GetFirstChild<AlternateContentChoice>()!.Append(choiceTransition);
               alternateContent!.GetFirstChild<AlternateContentFallback>()!.Append(fallbackTransition);
           }
       }

如果幻灯片上当前没有切换效果,代码将创建新的切换效果。 在这两种情况下,作为回退转换,都使用 RandomBarTransition,但没有 P14:dur (持续时间) ,以允许不支持此命名空间的客户端的 grater 支持

// Add transition if there is none
else
{
    // Check if there is a transition appended to the slide and set it to null
    if (slidePart.Slide.Transition != null)
    {
        slidePart.Slide.Transition = null;
    }
    // Create a new AlternateContent element
    AlternateContent alternateContent = new AlternateContent();
    alternateContent.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");

    // Create a new AlternateContentChoice element and add the transition
    AlternateContentChoice alternateContentChoice = new AlternateContentChoice() { Requires = "p14" };
    Transition choiceTransition = new Transition(new RandomBarTransition()) { Duration = durationMs, AdvanceAfterTime = startTransitionAfterMs, AdvanceOnClick = advanceOnClick, Speed = TransitionSpeedValues.Slow };
    Transition fallbackTransition = new Transition(new RandomBarTransition()) { AdvanceAfterTime = startTransitionAfterMs, AdvanceOnClick = advanceOnClick, Speed = TransitionSpeedValues.Slow };
    alternateContentChoice.Append(choiceTransition);

    // Create a new AlternateContentFallback element and add the transition
    AlternateContentFallback alternateContentFallback = new AlternateContentFallback(fallbackTransition);
    alternateContentFallback.AddNamespaceDeclaration("a14", "http://schemas.microsoft.com/office/drawing/2010/main");
    alternateContentFallback.AddNamespaceDeclaration("p16", "http://schemas.microsoft.com/office/powerpoint/2015/main");
    alternateContentFallback.AddNamespaceDeclaration("adec", "http://schemas.microsoft.com/office/drawing/2017/decorative");
    alternateContentFallback.AddNamespaceDeclaration("a16", "http://schemas.microsoft.com/office/drawing/2014/main");

    // Append the AlternateContentChoice and AlternateContentFallback to the AlternateContent
    alternateContent.Append(alternateContentChoice);
    alternateContent.Append(alternateContentFallback);
    slidePart.Slide.Append(alternateContent);
} 

示例代码

下面是可用于向所有幻灯片添加 RandomBarTransition 的完整示例代码。

AddTransmitionToSlides(args[0]);
static void AddTransmitionToSlides(string filePath)
{
    using (PresentationDocument presentationDocument = PresentationDocument.Open(filePath, true))
    {
    
     // Check if the presentation part and slide list are available
        if (presentationDocument.PresentationPart == null || presentationDocument.PresentationPart.Presentation.SlideIdList == null)
        {
            throw new NullReferenceException("Presentation part is empty or there are no slides");
        }

        // Get the presentation part
        PresentationPart presentationPart = presentationDocument.PresentationPart;

        // Get the list of slide IDs
        OpenXmlElementList slidesIds = presentationPart.Presentation.SlideIdList.ChildElements;

        // Define the transition start time and duration in milliseconds
        string startTransitionAfterMs = "3000", durationMs = "2000";

        // Set to true if you want to advance to the next slide on mouse click
        bool advanceOnClick = true;
     
        // Iterate through each slide ID to get slides parts
        foreach (SlideId slideId in slidesIds)
        {
            // Get the relationship ID of the slide
            string? relId = slideId!.RelationshipId!.ToString();

            if (relId == null)
            {
                throw new NullReferenceException("RelationshipId not found");
            }

            // Get the slide part using the relationship ID
            SlidePart? slidePart = (SlidePart)presentationDocument.PresentationPart.GetPartById(relId);

            // Remove existing transitions if any
            if (slidePart.Slide.Transition != null)
            {
                slidePart.Slide.Transition.Remove();
            }

            // Check if there are any AlternateContent elements
            if (slidePart!.Slide.Descendants<AlternateContent>().ToList().Count > 0)
            {
                // Get all AlternateContent elements
                List<AlternateContent> alternateContents = [.. slidePart.Slide.Descendants<AlternateContent>()];
                foreach (AlternateContent alternateContent in alternateContents)
                {
                    // Remove transitions in AlternateContentChoice within AlternateContent
                    List<OpenXmlElement> childElements = alternateContent.ChildElements.ToList();

                    foreach (OpenXmlElement element in childElements)
                    {
                        List<Transition> transitions = element.Descendants<Transition>().ToList();
                        foreach (Transition transition in transitions)
                        {
                            transition.Remove();
                        }
                    }
                    // Add new transitions to AlternateContentChoice and AlternateContentFallback
                    alternateContent!.GetFirstChild<AlternateContentChoice>();
                    Transition choiceTransition = new Transition(new RandomBarTransition()) { Duration = durationMs, AdvanceAfterTime = startTransitionAfterMs, AdvanceOnClick = advanceOnClick, Speed = TransitionSpeedValues.Slow };
                    Transition fallbackTransition = new Transition(new RandomBarTransition()) {AdvanceAfterTime = startTransitionAfterMs, AdvanceOnClick = advanceOnClick, Speed = TransitionSpeedValues.Slow };
                    alternateContent!.GetFirstChild<AlternateContentChoice>()!.Append(choiceTransition);
                    alternateContent!.GetFirstChild<AlternateContentFallback>()!.Append(fallbackTransition);
                }
            }
            
            // Add transition if there is none
            else
            {
                // Check if there is a transition appended to the slide and set it to null
                if (slidePart.Slide.Transition != null)
                {
                    slidePart.Slide.Transition = null;
                }
                // Create a new AlternateContent element
                AlternateContent alternateContent = new AlternateContent();
                alternateContent.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");

                // Create a new AlternateContentChoice element and add the transition
                AlternateContentChoice alternateContentChoice = new AlternateContentChoice() { Requires = "p14" };
                Transition choiceTransition = new Transition(new RandomBarTransition()) { Duration = durationMs, AdvanceAfterTime = startTransitionAfterMs, AdvanceOnClick = advanceOnClick, Speed = TransitionSpeedValues.Slow };
                Transition fallbackTransition = new Transition(new RandomBarTransition()) { AdvanceAfterTime = startTransitionAfterMs, AdvanceOnClick = advanceOnClick, Speed = TransitionSpeedValues.Slow };
                alternateContentChoice.Append(choiceTransition);

                // Create a new AlternateContentFallback element and add the transition
                AlternateContentFallback alternateContentFallback = new AlternateContentFallback(fallbackTransition);
                alternateContentFallback.AddNamespaceDeclaration("a14", "http://schemas.microsoft.com/office/drawing/2010/main");
                alternateContentFallback.AddNamespaceDeclaration("p16", "http://schemas.microsoft.com/office/powerpoint/2015/main");
                alternateContentFallback.AddNamespaceDeclaration("adec", "http://schemas.microsoft.com/office/drawing/2017/decorative");
                alternateContentFallback.AddNamespaceDeclaration("a16", "http://schemas.microsoft.com/office/drawing/2014/main");

                // Append the AlternateContentChoice and AlternateContentFallback to the AlternateContent
                alternateContent.Append(alternateContentChoice);
                alternateContent.Append(alternateContentFallback);
                slidePart.Slide.Append(alternateContent);
            } 
        }
    }
}

另请参阅