參考:https://blog.csdn.net/badaaasss/article/details/89188807 C#生成PPT
https://docs.microsoft.com/zh-cn/office/vba/api/powerpoint.animationsettings msdn 動畫處理
C#操作PPT 文本、圖片、動畫,代碼如下:(使用Microsoft.Office.Interop.PowerPoint庫)
using System; using System.Collections.Generic; using System.IO; using System.Text; using Microsoft.Office.Core; using Microsoft.Office; using Microsoft.Office.Interop.PowerPoint; namespace PPTConApp { class Program { static void Main(string[] args) { //創建PPT應用 Microsoft.Office.Interop.PowerPoint.Application PPT = new Microsoft.Office.Interop.PowerPoint.Application(); Microsoft.Office.Interop.PowerPoint.Presentation MyPres = null;//PPT應用的實例 Microsoft.Office.Interop.PowerPoint.Slide MySlide = null;//PPT中的幻燈片 //此處將一個PPT實例給了MyPres MyPres = PPT.Presentations.Open("D:\\Download\\2.pptx", MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoTrue); //像PPT實例中,添加一個空白頁,位置是“第一頁” MySlide = MyPres.Slides.Add(1, Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutBlank); //1、添加圖形(矩形) MySlide.Shapes.AddShape(MsoAutoShapeType.msoShapeRectangle, 8.5F, 6.5F, 705F, 525F); MySlide.Shapes[1].Line.ForeColor.RGB = 150 + 20 * 256 + 30 * 256 * 256;//改變線條顏色 MySlide.Shapes[1].Fill.Transparency = 1;//控制填充色為透明 MySlide.Shapes[1].Line.Style = MsoLineStyle.msoLineSingle;//改變線型里的復合類型 MySlide.Shapes[1].Line.Weight = 1F;//改變線粗細 MySlide.Shapes[1].Shadow.Style = MsoShadowStyle.msoShadowStyleOuterShadow;//控制陰影類型 MySlide.Shapes[1].Shadow.ForeColor.RGB = 0;//控制陰影顏色 MySlide.Shapes[1].Shadow.Transparency = 0.6F;//控制透明度 MySlide.Shapes[1].Shadow.Size = 100F;//控制大小 MySlide.Shapes[1].Shadow.Blur = 4F;//控制虛化 MySlide.Shapes[1].Shadow.OffsetX = 2.1F;//控制距離; MySlide.Shapes[1].Shadow.OffsetY = 2.1F;//與offsetX共同決定角度 //2、添加圖片 MySlide.Shapes.AddPicture(@"D:\workspace\workdotnet\2020TechSearch\reveal-ppt\images\zsylogo.png", MsoTriState.msoFalse, MsoTriState.msoTrue, 27F, 24F, 665F, 333F); //3、添加文本框 Microsoft.Office.Interop.PowerPoint.TextRange MyTextRng = null; MySlide.Shapes.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, 721.5F, 365F, 670F, 270F); MyTextRng = MySlide.Shapes[3].TextFrame.TextRange;//請注意此處Shapes的索引,由於文本框是第一個添加的Shapes,所以此處索引是1。 MyTextRng.Font.NameFarEast = "微軟雅黑";//文本框中,中文的字體 MyTextRng.Font.NameAscii = "Calibri";//文本框中,英文和數字的字體 MyTextRng.Text = "C#生成PPT";//顯示的內容 MyTextRng.Font.Bold = MsoTriState.msoTrue;//是否加粗 MyTextRng.Font.Color.RGB = 1 + 2 * 256 + 3 * 256 * 256;//字體顏色,其中ABC直接用自定義顏色中的數字代替即可。 MyTextRng.Characters(1, 10).Font.Size = 24;//個性化設計。第1個字符開始,長度為10的字符,字體大小是24. MyTextRng.ParagraphFormat.Alignment = Microsoft.Office.Interop.PowerPoint.PpParagraphAlignment.ppAlignLeft;//文本對齊方式(水平方向) MySlide.Shapes[3].TextFrame.VerticalAnchor = MsoVerticalAnchor.msoAnchorMiddle; //文本對齊方式(垂直方向) //4、文本框動畫處理:設置該文本由第一級段落從左邊時進行動畫處理,正在進行動畫處理后為指定的顏色變暗,並按相反的順序動畫顯示其項飛入動畫顯示。 MySlide.Shapes[3].AnimationSettings.TextLevelEffect = PpTextLevelEffect.ppAnimateByFirstLevel; MySlide.Shapes[3].AnimationSettings.EntryEffect = PpEntryEffect.ppEffectFlyFromLeft; MySlide.Shapes[3].AnimationSettings.AfterEffect = PpAfterEffect.ppAfterEffectDim; MySlide.Shapes[3].AnimationSettings.DimColor.RGB = 100 + 120 * 256 + 100 * 256 * 256; MySlide.Shapes[3].AnimationSettings.AnimateTextInReverse = MsoTriState.msoTrue ; //5、讀取動畫信息 Console.WriteLine("text animation info:" + MySlide.Shapes[3].AnimationSettings.TextLevelEffect + "\n" + MySlide.Shapes[3].AnimationSettings.EntryEffect + "\n" + MySlide.Shapes[3].AnimationSettings.AfterEffect + "\n" + MySlide.Shapes[3].AnimationSettings.DimColor.RGB + "\n" + MySlide.Shapes[3].AnimationSettings.AnimateTextInReverse); } } }