How to Load Presentations in .NET
Aspose.Slides FOSS for .NET lets you open any .pptx file, inspect its content, and either save it back to PPTX or extract data from it. This guide covers opening a file, iterating slides, reading shape text, and round-tripping the save.
Step-by-Step Guide
Step 1: Install the Package
dotnet add package Aspose.Slides.FossStep 2: Open an Existing Presentation
Pass the file path to new Presentation(). Use a using statement to ensure cleanup.
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation("input.pptx");
Console.WriteLine($"Slide count: {prs.Slides.Count}");
prs.Save("output.pptx", SaveFormat.Pptx);Unknown XML parts in the source file are preserved verbatim: the library never removes content it does not yet understand.
Step 3: Inspect Slides
Iterate over all slides and print their index:
using Aspose.Slides.Foss;
using var prs = new Presentation("deck.pptx");
for (int i = 0; i < prs.Slides.Count; i++)
{
var slide = prs.Slides[i];
int shapeCount = slide.Shapes.Count;
Console.WriteLine($"Slide {i}: {shapeCount} shapes");
}Step 4: Read Shape Text
Iterate over shapes and read text from shapes that have a TextFrame:
using Aspose.Slides.Foss;
using var prs = new Presentation("deck.pptx");
foreach (var slide in prs.Slides)
{
foreach (var shape in slide.Shapes)
{
if (shape is IAutoShape autoShape && autoShape.TextFrame != null)
{
string text = autoShape.TextFrame.Text;
if (!string.IsNullOrWhiteSpace(text))
Console.WriteLine($" Shape text: {text}");
}
}
}Step 5: Read Document Properties
Access core document properties from prs.DocumentProperties:
using Aspose.Slides.Foss;
using var prs = new Presentation("deck.pptx");
var props = prs.DocumentProperties;
Console.WriteLine($"Title: {props.Title}");
Console.WriteLine($"Author: {props.Author}");
Console.WriteLine($"Subject: {props.Subject}");Step 6: Round-Trip Save
After inspecting or modifying the presentation, save it back to PPTX:
prs.Save("output.pptx", SaveFormat.Pptx);Saving to a different path creates a new file. Saving to the same path overwrites the original.
Common Issues and Fixes
FileNotFoundException
Check that the path to the .pptx file is correct relative to the working directory. Use Path.Combine for robust path construction:
string path = Path.Combine(AppContext.BaseDirectory, "assets", "deck.pptx");
using var prs = new Presentation(path);Exception: File format is not supported
The library supports .pptx (Office Open XML) only. Legacy .ppt (binary PowerPoint 97-2003) files are not supported.
Shapes do not have a TextFrame property
Some shapes (Connector, PictureFrame) do not have a TextFrame. Cast to IAutoShape and check for null before accessing text.
Frequently Asked Questions
Does loading preserve all original content?
Yes. Unknown XML parts are preserved verbatim on round-trip save. The library will not remove any XML content it does not yet recognize.
Can I load a password-protected PPTX?
Password-protected (encrypted) presentations are not supported in this edition.
Can I extract embedded images?
Access the images collection: prs.Images returns the ImageCollection. Each image has properties to read the raw image data.
Is loading from a MemoryStream supported?
Yes. The Presentation constructor accepts a Stream:
using var stream = new MemoryStream(pptxBytes);
using var prs = new Presentation(stream);
Console.WriteLine($"Slides: {prs.Slides.Count}");