Monday, 9 August 2021

Add Animations to PowerPoint Slides in Java

Animations are normally used in PowerPoint slides and they are visual effects which make your text or objects in slides come alive, so that they can catch your audience’s attention and help them engage with you and your presentation.

This article will introduce how to add animations to shapes and paragraph text in slides using Java codes with the help of a free third-party library called Free Spire.Presentation for Java.

BEFORE CODING

First of all, you need to create a development environment and then add a Jar file which is located in the “lib” folder of the free library to Intellij IDEA. Finally create a Java class to run the corresponding codes.

You can get the package of the free library from the link. Of course, there is a way that you don’t need to download the package rather than use the following Maven configurations to add the Jar file to IDEA.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://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>

USING THE CODES

Add an animation to a shape in a PowerPoint slide

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import com.spire.presentation.drawing.animation.AnimationEffectType;
import java.awt.*;
import java.awt.geom.Rectangle2D;

public class SetAnimationForShape {
public static void main(String[] args) throws Exception {
//create a PowerPoint document
Presentation ppt = new Presentation();
//add a slide
ISlide slide = ppt.getSlides().get(0);

//Add a shape to slide
IAutoShape shape = slide.getShapes().appendShape(ShapeType.CUBE, new Rectangle2D.Double(50, 150, 150, 150));
shape.getFill().setFillType(FillFormatType.SOLID);
shape.getFill().getSolidColor().setColor(Color.orange);
shape.getShapeStyle().getLineColor().setColor(Color.white);

//Add animation to the shape
slide.getTimeline().getMainSequence().addEffect(shape, AnimationEffectType.FADED_SWIVEL);

//save the document
ppt.saveToFile("output/AddAnimationToShape.pptx", FileFormat.PPTX_2013);
}
}
Output

Add an animation to paragraph text in a PowerPoint slide

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import com.spire.presentation.drawing.animation.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;

public class SetAnimationForText {
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 the slide
IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Double(150, 150, 450, 100));
shape.getFill().setFillType(FillFormatType.
SOLID);
shape.getFill().getSolidColor().setColor(Color.
gray);
shape.getShapeStyle().getLineColor().setColor(Color.
white);
shape.appendTextFrame(
"This demo shows how to apply animation on paragraph in PPT document.");

//Add 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 result document
ppt.saveToFile("output/AddAnimationOnPara.pptx", FileFormat.PPTX_2013);
ppt.dispose();
}
}

output




No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...