Adding Animation to PowerPoint Slide Elements in C#

Syncfusion is happy to share that Essential Presentation (.NET PowerPoint library) has added support to apply and edit animations in a PowerPoint slide from the 2018 Volume 2 (v16.2.0.41) release on. Using animation, you can grab and keep the attention of your audience by emphasizing key points, controlling the focus of the discussion, and entertaining in ways that will make your message memorable. You can animate any individual elements on a slide including text, pictures, and shapes.

Using this animation support in Essential Presentation, you can add a new animation effect or edit an animation effect in an existing PowerPoint slide; and control the direction, trigger, and duration of animation effects. This support is available across all the Microsoft platforms (.NET Framework, .NET Core, UWP, and Xamarin).

Animation effect types

Effects can make an object appear, disappear, or move. They can change an object’s size or color. You can add animation to elements on a slide using the below four types of animation effects:

  • Entrance Effects—An element with an animated entrance is not visible when the slide initially appears. It will appear on the slide in the manner specified by the entrance effect.
  • Emphasis effects—These effects animate the slide element that is already visible on the slide to draw attention to it by changing its appearance.
  • Motion path effects—These effects move an element along a path that you specify, over a period that you specify.
  • Exit effects—These effects allow the elements to leave/disappear from the slide.

 

Applying multiple animation effects to an element

For a slide element, you can apply an animation effect for each animation type (entrance, emphasis, exit, and motion paths). There are 149 animation effects available to apply to a slide element (among them, 50 effects are available for the entrance animation type, 49 effects for the exit animation type, 33 effects for emphasis, and 65 effects for the motion path animation type). Every animation effect will fall within one of these four animation types. Mostly, the animation effects are same for entrance and exit categories. When applying an effect for an exit type alone, you need to specify the type as exit through the PresetClassType property.

For example, the fade animation effect will be available in both entrance and exit categories. The following code example illustrates how to apply a fade animation effect to a rectangle with entrance and exit animation types.

//Create a PowerPoint presentation
using (IPresentation pptxDoc = Presentation.Create())
{

	//Add a blank slide to the presentation.

	ISlide slide = pptxDoc.Slides.Add(SlideLayoutType.Blank);

	//Add a shape to the slide.

	IShape shape = slide.Shapes.AddShape(AutoShapeType.Rectangle, 250, 100, 200, 200);

	//Get the sequence of the slide.

	ISequence sequence = slide.Timeline.MainSequence;

	//Add the entrance effect for the shape.

	IEffect entranceEffect = sequence.AddEffect(shape, EffectType.Bounce, EffectSubtype.None, EffectTriggerType.WithPrevious);

	//Add the emphasis effect for the shape.

	IEffect emphasisEffect = sequence.AddEffect(shape, EffectType.GrowShrink, EffectSubtype.None, EffectTriggerType.AfterPrevious);

	//Add the motion effect for the shape.

	IEffect motionEffect = sequence.AddEffect(shape, EffectType.PathLoopdeLoop, EffectSubtype.None, EffectTriggerType.AfterPrevious);

	//Add the exit effect for the shape.

	IEffect exitEffect = sequence.AddEffect(shape, EffectType.RandomBars, EffectSubtype.None, EffectTriggerType.AfterPrevious);

	//Change the presetclass of the effect from default entrance to exit.

	exitEffect.PresetClassType = EffectPresetClassType.Exit;

	//Save the PowerPoint presentation.

	pptxDoc.Save("Sample.pptx");

	//Close the PowerPoint presentation.

	pptxDoc.Close();

}

Choose the trigger type

 In PowerPoint presentations, an animation of an element can be triggered by any of the following actions:

  •  On clicking
  • With previous animation
  • After previous animation

 

The following code example illustrates how to apply a fade animation effect to a rectangle that will appear after a click in the slide show.

//Create a PowerPoint presentation
using (IPresentation presentation = Presentation.Create())
{
	//Add a slide to the presentation.
	ISlide slide = presentation.Slides.Add(SlideLayoutType.Blank);

	//Add a shape to appear with entrance category.
	IShape shape = slide.Shapes.AddShape(AutoShapeType.Rectangle, 10, 10, 200, 200);


	//Get the animation sequence.
	ISequence sequence = slide.Timeline.MainSequence;

	//Apply fade to the shape with click trigger. By default, the fade effect will fall in the entrance category. This means the animation will appear with previous animation sequence in parallel.
	IEffect effectClick = sequence.AddEffect(shape, EffectType.Fade, EffectSubtype.None, EffectTriggerType.WithPrevious);

	//Save the PowerPoint presentation.
	presentation.Save("TriggerAnimation.pptx");
}

Apply an animation to text

The Microsoft PowerPoint presentation allows text to be animated in the following ways:

  • Apply animation to the parent shape.
  • Apply individual animation effect options to text in a shape, so that the text will animate separately from its parent shape. This is mostly used with bullets and numbering.

Animate text by apply animation to its parent shape

The following code example illustrates how to apply an animation effect to simple text in a PowerPoint slide.

