Open a Presentation in C#
Overview
Beyond creating PowerPoint presentations from scratch, Aspose.Slides also lets you open existing presentations. After loading a presentation, you can retrieve information about it, edit slide content, add new slides, remove existing ones, and more.
Open Presentations
To open an existing presentation, instantiate the Presentation class and pass the file path to its constructor.
The following C# example shows how to open a presentation and get its slide count:
// Instantiate the Presentation class and pass a file path to its constructor.
using (Presentation presentation = new Presentation("Sample.pptx"))
{
// Print the total number of slides in the presentation.
System.Console.WriteLine(presentation.Slides.Count);
}
Open Password-Protected Presentations
When you need to open a password-protected presentation, pass the password through the Password property of the LoadOptions class to decrypt and load it. The following C# code demonstrates this operation:
LoadOptions loadOptions = new LoadOptions {Password = "YOUR_PASSWORD"};
using (Presentation presentation = new Presentation("Sample.pptx", loadOptions))
{
// Perform operations on the decrypted presentation.
}
Open Large Presentations
Aspose.Slides provides options—particularly the BlobManagementOptions property in the LoadOptions class—to help you load large presentations.
The following C# code demonstrates loading a large presentation (for example, 2 GB):
const string filePath = "LargePresentation.pptx";
LoadOptions loadOptions = new LoadOptions
{
BlobManagementOptions =
{
// Choose the KeepLocked behavior—the presentation file will remain locked for the lifetime of
// the Presentation instance, but it does not need to be loaded into memory or copied to a temporary file.
PresentationLockingBehavior = PresentationLockingBehavior.KeepLocked,
IsTemporaryFilesAllowed = true,
MaxBlobsBytesInMemory = 10 * 1024 * 1024 // 10 MB
}
};
using (Presentation presentation = new Presentation(filePath, loadOptions))
{
// The large presentation has been loaded and can be used, while memory consumption remains low.
// Make changes to the presentation.
presentation.Slides[0].Name = "Large presentation";
// Save the presentation to another file. Memory consumption remains low during this operation.
presentation.Save("LargePresentation-copy.pptx", SaveFormat.Pptx);
// Don't do this! An I/O exception will be thrown because the file is locked until the presentation object is disposed.
File.Delete(filePath);
}
// It is OK to do it here. The source file is no longer locked by the presentation object.
File.Delete(filePath);
Info
To work around certain limitations when working with streams, Aspose.Slides may copy a stream’s contents. Loading a large presentation from a stream causes the presentation to be copied and can slow loading. Therefore, when you need to load a large presentation, we strongly recommend using the presentation file path rather than a stream.
When creating a presentation that contains large objects (video, audio, high-resolution images, etc.), you can use BLOB management to reduce memory consumption.
Control External Resources
Aspose.Slides provides the IResourceLoadingCallback interface that lets you manage external resources. The following C# code shows how to use the IResourceLoadingCallback
interface:
LoadOptions loadOptions = new LoadOptions();
loadOptions.ResourceLoadingCallback = new ImageLoadingHandler();
Presentation presentation = new Presentation("Sample.pptx", loadOptions);
public class ImageLoadingHandler : IResourceLoadingCallback
{
public ResourceLoadingAction ResourceLoading(IResourceLoadingArgs args)
{
if (args.OriginalUri.EndsWith(".jpg"))
{
try
{
// Load a substitute image.
byte[] imageData = File.ReadAllBytes("aspose-logo.jpg");
args.SetData(imageData);
return ResourceLoadingAction.UserProvided;
}
catch (Exception)
{
return ResourceLoadingAction.Skip;
}
}
else if (args.OriginalUri.EndsWith(".png"))
{
// Set a substitute URL.
args.Uri = "http://www.google.com/images/logos/ps_logo2.png";
return ResourceLoadingAction.Default;
}
// Skip all other images.
return ResourceLoadingAction.Skip;
}
}
Load Presentations Without Embedded Binary Objects
A PowerPoint presentation can contain the following types of embedded binary objects:
- VBA project (accessible via IPresentation.VbaProject);
- OLE object embedded data (accessible via IOleEmbeddedDataInfo.EmbeddedFileData);
- ActiveX control binary data (accessible via IControl.ActiveXControlBinary).
Using the ILoadOptions.DeleteEmbeddedBinaryObjects property, you can load a presentation without any embedded binary objects.
This property is useful for removing potentially malicious binary content. The following C# code demonstrates how to load a presentation without any embedded binary content:
LoadOptions loadOptions = new LoadOptions()
{
DeleteEmbeddedBinaryObjects = true
}
using (Presentation presentation = new Presentation("malware.ppt", loadOptions))
{
// Perform operations on the presentation.
}