DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on

How to Apply Animation Effect to Text and Shapes in PowerPoint in Java

Animation can help to make a PowerPoint document more dynamic and attractive. You can animate both text or shapes on a PowerPoint slide. In this article, I am going to show you how to apply animation effect to text and shapes in PowerPoint programmatically in Java using Free Spire.Presentation for Java library.

Installation

If you use maven, you need to specify the dependencies for Free Spire.Presentation for Java library in your project’s pom.xml file.

<repositories>  
        <repository>  
            <id>com.e-iceblue</id>  
            <name>e-iceblue</name>  
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>  
        </repository>  
</repositories>  
<dependencies>  
    <dependency>  
        <groupId>e-iceblue</groupId>  
        <artifactId>spire.presentation.free</artifactId>  
        <version>3.9.0</version>  
    </dependency>  
</dependencies>
Enter fullscreen mode Exit fullscreen mode

For non-maven projects, download Free Spire.Presentation for Java pack from this website, unzip the package and add Spire.Presentation.jar in the lib folder into your project as a dependency.

Apply animation effect to shapes

Free Spire.Presentation for Java supports 151 animation effects, you can see the full list of animation effects in AnimationEffectType enum. The following example shows how to add boomerang effect to a shape.

import com.spire.presentation.*;
import com.spire.presentation.drawing.animation.*;

import java.awt.geom.Rectangle2D;

public class AnimateShapes {
    public static void main(String []args) throws Exception {
        //Create a Presentation instance
        Presentation ppt = new Presentation();
        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);

        //Add a shape to slide
        IAutoShape shape = slide.getShapes().appendShape(ShapeType.CUBE, new Rectangle2D.Double(250, 150, 150, 150));

        //Add animation effect to the shape
        AnimationEffect effect= slide.getTimeline().getMainSequence().addEffect(shape, AnimationEffectType.BOOMERANG);

        //Save the document
        ppt.saveToFile("AddAnimationToShape.pptx", FileFormat.PPTX_2013);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:
Animate Shape

Apply animation effect to text

Animation effect can also be added to text. The following example shows how to add animation effect to a particular paragraph in a shape.

import com.spire.presentation.*;
import com.spire.presentation.drawing.animation.AnimationEffect;
import com.spire.presentation.drawing.animation.AnimationEffectType;

import java.awt.geom.Rectangle2D;

public class AnimateText {
    public static void main(String []args) throws Exception {
        //Create an instance of presentation document
        Presentation ppt = new Presentation();

        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);

        //Add a shape to the slide
        IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Double(250, 150, 250, 100));

        //Add text to the shape
        shape.appendTextFrame("This example shows how to apply animation to text in PPT document.");

        //Apply animation effect to the first paragraph in the shape
        AnimationEffect animation = shape.getSlide().getTimeline().getMainSequence().addEffect(shape, AnimationEffectType.FLOAT);
        animation.setStartEndParagraphs(0, 0);

        //Save the document
        ppt.saveToFile("AddAnimationToText.pptx", FileFormat.PPTX_2013);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:
Animate Text

Top comments (0)