Shape Animation

Animations are visual effects that can be applied to texts, images, shapes, or charts. They give life to presentations or its constituents.

Why Use Animations in Presentations?

Using animations, you can

  • control the flow of information
  • emphasize important points
  • increase interest or participation among your audience
  • make content easier to read or assimilate or process
  • draw your readers or viewers attention to important parts in a presentation

PowerPoint provides many options and tools for animations and animation effects across the entrance, exit, emphasis, and motion paths categories.

Animations in Aspose.Slides

  • Aspose.Slides provides the classes and types you need to work with animations under the Aspose.Slides.Animation namespace,
  • Aspose.Slides provides over 150 animation effects under the EffectType enumeration. These effects are essentially the same (or equivalent) effects used in PowerPoint.

Apply Animation to TextBox

Aspose.Slides for .NET allows you to apply animation to the text in a shape.

  1. Create an instance of the Presentation class.
  2. Get a slide’s reference through its index.
  3. Add a rectangle IAutoShape.
  4. Add text to IAutoShape.TextFrame.
  5. Get a main sequence of effects.
  6. Add an animation effect to IAutoShape.
  7. Set the TextAnimation.BuildType property to the value from BuildType Enumeration.
  8. Write the presentation to disk as a PPTX file.

This C# code shows you how to apply the Fade effect to AutoShape and set the text animation to the By 1st Level Paragraphs value:

// Instantiates a presentation class that represents a presentation file.
using (Presentation pres = new Presentation())
{
    ISlide sld = pres.Slides[0];
    
    // Adds new AutoShape with text
    IAutoShape autoShape = sld.Shapes.AddAutoShape(ShapeType.Rectangle, 20, 20, 150, 100);

    ITextFrame textFrame = autoShape.TextFrame;
    textFrame.Text = "First paragraph \nSecond paragraph \n Third paragraph";

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

    // Adds Fade animation effect to shape
    IEffect effect = sequence.AddEffect(autoShape, EffectType.Fade, EffectSubtype.None, EffectTriggerType.OnClick);

    // Animates shape text by 1st level paragraphs
    effect.TextAnimation.BuildType = BuildType.ByLevelParagraphs1;

    // Save the PPTX file to disk
    pres.Save(path + "AnimTextBox_out.pptx", SaveFormat.Pptx);
}

Apply Animation to PictureFrame

  1. Create an instance of the Presentation class.
  2. Get a slide’s reference through its index.
  3. Add or get a PictureFrame on the slide.
  4. Get the main sequence of effects.
  5. Add an animation effect to PictureFrame.
  6. Write the presentation to disk as a PPTX file.

This C# code shows you how to apply the Fly effect to a picture frame:

// Instantiates a presentation class that represents a presentation file.
using (Presentation pres = new Presentation())
{
    // Load Image to be added in presentaiton image collection
    IImage image = Images.FromFile("aspose-logo.jpg");
    IPPImage ppImage = pres.Images.AddImage(image);
    image.Dispose();

    // Adds picture frame to slide
    IPictureFrame picFrame = pres.Slides[0].Shapes.AddPictureFrame(ShapeType.Rectangle, 50, 50, 100, 100, ppImage);

    // Gets the main sequence of the slide.
    ISequence sequence = pres.Slides[0].Timeline.MainSequence;

    // Adds Fly from Left animation effect to picture frame
    IEffect effect = sequence.AddEffect(picFrame, EffectType.Fly, EffectSubtype.Left, EffectTriggerType.OnClick);

    // Save the PPTX file to disk
    pres.Save("AnimImage_out.pptx", SaveFormat.Pptx);
}

Apply Animation to Shape

  1. Create an instance of the Presentation class.
  2. Get a slide’s reference through its index.
  3. Add a rectangle IAutoShape.
  4. Add a Bevel IAutoShape (when this object is clicked, the animation gets played).
  5. Create a sequence of effects on the bevel shape.
  6. Create a custom UserPath.
  7. Add commands for moving to the UserPath.
  8. Write the presentation to disk as a PPTX file.

This C# code shows you how to apply the PathFootball (path football) effect to a shape:

