MS PowerPoint presentations allow you to create slide shows containing text, images, charts, animations, and other elements. Various additional formatting options let you make your presentations more appealing. In this post, you will come to know how to create such presentations programmatically. You will learn how to create PPTX presentations having text, tables, images, and charts using C#.
- C# PowerPoint API
- Create a PowerPoint Presentation
- Open an Existing PowerPoint Presentation
- Add Slide to a Presentation
- Add Text to Presentation’s Slide
- Create a Table in Presentation
- Create a Chart in Presentation
- Add an Image in Presentation
C# PowerPoint API – Free Download
Aspose.Slides for .NET is a presentation manipulation API that lets you create and manipulate PowerPoint documents from within your .NET applications. The API provides nearly all possible features required to implement basic as well as advanced PowerPoint automation features. You can either download the API or install it via NuGet.
Install-Package Aspose.Slides.NET
Create a PowerPoint Presentation in C#
Let’s start by creating an empty PowerPoint presentation using Aspose.Slides for .NET. The following are the steps to do so.
- Create an instance of the Presentation class.
- Save it as PPTX using Presentation.Save(String, SaveFormat) method.
The following code sample shows how to create a PowerPoint presentation in C#.
Open an Existing PowerPoint Presentation in C#
You don’t need to put extra efforts in order to open an existing PowerPoint presentation. Simply provide the path of the PPTX file to the constructor of the Presentation class and you are done. The following code sample shows how to open an existing PPTX presentation.
Add a Slide to Presentation in C#
Once you have created the presentation, you can start adding the slides to it. The following are the steps to add a slide in the presentation using Aspose.Slides for .NET.
- Create an instance of the Presentation class.
- Instantiate ISlideCollection class by setting a reference to the Presentations.Slides property.
- Add an empty slide to the presentation using Slide.AddEmptySlide(ILayoutSlide) method exposed by ISlideCollection object
- Save the presentation file using the Presentation.Save(String, SaveFormat) method.
The following code sample shows how to add a slide in a PowerPoint presentation using C#.
Insert Text in a Slide using C#
Now we can add content to the slides in the PowerPoint presentation. Let’s first add a piece of text to the slide using the following steps.
- Create a new presentation using the Presentation class.
- Obtain the reference of the slide in the presentation.
- Add an IAutoShape with ShapeType as Rectangle at a specified position of the slide.
- Obtain the reference of that newly added IAutoShape object.
- Add a TextFrame to the AutoShape containing the default text.
- Save the presentation as a PPTX file.
The following code sample shows how to add text to slide in a presentation using C#.
Create Table in a Presentation using C#
Aspose.Slides for .NET provides an easy way to create a table in the presentation document. The following are the steps for it.
- Create an instance of the Presentation class.
- Obtain the reference of a slide by using its index.
- Define arrays of columns with width and rows with height.
- Add a table to the slide using Slide.Shapes.AddTable() method exposed by IShapes object and get the reference to the table in ITable instance.
- Iterate through each cell to apply the formatting.
- Add text to the cells using Table.Rows[][].TextFrame.Text property.
- Save the presentation as a PPTX file.
The following code sample shows how to create table in a slide of PowerPoint presentation.
Create Chart in a Presentation using C#
The following are the steps to add a chart in PowerPoint presentation using C#.
- Create an instance of the Presentation class.
- Obtain the reference of a slide by index.
- Add a chart with the desired type using ISlide.Shapes.AddChart(ChartType, Single, Single, Single, Single) method.
- Add a chart title.
- Access the chart data worksheet.
- Clear all the default series and categories.
- Add new series and categories.
- Add new chart data for chart series.
- Set fill color for chart series.
- Add chart series labels.
- Save the presentation as a PPTX file.
The following code sample shows how to add chart in a presentation using C#.
Learn more about the presentation charts here.
Add an Image in Presentation
The following are the steps to add images in the presentation slide.
- Create a new presentation using Presentation class.
- Read SVG image using File.ReadAllText(String path) method.
- Add image to slide using Presentation.Slides[0].Shapes.AddPictureFrame(ShapeType shapeType, float x, float y,float width, float height, IPPImage image) method.
- Save the presentation.
The following code sample shows how to add image to a presentation in C#.
TIP: Besides the creation of slides or presentations, Aspose.Slides provides many features that allow you to work with presentations. For example, using its own APIs, Aspose developed a free online viewer for Microsoft PowerPoint Presentations.
Conclusion
In this article, you have learned how to create PowerPoint presentations from scratch using C#. In addition, you have seen how to add slides, text, tables, images, and charts in new or existing PPTX presentations. You can learn more about the API using the documentation.
See Also
- Protect PowerPoint PPTX Presentations using C#
- Create MS PowerPoint Presentations in ASP.NET
- Convert PowerPoint PPTX/PPT to PNG Images in C#
- Set Slide Background in PowerPoint Presentations using C#
- Generate Thumbnails for PowerPoint PPTX or PPT using C#
- Apply Animation to Text in PowerPoint using C#
- Split PowerPoint Presentations using C#