您好,登錄后才能下訂單哦!
小編給大家分享一下C# PowerPoint中如何添加、修改和刪除動畫,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
為了讓PowerPoint文檔更加精美,在制作文檔的過程中,我們通常會給PowerPoint文檔中的元素如形狀、圖片、表格等添加動畫。本文將介紹如何使用.NET PowerPoint組件Spire.Presentation和C#編程的方式給PowerPoint中的形狀添加動畫,并修改和刪除現有動畫。
在開始前我們可以了解一下PowerPoint中的動畫。在PowerPoint中,動畫大致可分為以下四大類:
1. 進入
2. 強調
3. 退出
4. 動作路徑
其中進入、強調和退出類型下有多種不同的預設動畫效果,有些動畫效果還可以添加子效果,例如“進入”分類下的“隨機線條”動畫效果,可以設置水平方向或垂直方向子效果(默認為水平方向)。下圖展示了如何在PowerPoint中添加動畫:
如何在Spire.Presentation中添加動畫
Spire.Presentation支持約151種動畫效果(可以在AnimationEffectType枚舉中查看),這些動畫效果及其所屬分類和子效果,請參見文末附表。
在使用以下代碼前,需要下載Spire.Presentation安裝, 并從安裝路徑下引用Spire.Presentation.dll到工程中(或可從NuGet搜索Spire.Presentation并安裝)。
添加動畫
//加載文檔 Presentation ppt = new Presentation(); ppt.LoadFromFile("Input.pptx"); //獲取第一張幻燈片 ISlide slide = ppt.Slides[0]; RectangleF rect = new RectangleF(50, 200, 200, 200); //添加形狀到幻燈片 IShape cubeShape = slide.Shapes.AppendShape(ShapeType.Cube, rect); //給形狀添加動畫效果 AnimationEffectCollection sequence = slide.Timeline.MainSequence; AnimationEffect effect = sequence.AddEffect(cubeShape, AnimationEffectType.Bounce); //保存文檔 ppt.SaveToFile("Output.pptx", FileFormat.Pptx2013);
通常我們添加的動畫效果默認是“進入”效果,下面的代碼將介紹如何添加“退出”動畫效果:
//加載文檔 Presentation ppt = new Presentation(); ppt.LoadFromFile("Input.pptx"); //獲取第一張幻燈片 ISlide slide = ppt.Slides[0]; RectangleF rect = new RectangleF(50, 200, 200, 200); //添加形狀到幻燈片 IShape cubeShape = slide.Shapes.AppendShape(ShapeType.Cube, rect); //給形狀添加動畫效果 AnimationEffectCollection sequence = slide.Timeline.MainSequence; AnimationEffect effect = sequence.AddEffect(cubeShape, AnimationEffectType.RandomBars); //將動畫效果從默認的“進入”效果改為“退出”效果 effect.PresetClassType = TimeNodePresetClassType.Exit; //給該動畫添加子效果 effect.Subtype = AnimationEffectSubtype.Vertical; //保存文檔 ppt.SaveToFile("ExitAnimationEffect.pptx", FileFormat.Pptx2013);
修改動畫
對文檔中的現有動畫,我們可以對其進行修改。下面的代碼將介紹如何修改現有動畫的類型和持續時間。
修改動畫類型
//加載文檔 Presentation ppt = new Presentation(); ppt.LoadFromFile("Output.pptx"); //獲取第一張幻燈片 ISlide slide = ppt.Slides[0]; //修改第一個動畫的類型 AnimationEffectCollection sequence = slide.Timeline.MainSequence; sequence[0].AnimationEffectType = AnimationEffectType.GrowAndTurn; //保存文本 ppt.SaveToFile("EditAnimationType.pptx", FileFormat.Pptx2013);
修改持續時間
//加載文檔 Presentation ppt = new Presentation(); ppt.LoadFromFile("Output.pptx"); //獲取第一張幻燈片 ISlide slide = ppt.Slides[0]; //修改第一個動畫的持續時間 AnimationEffectCollection sequence = slide.Timeline.MainSequence; sequence[0].Timing.Duration = 5; //保存文檔 ppt.SaveToFile("EditAnimationTime.pptx", FileFormat.Pptx2013);
刪除動畫
//加載文檔 Presentation ppt = new Presentation(); ppt.LoadFromFile("Output.pptx"); //獲取第一張幻燈片 ISlide slide = ppt.Slides[0]; //刪除第一個動畫 AnimationEffectCollection sequence = slide.Timeline.MainSequence; sequence.RemoveAt(0); //保存文檔 ppt.SaveToFile("RemoveAnimation.pptx", FileFormat.Pptx2013);
附表(Spire.Presentation中動畫效果及其所屬分類和子效果):
動畫效果 | 分類 | 子效果 |
Appear | Entrance or Exit | EffectSubtype.None |
CurveUpDown | Entrance or Exit | EffectSubtype.None |
Ascend | Entrance or Exit | EffectSubtype.None |
Blast | Emphasis | EffectSubtype.None |
Blinds | Entrance or Exit | · EffectSubtype.Horizontal · EffectSubtype.Vertical |
Blink | Emphasis | EffectSubtype.None |
BoldFlash | Emphasis | EffectSubtype.None |
BoldReveal | Emphasis | EffectSubtype.None |
Boomerang | Entrance or Exit | EffectSubtype.None |
Bounce | Entrance or Exit | EffectSubtype.None |
Box | Entrance or Exit | · EffectSubtype.In · EffectSubtype.Out |
BrushOnColor | Emphasis | EffectSubtype.None |
BrushOnUnderline | Emphasis | EffectSubtype.None |
CenterRevolve | Entrance or Exit | EffectSubtype.None |
ChangeFillColor | Emphasis | · EffectSubtype.Instant · EffectSubtype.Gradual · EffectSubtype.GradualAndCycleClockwise · EffectSubtype.GradualAndCycleCounterClockwise |
ChangeFont | Emphasis | · EffectSubtype.Instant · EffectSubtype.Gradual |
ChangeFontColor | Emphasis | · EffectSubtype.Instant · EffectSubtype.Gradual · EffectSubtype.GradualAndCycleClockwise · EffectSubtype.GradualAndCycleCounterClockwise |
ChangeFontSize | Emphasis | · EffectSubtype.Instant · EffectSubtype.Gradual |
ChangeFontStyle | Emphasis | · EffectSubtype.FontBold · EffectSubtype.FontItalic · EffectSubtype.FontUnderline |
ChangeLineColor | Emphasis | · EffectSubtype.Instant · EffectSubtype.Gradual · EffectSubtype.GradualAndCycleClockwise · EffectSubtype.GradualAndCycleCounterClockwise |
Checkerboard | Entrance or Exit | · EffectSubtype.Vertical · EffectSubtype.Across |
Circle | Entrance or Exit | · EffectSubtype.In · EffectSubtype.Out |
ColorBlend | Emphasis | EffectSubtype.None |
ColorTypewriter | Entrance or Exit | EffectSubtype.None |
ColorWave | Emphasis | EffectSubtype.None |
ComplementaryColor | Emphasis | EffectSubtype.None |
ComplementaryColor2 | Emphasis | EffectSubtype.None |
Compress | Entrance or Exit | EffectSubtype.None |
ContrastingColor | Emphasis | EffectSubtype.None |
Crawl | Entrance or Exit | · EffectSubtype.Right · EffectSubtype.Left · EffectSubtype.Top · EffectSubtype.Bottom |
Credits | Entrance or Exit | EffectSubtype.None |
Custom | - | - |
Darken | Emphasis | EffectSubtype.None |
Desaturate | Emphasis | EffectSubtype.None |
Descend | Entrance or Exit | EffectSubtype.None |
Diamond | Entrance or Exit | · EffectSubtype.In · EffectSubtype.Out |
Dissolve | Entrance or Exit | EffectSubtype.None |
EaseInOut | Entrance or Exit | EffectSubtype.None |
Expand | Entrance or Exit | EffectSubtype.None |
Fade | Entrance or Exit | EffectSubtype.None |
FadedSwivel | Entrance or Exit | EffectSubtype.None |
FadedZoom | Entrance or Exit | · EffectSubtype.None · EffectSubtype.Center |
FlashBulb | Emphasis | EffectSubtype.None |
FlashOnce | Entrance or Exit | EffectSubtype.None |
Flicker | Emphasis | EffectSubtype.None |
Flip | Entrance or Exit | EffectSubtype.None |
Float | Entrance or Exit | EffectSubtype.None |
Fly | Entrance or Exit | · EffectSubtype.Right · EffectSubtype.Left · EffectSubtype.Top · EffectSubtype.Bottom · EffectSubtype.TopLeft · EffectSubtype.TopRight · EffectSubtype.BottomLeft · EffectSubtype.BottomRight |
Fold | Entrance or Exit | EffectSubtype.None |
Glide | Entrance or Exit | EffectSubtype.None |
GrowAndTurn | Entrance or Exit | EffectSubtype.None |
GrowShrink | Emphasis | EffectSubtype.None |
GrowWithColor | Emphasis | EffectSubtype.None |
Lighten | Emphasis | EffectSubtype.None |
LightSpeed | Entrance or Exit | EffectSubtype.None |
Path5PointStar | Path | EffectSubtype.None |
Path6PointStar | Path | EffectSubtype.None |
Path7PointStar | Path | EffectSubtype.None |
Path8PointStar | Path | EffectSubtype.None |
PathArcDown | Path | EffectSubtype.None |
PathArcLeft | Path | EffectSubtype.None |
PathArcRight | Path | EffectSubtype.None |
PathArcUp | Path | EffectSubtype.None |
PathBean | Path | EffectSubtype.None |
PathBounceLeft | Path | EffectSubtype.None |
PathBounceRight | Path | EffectSubtype.None |
PathBuzzsaw | Path | EffectSubtype.None |
PathCircle | Path | EffectSubtype.None |
PathCrescentMoon | Path | EffectSubtype.None |
PathCurvedSquare | Path | EffectSubtype.None |
PathCurvedX | Path | EffectSubtype.None |
PathCurvyLeft | Path | EffectSubtype.None |
PathCurvyRight | Path | EffectSubtype.None |
PathCurvyStar | Path | EffectSubtype.None |
PathDecayingWave | Path | EffectSubtype.None |
PathDiagonalDownRight | Path | EffectSubtype.None |
PathDiagonalUpRight | Path | EffectSubtype.None |
PathDiamond | Path | EffectSubtype.None |
PathDown | Path | EffectSubtype.None |
PathEqualTriangle | Path | EffectSubtype.None |
PathFigure8Four | Path | EffectSubtype.None |
PathFootball | Path | EffectSubtype.None |
PathFunnel | Path | EffectSubtype.None |
PathHeart | Path | EffectSubtype.None |
PathHeartbeat | Path | EffectSubtype.None |
PathHexagon | Path | EffectSubtype.None |
PathHorizontalFigure8 | Path | EffectSubtype.None |
PathInvertedSquare | Path | EffectSubtype.None |
PathInvertedTriangle | Path | EffectSubtype.None |
PathLeft | Path | EffectSubtype.None |
PathLoopdeLoop | Path | EffectSubtype.None |
PathNeutron | Path | EffectSubtype.None |
PathOctagon | Path | EffectSubtype.None |
PathParallelogram | Path | EffectSubtype.None |
PathPeanut | Path | EffectSubtype.None |
PathPentagon | Path | EffectSubtype.None |
PathPlus | Path | EffectSubtype.None |
PathPointyStar | Path | EffectSubtype.None |
PathRight | Path | EffectSubtype.None |
PathRightTriangle | Path | EffectSubtype.None |
PathSCurve1 | Path | EffectSubtype.None |
PathSCurve2 | Path | EffectSubtype.None |
PathSineWave | Path | EffectSubtype.None |
PathSpiralLeft | Path | EffectSubtype.None |
PathSpiralRight | Path | EffectSubtype.None |
PathSpring | Path | EffectSubtype.None |
PathSquare | Path | EffectSubtype.None |
PathStairsDown | Path | EffectSubtype.None |
PathSwoosh | Path | EffectSubtype.None |
PathTeardrop | Path | EffectSubtype.None |
PathTrapezoid | Path | EffectSubtype.None |
PathTurnDown | Path | EffectSubtype.None |
PathTurnRight | Path | EffectSubtype.None |
PathTurnUp | Path | EffectSubtype.None |
PathTurnUpRight | Path | EffectSubtype.None |
PathUp | Path | EffectSubtype.None |
PathUser | Path | EffectSubtype.None |
PathVerticalFigure8 | Path | EffectSubtype.None |
PathWave | Path | EffectSubtype.None |
PathZigzag | Path | EffectSubtype.None |
Peek | Entrance or Exit | · EffectSubtype.Bottom · EffectSubtype.Left · EffectSubtype.Right · EffectSubtype.Top |
Pinwheel | Entrance or Exit | EffectSubtype.None |
Plus | Entrance or Exit | · EffectSubtype.In · EffectSubtype.Out |
RandomBars | Entrance or Exit | · EffectSubtype.Horizontal · EffectSubtype.Vertical |
RandomEffects | Entrance or Exit | EffectSubtype.None |
RiseUp | Entrance | EffectSubtype.None |
Shimmer | Emphasis | EffectSubtype.None |
Sling | Entrance or Exit | EffectSubtype.None |
Spin | Emphasis | EffectSubtype.None |
Spinner | Emphasis | EffectSubtype.None |
Spiral | Entrance or Exit | EffectSubtype.None |
Split | Entrance or Exit | · EffectSubtype.HorizontalIn · EffectSubtype.HorizontalOut · EffectSubtype.VerticalIn · EffectSubtype.VerticalOut |
Stretch | Entrance or Exit | · EffectSubtype.Right · EffectSubtype.Left · EffectSubtype.Top · EffectSubtype.Bottom · EffectSubtype.Across |
Strips | Entrance or Exit | · EffectSubtype.UpLeft · EffectSubtype.UpRight · EffectSubtype.DownLeft · EffectSubtype.DownRight |
StyleEmphasis | Emphasis | EffectSubtype.None |
Swish | Entrance or Exit | EffectSubtype.None |
Swivel | Entrance or Exit | · EffectSubtype.Horizontal · EffectSubtype.Vertical |
Teeter | Emphasis | EffectSubtype.None |
Thread | Emphasis | EffectSubtype.None |
Transparency | Emphasis | EffectSubtype.None |
Unfold | Entrance or Exit | EffectSubtype.None |
VerticalGrow | Emphasis | EffectSubtype.None |
Wave | Emphasis | EffectSubtype.None |
Wedge | Entrance or Exit | EffectSubtype.None |
Wheel | Entrance or Exit | · EffectSubtype.Wheel1 · EffectSubtype.Wheel2 · EffectSubtype.Wheel3 · EffectSubtype.Wheel4 · EffectSubtype.Wheel8 |
Whip | Entrance or Exit | EffectSubtype.None |
Wipe | Entrance or Exit | · EffectSubtype.Top · EffectSubtype.Right · EffectSubtype.Bottom · EffectSubtype.Left |
Magnify | Entrance or Exit | EffectSubtype.None |
Zoom | Entrance or Exit | · EffectSubtype.In · EffectSubtype.Out · EffectSubtype.InCenter - only for Entrance type · EffectSubtype.OutBottom - only for Entrance type · EffectSubtype.OutSlightly · EffectSubtype.InSlightly · EffectSubtype.OutCenter - only for Exit type · EffectSubtype.InBottom - only for Exit type |
注:Entrance表示“進入”, Exit表示“退出”,Emphasis表示“強調”,Path表示“動作路徑”;EffectSubtype.None表示該效果無子效果。
以上是“C# PowerPoint中如何添加、修改和刪除動畫”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。