// Instantiates a Presentation class that represents a presentation file.
using (Presentation pres = new Presentation())
{
    ISlide sld = pres.Slides[0];

    // Creates PathFootball effect for existing shape from scratch.
    IAutoShape ashp = sld.Shapes.AddAutoShape(ShapeType.Rectangle, 150, 150, 250, 25);

    ashp.AddTextFrame("Animated TextBox");

    // Adds the PathFootBall animation effect.
    pres.Slides[0].Timeline.MainSequence.AddEffect(ashp, EffectType.PathFootball,
                           EffectSubtype.None, EffectTriggerType.AfterPrevious);

    // Creates some kind of "button".
    IShape shapeTrigger = pres.Slides[0].Shapes.AddAutoShape(ShapeType.Bevel, 10, 10, 20, 20);

    // Creates a sequence of effects for the button.
    ISequence seqInter = pres.Slides[0].Timeline.InteractiveSequences.Add(shapeTrigger);

    // Creates a custom user path. Our object will be moved only after the button is clicked.
    IEffect fxUserPath = seqInter.AddEffect(ashp, EffectType.PathUser, EffectSubtype.None, EffectTriggerType.OnClick);

    // Adds commands for moving since created path is empty.
    IMotionEffect motionBhv = ((IMotionEffect)fxUserPath.Behaviors[0]);

    PointF[] pts = new PointF[1];
    pts[0] = new PointF(0.076f, 0.59f);
    motionBhv.Path.Add(MotionCommandPathType.LineTo, pts, MotionPathPointsType.Auto, true);
    pts[0] = new PointF(-0.076f, -0.59f);
    motionBhv.Path.Add(MotionCommandPathType.LineTo, pts, MotionPathPointsType.Auto, false);
    motionBhv.Path.Add(MotionCommandPathType.End, null, MotionPathPointsType.Auto, false);

    // Writes the PPTX file to disk
    pres.Save("AnimExample_out.pptx", SaveFormat.Pptx);
}

Get the Animation Effects Applied to Shape

The following examples show you how to use the GetEffectsByShape method from the ISequence interface to get all animation effects applied to a shape.

Example 1: Get animation effects applied to a shape on a normal slide

Previously, you learned how to add animation effects to shapes in PowerPoint presentations. The following sample code shows you how to get the effects applied to the first shape on the first normal slide in the presentation AnimExample_out.pptx.

using (Presentation presentation = new Presentation("AnimExample_out.pptx"))
{
    ISlide firstSlide = presentation.Slides[0];

    // Gets the main animation sequence of the slide.
    ISequence sequence = firstSlide.Timeline.MainSequence;

    // Gets the first shape on the first slide.
    IShape shape = firstSlide.Shapes[0];

    // Gets animation effects applied to the shape.
    IEffect[] shapeEffects = sequence.GetEffectsByShape(shape);

    if (shapeEffects.Length > 0)
        Console.WriteLine($"The shape {shape.Name} has {shapeEffects.Length} animation effects.");
}

Example 2: Get all animation effects, including those inherited from placeholders

If a shape on a normal slide has placeholders that are on the layout slide and/or master slide, and animation effects have been added to these placeholders, then all effects of the shape will be played during the slide show, including those inherited from the placeholders.

Let’s say we have a PowerPoint presentation file sample.pptx with one slide containg only a footer shape with the text “Made with Aspose.Slides” and the Random Bars effect is applied to the shape.

Slide shape animation effect

Let’s also assume that the Split effect is applied to the footer placeholder on the layout slide.

Layout shape animation effect

And finally, the Fly In effect is applied to the footer placeholder on the master slide.

Master shape animation effect

The following sample code shows you how to use the GetBasePlaceholder method from the IShape interface to access the shape placeholders and get the animation effects applied to the footer shape, including those inherited from placeholders located on the layout and master slides.

using (Presentation presentation = new Presentation("sample.pptx"))
{
    ISlide slide = presentation.Slides[0];

    // Get animation effects of the shape on the normal slide.
    IShape shape = slide.Shapes[0];
    IEffect[] shapeEffects = slide.Timeline.MainSequence.GetEffectsByShape(shape);

    // Get animation effects of the placeholder on the layout slide.
    IShape layoutShape = shape.GetBasePlaceholder();
    IEffect[] layoutShapeEffects = slide.LayoutSlide.Timeline.MainSequence.GetEffectsByShape(layoutShape);

    // Get animation effects of the placeholder on the master slide.
    IShape masterShape = layoutShape.GetBasePlaceholder();
    IEffect[] masterShapeEffects = slide.LayoutSlide.MasterSlide.Timeline.MainSequence.GetEffectsByShape(masterShape);

    Console.WriteLine("Main sequence of shape effects:");
    PrintEffects(masterShapeEffects);
    PrintEffects(layoutShapeEffects);
    PrintEffects(shapeEffects);
}
static void PrintEffects(IEnumerable<IEffect> effects)
{
    foreach (IEffect effect in effects)
    {
        Console.WriteLine($"{effect.Type} {effect.Subtype}");
    }
}

Output:

Main sequence of shape effects:
Fly Bottom
Split VerticalIn
RandomBars Horizontal

Change Animation Effect Timing Properties

Aspose.Slides for .NET allows you to change the Timing properties of an animation effect.

This is the Animation Timing pane and extended menu in Microsoft PowerPoint:

example1_image

These are the correspondences between PowerPoint Timing and Effect.Timing properties:

This is how you change the Effect Timing properties:

  1. Apply or get the animation effect.
  2. Set new values for the Effect.Timing properties you need.
  3. Save the modified PPTX file.

This C# code demonstrates the operation:

