How to Create Presentations in .NET

How to Create Presentations in .NET

Aspose.Slides FOSS for .NET lets you create PowerPoint presentations entirely in C# with no dependency on Microsoft Office. This guide shows how to create a new presentation, add slides and shapes, format text, and save the result.

Step-by-Step Guide

Step 1: Install the Package

Install Aspose.Slides FOSS from NuGet. .NET 9.0 or later is required.

dotnet add package Aspose.Slides.Foss

Verify the installation:

using Aspose.Slides.Foss;

using var prs = new Presentation();
Console.WriteLine("Aspose.Slides FOSS ready");

No other system packages are required.


Step 2: Import the Required Namespaces

Add the namespaces needed for creating and saving presentations.

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;

All shape-type constants live in Aspose.Slides.Foss.ShapeType. All formatting types (FillType, NullableBool) are also in Aspose.Slides.Foss.


Step 3: Create a Presentation

Use new Presentation() with a using statement. A new presentation starts with one blank slide.

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;

using var prs = new Presentation();
Console.WriteLine($"Slides in new presentation: {prs.Slides.Count}");
// work with prs
prs.Save("output.pptx", SaveFormat.Pptx);

Important: Always use Presentation with a using statement. The class implements IDisposable and resources will not be released correctly without it.


Step 4: Access a Slide

The first slide is at index 0. A blank presentation has exactly one slide.

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;

using var prs = new Presentation();
var slide = prs.Slides[0];  // zero-based index
Console.WriteLine($"Slide at index 0: {slide}");
prs.Save("output.pptx", SaveFormat.Pptx);

Step 5: Add a Shape

Use slide.Shapes.AddAutoShape() to add an AutoShape. The parameters are (shapeType, x, y, width, height) all in points (1 point = 1/72 inch; standard slide is 720 x 540 pt).

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;

using var prs = new Presentation();
var slide = prs.Slides[0];

// Rectangle at (50, 50) with 400 wide and 120 tall
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 400, 120);

// Attach a text frame
shape.AddTextFrame("Hello from Aspose.Slides FOSS!");

prs.Save("with-shape.pptx", SaveFormat.Pptx);

Step 6: Save the Presentation

Call prs.Save(path, SaveFormat.Pptx) before the object is disposed. PPTX is the only supported output format.

prs.Save("result.pptx", SaveFormat.Pptx);

The file is written atomically; if an error occurs before this call, no output file is created.


Complete Working Example

The following program creates a two-slide presentation with a title shape on the first slide and a table on the second.

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Drawing;
using Aspose.Slides.Foss.Export;

using var prs = new Presentation();

// --- Slide 1: title shape ---
var slide1 = prs.Slides[0];
var title = slide1.Shapes.AddAutoShape(ShapeType.Rectangle, 40, 40, 640, 80);
var tf = title.AddTextFrame("Q1 Results: Executive Summary");
var fmt = tf.Paragraphs[0].Portions[0].PortionFormat;
fmt.FontHeight = 32;
fmt.FontBold = NullableBool.True;
fmt.FillFormat.FillType = FillType.Solid;
fmt.FillFormat.SolidFillColor.Color = Color.FromArgb(255, 0, 70, 127);

// --- Slide 2: table ---
prs.Slides.AddEmptySlide(prs.LayoutSlides[0]);
var slide2 = prs.Slides[1];
var table = slide2.Shapes.AddTable(40, 40,
    new double[] { 200.0, 120.0, 120.0 },
    new double[] { 40.0, 40.0, 40.0 });

string[] headers = { "Region", "Revenue", "Growth" };
string[][] data = {
    new[] { "North", "$1.2M", "+8%" },
    new[] { "South", "$0.9M", "+4%" },
};

for (int col = 0; col < headers.Length; col++)
    table.Rows[0][col].TextFrame.Text = headers[col];

for (int row = 0; row < data.Length; row++)
    for (int col = 0; col < data[row].Length; col++)
        table.Rows[row + 1][col].TextFrame.Text = data[row][col];

prs.Save("q1-results.pptx", SaveFormat.Pptx);
Console.WriteLine("Saved q1-results.pptx");

Common Issues and Fixes

ObjectDisposedException

You are using the Presentation object after the using block has ended. All work must happen before the object is disposed.

Missing namespace error for ShapeType

Ensure you have using Aspose.Slides.Foss; at the top of your file. ShapeType, NullableBool, FillType, and other enums are in this namespace.

NotSupportedException when saving

SaveFormat.Pptx is the only supported format. Attempting to save in any other format will raise an error.


Frequently Asked Questions

What is the default slide size?

A new Presentation() creates slides at the standard 10 x 7.5 inch (720 x 540 point) size. Changing the slide size is not yet supported in this edition.

Can I add more than one slide?

Yes. Call prs.Slides.AddEmptySlide(prs.LayoutSlides[0]) to append a blank slide and access it by index:

prs.Slides.AddEmptySlide(prs.LayoutSlides[0]);
var slide2 = prs.Slides[1];

Can I open an existing file and add slides?

Yes:

using var prs = new Presentation("existing.pptx");
prs.Slides.AddEmptySlide(prs.LayoutSlides[0]);
prs.Save("existing.pptx", SaveFormat.Pptx);

What formats can I save to?

Only SaveFormat.Pptx is supported. Export to PDF, HTML, SVG, or images is not available in this edition.


See Also