本文介紹通過Java程序代碼給PPT幻燈片中的形狀添加動畫效果。需要準備PPT操作類庫Spire.Presentation for Java(下載地址)。編輯代碼前注意先將工具包lib文件夾下的Spire.Presentation.jar文件導入java程序。本文分兩部分介紹:
一、新建PPT文檔,添加形狀,設置動畫效果
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 AddAnimationToShape {
public static void main(String[]args) throws Exception{
//創建PowerPoint文檔
Presentation ppt = new Presentation();
//獲取幻燈片
ISlide slide = ppt.getSlides().get(0);
//添加一個形狀到幻燈片
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);
//設置形狀動畫效果
slide.getTimeline().getMainSequence().addEffect(shape, AnimationEffectType.CHANGE_LINE_COLOR);
//保存文檔
ppt.saveToFile("AddAnimationToShape.pptx", FileFormat.PPTX_2013);
}
}
二、加載已有PPT文檔,獲取形狀動畫效果,進行動畫效果設置,這裏可做更爲詳細的動畫設置,包括動畫重複播放類型、次數、持續時間、延遲時間等
import com.spire.presentation.*;
import com.spire.presentation.drawing.animation.AnimationEffect;
public class RepeatAnimation {
public static void main(String[] args) throws Exception{
//加載測試文檔
Presentation ppt = new Presentation();
ppt.loadFromFile("test.pptx");
//獲取第一張幻燈片
ISlide slide = ppt.getSlides().get(0);
//獲取幻燈片中第一個動畫效果
AnimationEffect animation = slide.getTimeline().getMainSequence().get(0);
//設置動畫效果循環播放類型、次數、持續時間、延遲時間
animation.getTiming().setAnimationRepeatType(AnimationRepeatType.Number);
animation.getTiming().setRepeatCount(2);//設置重複次數
animation.getTiming().setDuration(2);//設置持續時間
animation.getTiming().setTriggerDelayTime(2);//設置延遲時間
//animation.getTiming().setAnimationRepeatType(AnimationRepeatType.UtilEndOfSlide);//設置動畫循環播放至幻燈片末
//animation.getTiming().setAnimationRepeatType(AnimationRepeatType.UtilNextClick);//設置動畫循環播放至下次點擊
//保存結果文檔
ppt.saveToFile("RepeatAnimation.pptx", FileFormat.PPTX_2013);
ppt.dispose();
}
}
(完)