// Instantiates a presentation class that represents a presentation file.
using (Presentation pres = new Presentation("AnimExample_out.pptx"))
{
    // Gets the main sequence of the slide.
    ISequence sequence = pres.Slides[0].Timeline.MainSequence;

    // Gets the first effect of main sequence.
    IEffect effect = sequence[0];

    // Changes effect TriggerType to start on click
    effect.Timing.TriggerType = EffectTriggerType.OnClick;

    // Changes effect Duration
    effect.Timing.Duration = 3f;

    // Changes effect TriggerDelayTime
    effect.Timing.TriggerDelayTime = 0.5f;

    // If the effect Repeat value is "none"
    if (effect.Timing.RepeatCount == 1f)
    {
        // Changes effect Repeat to "Until Next Click"
        effect.Timing.RepeatUntilNextClick = true;
    }
    else
    {
        // Changes effect Repeat to "Until End of Slide"
        effect.Timing.RepeatUntilEndSlide = true;
    }

    // Turns the effect Rewind on
        effect.Timing.Rewind = true;
    
    // Saves the PPTX file to disk
    pres.Save("AnimExample_changed.pptx", SaveFormat.Pptx);
}

Animation Effect Sound

Aspose.Slides provides these properties to allow you to work with sounds in animation effects:

Add Animation Effect Sound

This C# code shows you how to add an animation effect sound and stop it when the next effect starts:

using (Presentation pres = new Presentation("AnimExample_out.pptx"))
{
	// Adds audio to presentation audio collection
	IAudio effectSound = pres.Audios.AddAudio(File.ReadAllBytes("sampleaudio.wav"));

	ISlide firstSlide = pres.Slides[0];

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

	// Gets the first effect of the main sequence
	IEffect firstEffect = sequence[0];

	// Сhecks the effect for "No Sound"
	if (!firstEffect.StopPreviousSound && firstEffect.Sound == null)
	{
		// Adds sound for the first effect
		firstEffect.Sound = effectSound;
	}

	// Gets the first interactive sequence of the slide.
	ISequence interactiveSequence = firstSlide.Timeline.InteractiveSequences[0];

	// Sets the effect "Stop previous sound" flag
	interactiveSequence[0].StopPreviousSound = true;

	// Writes the PPTX file to disk
	pres.Save("AnimExample_Sound_out.pptx", SaveFormat.Pptx);
}

Extract Animation Effect Sound

  1. Create an instance of the Presentation class.
  2. Get a slide’s reference through its index.
  3. Get the main sequence of effects.
  4. Extract the Sound embedded to each animation effect.

This C# code shows you how to extract the sound embedded in an animation effect:

// Instantiates a presentation class that represents a presentation file.
using (Presentation presentation = new Presentation("EffectSound.pptx"))
{
    ISlide slide = presentation.Slides[0];

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

    foreach (IEffect effect in sequence)
    {
        if (effect.Sound == null)
            continue;

        // Extracts the effect sound in byte array
        byte[] audio = effect.Sound.BinaryData;
    }
}

After Animation

Aspose.Slides for .NET allows you to change the After animation property of an animation effect.

This is the Animation Effect pane and extended menu in Microsoft PowerPoint:

example1_image

PowerPoint Effect After animation drop-down list matches these properties:

This C# code shows you how to change an after animation effect:

// Instantiates a presentation class that represents a presentation file
using (Presentation pres = new Presentation("AnimImage_out.pptx"))
{
    ISlide firstSlide = pres.Slides[0];

    // Gets the first effect of the main sequence
    IEffect firstEffect = firstSlide.Timeline.MainSequence[0];

    // Changes the after animation type to Color
    firstEffect.AfterAnimationType = AfterAnimationType.Color;

    // Sets the after animation dim color
    firstEffect.AfterAnimationColor.Color = Color.AliceBlue;

    // Writes the PPTX file to disk
    pres.Save("AnimImage_AfterAnimation.pptx", SaveFormat.Pptx);
}

Animate Text

Aspose.Slides provides these properties to allow you to work with an animation effect’s Animate text block:

This is how you can change the Effect Animate text properties:

  1. Apply or get the animation effect.
  2. Set the IEffect.TextAnimation.BuildType property to BuildType.AsOneObject value to turn off the By Paragraphs animation mode.
  3. Set new values for the IEffect.AnimateTextType and IEffect.DelayBetweenTextParts properties.
  4. Save the modified PPTX file.

This C# code demonstrates the operation:

// Instantiates a presentation class that represents a presentation file.
using (Presentation pres = new Presentation("AnimTextBox_out.pptx"))
{
    ISlide firstSlide = pres.Slides[0];

    // Gets the first effect of the main sequence
    IEffect firstEffect = firstSlide.Timeline.MainSequence[0];

    // Changes the effect Text animation type to "As One Object"
    firstEffect.TextAnimation.BuildType = BuildType.AsOneObject;

    // Changes the effect Animate text type to "By word"
    firstEffect.AnimateTextType = AnimateTextType.ByWord;

    // Sets the delay between words to 20% of effect duration
    firstEffect.DelayBetweenTextParts = 20f;

    // Writes the PPTX file to disk
    pres.Save("AnimTextBox_AnimateText.pptx", SaveFormat.Pptx);
}