幻灯片过渡是当您在 PowerPoint 演示文稿中从一张幻灯片移动到另一张幻灯片时出现的视觉效果。此外,您可以根据自己的选择设置过渡声音、速度、持续时间和其他效果。在本文中,您将学习如何使用 C# 以编程方式在 PowerPoint 演示文稿中添加幻灯片过渡。

.NET API 在 PowerPoint 中添加幻灯片过渡

为了给 PowerPoint 演示文稿中的幻灯片添加过渡效果,我们将使用 Aspose.Slides for .NET。它是一个用于创建和操作 PowerPoint 和 OpenOffice 文档的 .NET API。您可以 下载 API 或使用 NuGet 安装它。

PM> Install-Package Aspose.Slides.NET 

使用 C# 添加幻灯片过渡

以下是使用 C# 在 PowerPoint 演示文稿中添加幻灯片过渡的步骤。

  1. 首先,使用 Presentation 类加载 PowerPoint 演示文稿。
  2. 然后,使用 SlideShowTransition.Type 属性和 TransitionType 枚举设置幻灯片过渡类型。
  3. 最后,使用 Presentation.Save(String, SaveFormat) 方法保存更新的演示文稿。

以下代码示例演示如何在 PowePoint 演示文稿中设置幻灯片的过渡。

// 实例化 Presentation 类以加载源演示文件
using (Presentation presentation = new Presentation("AccessSlides.pptx"))
{
    // 在幻灯片 1 上应用圆形过渡
    presentation.Slides[0].SlideShowTransition.Type = TransitionType.Circle;

    // 在幻灯片 2 上应用梳状过渡
    presentation.Slides[1].SlideShowTransition.Type = TransitionType.Comb;

    // 保存演示文稿
    presentation.Save("SampleTransition_out.pptx", SaveFormat.Pptx);
}

使用 C# 添加高级幻灯片过渡

以下是设置高级幻灯片过渡选项(如持续时间、声音、速度等)的步骤。

  1. 首先,使用 Presentation 类加载 PowerPoint 演示文稿。
  2. 使用 SlideShowTransition.Type 属性和 TransitionType 枚举设置幻灯片过渡类型。
  3. 设置高级效果,如 SlideShowTransition.SoundSlideShowTransition.AdvanceAfterTime 等(参见效果列表)。
  4. 最后,使用 Presentation.Save(String, SaveFormat) 方法保存更新的演示文稿。

以下代码示例演示如何在 PowerPoint 演示文稿中设置高级幻灯片切换选项。

// 实例化表示演示文件的演示类
using (Presentation pres = new Presentation("BetterSlideTransitions.pptx"))
{

    // 在幻灯片 1 上应用圆形过渡
    pres.Slides[0].SlideShowTransition.Type = TransitionType.Circle;

    // 设置过渡时间为 3 秒
    pres.Slides[0].SlideShowTransition.AdvanceOnClick = true;
    pres.Slides[0].SlideShowTransition.AdvanceAfterTime = 3000;

    // 在幻灯片 2 上应用梳状过渡
    pres.Slides[1].SlideShowTransition.Type = TransitionType.Comb;

    // 设置过渡时间为 5 秒
    pres.Slides[1].SlideShowTransition.AdvanceOnClick = true;
    pres.Slides[1].SlideShowTransition.AdvanceAfterTime = 5000;

    // 在幻灯片 3 上应用缩放类型过渡
    pres.Slides[2].SlideShowTransition.Type = TransitionType.Zoom;

    // 设置过渡时间为 7 秒
    pres.Slides[2].SlideShowTransition.AdvanceOnClick = true;
    pres.Slides[2].SlideShowTransition.AdvanceAfterTime = 7000;

    // 保存演示文稿
    pres.Save("SampleTransition_out.pptx", SaveFormat.Pptx);
}

使用 C# 在 PowerPoint 中设置变形过渡

使用 Aspose.Slides for .NET,您还可以设置变形过渡,用于制作流畅的动画。 API 支持以下变形转换:

  • ByObject: Morph transition will be performed considering shapes as indivisible objects.
  • ByWord: Morph transition will be performed with transferring text by words where possible.
  • ByChar: Morph transition will be performed with transferring text by characters where possible.

以下是使用 C# 在 PowerPoint 演示文稿中添加变形过渡的步骤。

  1. 首先,使用 Presentation 类加载 PowerPoint 演示文稿。
  2. SlideShowTransition.Type 属性设置为 TransitionType.Morph
  3. 使用 ((IMorphTransition)presentation.Slides[0].SlideShowTransition.Value).MorphType 属性设置变形过渡类型。
  4. 最后,使用 Presentation.Save(String, SaveFormat) 方法保存更新的演示文稿。

以下代码示例演示如何在 PowerPoint 演示文稿中设置变形过渡。

// 加载 PowerPoint 演示文稿
using (Presentation presentation = new Presentation("presentation.pptx"))
{
    // 添加变形过渡
    presentation.Slides[0].SlideShowTransition.Type = TransitionType.Morph;
    ((IMorphTransition)presentation.Slides[0].SlideShowTransition.Value).MorphType = TransitionMorphType.ByWord;
    
    // 保存演示文稿
    presentation.Save("presentation-out.pptx", SaveFormat.Pptx);
}

获取免费 API 许可证

您可以通过申请 临时许可证 来使用 Aspose.Slides for .NET,而不受评估限制。

结论

在本文中,您学习了如何使用 C# 在 PowerPoint 演示文稿中添加幻灯片过渡。此外,您还了解了如何以编程方式在幻灯片中设置变形过渡。此外,您可以访问 文档 以探索 API 的其他功能。此外,您可以随时通过我们的 论坛 告诉我们您的疑问。

也可以看看