Open Presentation in C#
Besides creating PowerPoint presentations from scratch, Aspose.Slides allows you to open existing presentations. After you load a presentation, you can get information about the presentation, edit the presentation (content on its slides), add new slides or remove existing ones, etc.
Open Presentation
To open an existing presentation, you simply have to instantiate the Presentation class and pass the file path (to the presentation you want to open) to its constructor.
This C# code shows you how to open a presentation and also find out the number of slides it contains:
// Instantiates the Presentation class and passes the file path to its constructor
Presentation pres = new Presentation("OpenPresentation.pptx");
// Prints the total number of slides present in the presentation
System.Console.WriteLine(pres.Slides.Count.ToString());
Open Password-Protected Presentation
When you have to open a password-protected presentation, you can pass the password through the Password property (from the LoadOptions class) to decrypt the presentation and load the presentation. This C# code demonstrates the operation:
LoadOptions loadOptions = new LoadOptions {Password = "YOUR_PASSWORD"};
using (Presentation presentation = new Presentation("pres.pptx", loadOptions))
{
// Do some work with the decrypted presentation
}
Open Large Presentation
Aspose.Slides provides options (the BlobManagementOptions property in particular) under the LoadOptions class to allow you to load large presentations.
This C# demonstrates an operation in which a large presentation (say 2GB in size) is loaded:
const string pathToVeryLargePresentationFile = "veryLargePresentation.pptx";
LoadOptions loadOptions = new LoadOptions
{
BlobManagementOptions = {
// Let's choose the KeepLocked behavior - the "veryLargePresentation.pptx" will be locked for
// the Presentation's instance lifetime, but we don't need to load it into memory or copy into
// the temporary file
PresentationLockingBehavior = PresentationLockingBehavior.KeepLocked,
}
};
using (Presentation pres = new Presentation(pathToVeryLargePresentationFile, loadOptions))
{
// The large presentation has been loaded and can be used, but the memory consumption is still low.
// Makes changes to the presentation.
pres.Slides[0].Name = "Very large presentation";
// The presentation will be saved to the other file. The memory consumption stays low during the operation
pres.Save("veryLargePresentation-copy.pptx", SaveFormat.Pptx);
// can't do that! IO exception will be thrown, because the file is locked while pres objects will
// not be disposed
File.Delete(pathToVeryLargePresentationFile);
}
// It is ok to do it here, the source file is not locked by pres object
File.Delete(pathToVeryLargePresentationFile);
Info
To circumvent certain limitations when interacting with streams, Aspose.Slides may copy the stream’s content. Loading a large presentation through its stream will result in the copying of the presentation’s contents and cause slow loading. Therefore, when you intend to load a large presentation, we strongly recommend that you use the presentation file path and not its stream.
When you want to create a presentation that contains large objects (video, audio, big images, etc.), you can use the Blob facility to reduce memory consumption.
Load Presentation
Aspose.Slides provides IResourceLoadingCallback with a single method to allow you to manage external resources. This C# code shows you how to use the IResourceLoadingCallback
interface:
LoadOptions opts = new LoadOptions();
opts.ResourceLoadingCallback = new ImageLoadingHandler();
Presentation presentation = new Presentation("presentation.pptx", opts);
public class ImageLoadingHandler : IResourceLoadingCallback
{
public ResourceLoadingAction ResourceLoading(IResourceLoadingArgs args)
{
if (args.OriginalUri.EndsWith(".jpg"))
{
try // Loads substitute image
{
byte[] imageBytes = File.ReadAllBytes("c:\\aspose-logo.jpg");
args.SetData(imageBytes);
return ResourceLoadingAction.UserProvided;
}
catch (Exception)
{
return ResourceLoadingAction.Skip;
}
}
else if (args.OriginalUri.EndsWith(".png"))
{
// Sets substitute url
args.Uri = "http://www.google.com/images/logos/ps_logo2.png";
return ResourceLoadingAction.Default;
}
// Skips all other images
return ResourceLoadingAction.Skip;
}
}
Load Presentation Without Embedded Binary Objects
The PowerPoint presentation can contain the following types of the embedded binary objects:
- VBA Project (IPresentation.VbaProject);
- OLE Object embedded data (IOleEmbeddedDataInfo.EmbeddedFileData);
- ActiveX Control binary data (IControl.ActiveXControlBinary);
Using the ILoadOptions.DeleteEmbeddedBinaryObjects property, you can load the presentation without any embedded binary objects.
This property can be useful for removing potentially malicious binary content.
The C# code demonstrates how to load and save a presentation without any malware content:
LoadOptions loadOptions = new LoadOptions()
{
DeleteEmbeddedBinaryObjects = true
}
using (var pres = new Presentation("malware.ppt", loadOptions))
{
pres.Save("clean.ppt", SaveFormat.Ppt);
}
Open and Save Presentation
Steps: Open and Save Presentation in C#
- Create an instance of the Presentation class and pass the file you want to open.
- Save the Presentation.
// Loads any supported presentation e.g ppt, pptx, odp
Presentation presentation = new Presentation("Sample.odp");
presentation.Save("OutputPresenation.pptx", SaveFormat.Pptx);