MS PowerPoint 演示文稿允许您创建包含文本、图像、图表、动画和其他元素的幻灯片。各种附加格式选项可让您的演示文稿更具吸引力。在这篇文章中,将了解如何以编程方式创建此类演示文稿,以及将学习如何使用 C# 创建包含文本、表格、图像和图表的 PPTX 演示文稿。
- 创建 PowerPoint 演示文稿
- 打开现有的 PowerPoint 演示文稿
- 将幻灯片添加到演示文稿
- 将文本添加到演示文稿的幻灯片
- 在演示文稿中创建表格
- 在演示文稿中创建图表
Aspose.Slides for .NET是一个演示操作 API,允许您从 .NET 应用程序中创建和操作 PowerPoint 文档。API 提供了实现基本和高级 PowerPoint 自动化功能所需的几乎所有可能的功能。
>>你可以下载Aspose.Slides 最新版测试体验。
在 C# 中创建 PowerPoint 演示文稿
让我们首先使用 Aspose.Slides for .NET 创建一个空的 PowerPoint 演示文稿。以下是执行此操作的步骤。
- 创建Presentation类的实例。
- 使用Presentation.Save(String, SaveFormat)方法将其保存为 PPTX 。
以下代码示例展示了如何在 C# 中创建 PowerPoint 演示文稿。
// Instantiate a Presentation object that represents a presentation file using (Presentation presentation = new Presentation()) { // Get the first slide ISlide slide = presentation.Slides[0]; // Add content to slide... // Save presentation presentation.Save("NewPresentation.pptx", SaveFormat.Pptx); }
在 C# 中打开现有的 PowerPoint 演示文稿
无需付出额外的努力即可打开现有的 PowerPoint 演示文稿。只需将 PPTX 文件的路径提供给Presentation类的构造函数即可完成。以下代码示例展示了如何打开现有的 PPTX 演示文稿。
// Opening the presentation file by passing the file path to the constructor of Presentation class Presentation pres = new Presentation("OpenPresentation.pptx"); // Printing the total number of slides in the presentation System.Console.WriteLine(pres.Slides.Count.ToString());
在 C# 中向演示文稿添加幻灯片
创建演示文稿后,您可以开始向其中添加幻灯片。以下是使用 Aspose.Slides for .NET 在演示文稿中添加幻灯片的步骤。
- 创建Presentation 类的实例。
- 通过设置对Presentations.Slides属性的引用来实例化 ISlideCollection类。
- 使用ISlideCollection对象公开的Slide.AddEmptySlide (ILayoutSlide)方法将空幻灯片添加到演示文稿 <