PowerPoint文档(幻灯片)是一种常见的演示文档,如果能给形状添加动画,演示时就显得生动,有趣。本文将介绍如何通过编程的方式给PowerPoint文档里的形状添加动画,获取动画。
添加动画
C#
//创建一个ppt对象
Presentation ppt = new Presentation();
//获取第一张幻灯片
ISlide slide = ppt.Slides[0];
//添加一个形状,设置填充色颜色,添加文本
IAutoShape shape = slide.Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 50, 200, 80));
shape.Fill.FillType = FillFormatType.Solid;
shape.Fill.SolidColor.Color = Color.LightBlue;
shape.ShapeStyle.LineColor.Color = Color.White;
shape.AppendTextFrame("Set Animation");
//给形状添加动画
AnimationEffect effect = shape.Slide.Timeline.MainSequence.AddEffect(shape, AnimationEffectType.FadedSwivel);
//设置持续时间
effect.Timing.Duration = 2;
//设置重复次数
effect.Timing.RepeatCount = 2;
//设置动画文本效果
effect.IterateType = Spire.Presentation.Drawing.TimeLine.AnimateType.Letter;
//设置动画文本字符之间延迟时间
effect.IterateTimeValue = 2;
//保存文件
ppt.SaveToFile("SetAnimations.pptx", FileFormat.Pptx2010);
VB.NET
'创建一个ppt对象
Dim ppt As New Presentation()
'获取第一张幻灯片
Dim slide As ISlide = ppt.Slides(0)
'添加一个形状,设置填充色颜色,添加文本
Dim shape As IAutoShape = slide.Shapes.AppendShape(ShapeType.Rectangle, New RectangleF(50, 50, 200, 80))
shape.Fill.FillType = FillFormatType.Solid
shape.Fill.SolidColor.Color = Color.LightBlue
shape.ShapeStyle.LineColor.Color = Color.White
shape.AppendTextFrame("Set Animation")
'给形状添加动画
Dim effect As AnimationEffect = shape.Slide.Timeline.MainSequence.AddEffect(shape, AnimationEffectType.FadedSwivel)
'设置持续时间
effect.Timing.Duration = 2
'设置重复次数
effect.Timing.RepeatCount = 2
'设置动画文本效果
effect.IterateType = Spire.Presentation.Drawing.TimeLine.AnimateType.Letter
'设置动画文本字符之间延迟时间
effect.IterateTimeValue = 2
'保存文件
ppt.SaveToFile("SetAnimations.pptx", FileFormat.Pptx2010)
效果展示:
获取动画
C#
StringBuilder sb = new StringBuilder();
//加载PPT文件
Presentation ppt = new Presentation("SetAnimations.pptx",FileFormat.Pptx2010);
ISlide slide = ppt.Slides[0];
//获取动画集合
AnimationEffectCollection animations = slide.Timeline.MainSequence;
//获取动画的效果
AnimationEffectType effectType = animations[0].AnimationEffectType;
sb.Append("Animation type:" + effectType.ToString() + "\r\n");
//获取动画持续时间
float duration = animations[0].Timing.Duration;
sb.Append("Duration:" + duration.ToString() + "\r\n");
//获取动画重复次数
float count = animations[0].Timing.RepeatCount;
sb.Append("Repeat count:" + count + "\r\n");
//获取动画文本效果
var IterateType = animations[0].IterateType;
sb.Append("Iterate type:" + IterateType.ToString() + "\r\n");
//保存结果
File.WriteAllText("result.txt", sb.ToString());
VB.NET
Dim sb As New StringBuilder()
'加载PPT文件
Dim ppt As New Presentation("SetAnimations.pptx", FileFormat.Pptx2010)
Dim slide As ISlide = ppt.Slides(0)
'获取动画集合
Dim animations As AnimationEffectCollection = slide.Timeline.MainSequence
'获取动画的效果
Dim effectType As AnimationEffectType = animations(0).AnimationEffectType
sb.Append("Animation type:" + effectType.ToString() + vbCr & vbLf)
'获取动画持续时间
Dim duration As Single = animations(0).Timing.Duration
sb.Append("Duration:" + duration.ToString() + vbCr & vbLf)
'获取动画重复次数
Dim count As Single = animations(0).Timing.RepeatCount
sb.Append("Repeat count:" + count + vbCr & vbLf)
'获取动画文本效果
Dim IterateType = animations(0).IterateType
sb.Append("Iterate type:" + IterateType.ToString() + vbCr & vbLf)
'保存结果
File.WriteAllText("result.txt", sb.ToString())
结果: