How to Save Presentations in .NET

How to Save Presentations in .NET

Aspose.Slides FOSS for .NET saves presentations exclusively to .pptx format using prs.Save(path, SaveFormat.Pptx). This guide covers the correct save pattern, saving to a different path, saving to a stream, and common save-related errors.

Step-by-Step Guide

Step 1: Install the Package

dotnet add package Aspose.Slides.Foss

Step 2: Open or Create a Presentation

Always use a using statement. The save call must happen before the object is disposed.

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

// Create new
using var prs = new Presentation();
prs.Save("new.pptx", SaveFormat.Pptx);
// Open existing
using var prs = new Presentation("input.pptx");
prs.Save("output.pptx", SaveFormat.Pptx);

Step 3: Save After All Modifications

Place the Save() call after all modifications are complete, before the using scope ends.

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

using var prs = new Presentation();
var slide = prs.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 300, 100);
shape.AddTextFrame("Hello, World!");
prs.Save("output.pptx", SaveFormat.Pptx);

Step 4: Save to a Different Path

Pass a different output path to create a new file without modifying the original:

using var prs = new Presentation("template.pptx");
// modify ...
prs.Save("customized.pptx", SaveFormat.Pptx);

The template.pptx file is not modified; customized.pptx is created (or overwritten if it already exists).


Step 5: Save to a Stream

The Presentation.Save method also accepts a Stream:

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

using var prs = new Presentation();
using var stream = new MemoryStream();
prs.Save(stream, SaveFormat.Pptx);
byte[] pptxBytes = stream.ToArray();
Console.WriteLine($"Size: {pptxBytes.Length} bytes");

Step 6: Verify the Output

After saving to a file, check that it exists:

var info = new FileInfo("output.pptx");
Console.WriteLine($"Saved: {info.Exists}, size: {info.Length} bytes");

Supported Save Format

FormatEnum ValueSupported
PPTX (Office Open XML)SaveFormat.PptxYes
PDFN/ANo
HTMLN/ANo
SVGN/ANo
PNG / JPEGN/ANo
ODP (OpenDocument)N/ANo

Only PPTX is supported. Attempting to save in any other format will raise a NotSupportedException or an unsupported format error.


Common Issues and Fixes

IOException: The process cannot access the file

The output file is open in another application (e.g., PowerPoint has the file open). Close the file in other applications before saving.

File is created but appears empty or corrupted

Ensure prs.Save() is called before the object is disposed. After Dispose() runs, subsequent calls will fail or produce corrupt output.

NotSupportedException when saving

This occurs when attempting a save format other than PPTX, or when using an unsupported feature during save.


Frequently Asked Questions

Can I save to the same file I opened?

Yes. Saving to the same path overwrites the original file:

using var prs = new Presentation("deck.pptx");
// modify ...
prs.Save("deck.pptx", SaveFormat.Pptx);  // overwrites original

Does saving preserve content I haven’t modified?

Yes. Unknown XML parts from the original file are preserved verbatim. The library only serializes the parts of the document model it understands, and passes through any XML it does not recognize.


See Also