//Open an existing PowerPoint presentation from file system. 
using (IPresentation pptxDoc = Presentation.Open("Sample.pptx"))
{ 
    //Retrieve the first slide from the presentation. 
    ISlide slide = pptxDoc.Slides[0]; 

    //Retrieve the first shape. 
    IShape shape = slide.Shapes[0] as IShape; 

    //Access the animation sequence to create effects. 
    ISequence sequence = slide.Timeline.MainSequence; 

    //Add swivel effect with vertical subtype to the shape. Build type is used to represent the animated level of the paragraph. 
    IEffect bounceEffect = sequence.AddEffect(shape, EffectType.Swivel, EffectSubtype.Vertical, EffectTriggerType.OnClick); 

    //Save the presentation to the file system. 
    pptxDoc.Save("AnimateSimpleText.pptx"); 
}

Apply animation effects to bulleted and numbered lists

Animation effects can be applied to each paragraph or list within a shape.  You must set the BuildType enum to apply animation effects for the text. As with Microsoft PowerPoint, you can animate the text up to a maximum of five indentation or list levels. Syncfusion’s Presentation library has the below BuildType enumerator values to control animation for the list levels:

  • ByLevelParagraphs1—Apply the animation effect for the text at indent level 1.
  • ByLevelParagraphs2—Apply the animation effect for the text at indent level 2.
  • ByLevelParagraphs3—Apply the animation effect for the text at indent level 3.
  • ByLevelParagraphs4—Apply the animation effect for the text at indent level 4.
  • ByLevelParagraphs5—Apply the animation effect for the text at indent level 5.

 

The following code example illustrates how to apply an animation effect to text up to indentation level four in a bulleted list.

//Get the required shape with bulleted/numbered list. 
IShape rectWithBulletdList = slide.Shapes[0] as IShape;

//Get the main sequence of the slide.
ISequence sequence = slide.Timeline.MainSequence;

//Add the Random Effects animation effect to the shape.
IEffect shapeEffect = sequence.AddEffect(rectWithBulletdList, EffectType.RandomEffects, EffectSubtype.None, EffectTriggerType.OnClick, BuildType.ByLevelParagraphs4);

//Iterate the animation effect of the first paragraph.
IEffect firstParaEffect = sequence[1];

//Change the trigger type of the effect to AfterPrevious from default type OnClick.
firstParaEffect.Timing.TriggerType = EffectTriggerType.AfterPrevious;

//Iterate the animation effect of the second paragraph
IEffect secondParaEffect = sequence[2];

//Change the trigger type of the effect to AfterPrevious from default OnClick.
secondParaEffect.Timing.TriggerType = EffectTriggerType.AfterPrevious;

//Iterate the animation effect of the third paragraph.
IEffect thirdParaEffect = sequence[3];

//Change the trigger type of the effect to AfterPrevious from default OnClick.
thirdParaEffect.Timing.TriggerType = EffectTriggerType.AfterPrevious;

//Iterate the animation effect of the fourth paragraph.
IEffect fourthParaEffect = sequence[4];

//Change the trigger type of the effect to AfterPrevious from default OnClick.
fourthParaEffect.Timing.TriggerType = EffectTriggerType.AfterPrevious;
//Change the delay time of the effect to 3 seconds.
fourthParaEffect.Timing.TriggerDelayTime = 3;

//Iterate the animation effect of the fifth paragraph.
IEffect fifthParaEffect = sequence[5];

//Change the trigger type of the effect to AfterPrevious from default OnClick.
fifthParaEffect.Timing.TriggerType = EffectTriggerType.AfterPrevious;

Duration of the animation effect

The speed of the animation effect is determined by the Duration property. This is used to determine how long an animation effect should remain in a slideshow. The following code example illustrates how to control the duration of an animation effect applied to a rectangle in a PowerPoint slide.

//Open an existing presentation from file system. 
using (IPresentation pptxDoc = Presentation.Open("Sample.pptx")) 
{ 
	//Retrieve the first slide from the presentation. 
	ISlide slide = pptxDoc.Slides[0]; 

	//Retrieve a shape from the slide.
	IShape shape = slide.Shapes[0] as IShape; 

	//Access the animation main sequence to modify the effects. 
	ISequence sequence = slide.Timeline.MainSequence; 

	//Get the required animation effect.
	IEffect pathEffect = sequence[0] as IEffect; 

	//Increase the duration of the animation effect in seconds.
	pathEffect.Behaviors[0].Timing.Duration = 5; 

	//Save the presentation to the file system. 
	pptxDoc.Save("Duration.pptx"); 
}

Please refer to the below animation effect created with our Presentation library.

You can find the online sample to create an animation effect like the previous screenshot from here for a quick look—Create PowerPoint animation.

A complete runnable example can be downloaded from here.

Summary

The PowerPoint library now supports generating PowerPoint files with animation effects and modifying the existing animations applied to the PowerPoint slide elements. Take a moment to investigate the documentation, where you’ll find other options and features, all with accompanying code examples.

If you are new to our PowerPoint library, it is highly recommended that you follow our Getting Started guide.

To learn more about the functionalities of and find code snippets for animation, please refer to our documentation called Working with Animations

If you’re already a Syncfusion user, you can download the product setup here. If you’re not yet a Syncfusion user, you can download a free, 30-day trial on our website.

If you have any questions or require clarification on these features, please let us know in the comments below. You can also contact us through our support forum or Direct-Trac. We are happy to assist you!

If you like this blog post, we think you’ll also like the following free e-books:

C# Succinctly
ASP.NET Core Succinctly
ASP.NET MVC Succinctly
.NET Core Succinctly

Tags:

Share this post:

Related Posts

Leave a comment