Encrypt and Protect Presentations with the DevExpress Presentation API
- 4 minutes to read
The DevExpress Presentation API allows you to protect and encrypt presentations, as well as work with those that are already protected or encrypted.
Encrypt a Presentation
When you encrypt a presentation, you also set a password that is required for presentation content access.
To encrypt a presentation, follow the steps below:
Create an EncryptionOptions object and pass a password and encryption type to the
EncryptionOptionsconstructor.Call the Presentation.Encrypt method and pass the created
EncryptionOptionsobject to it.
The following code snippet encrypts a presentation:
using DevExpress.Docs.Office;
using DevExpress.Docs.Presentation;
// Specify source and destination paths.
var inputPath = @"..\..\..\data\my-presentation.pptx";
var outputPath = @"D:\presentation.pptx";
// Read source presentation bytes.
byte[] sourceBytes = await File.ReadAllBytesAsync(inputPath);
// Encrypt the presentation.
Presentation presentation = new Presentation(sourceBytes);
presentation.Encrypt(new EncryptionOptions("Pa$$w0rd", EncryptionType.Strong));
// Save the encrypted presentation.
using FileStream outputStream = new(outputPath, FileMode.Create, FileAccess.Write, FileShare.None);
presentation.SaveDocument(outputStream, DocumentFormat.Pptx);
Load an Encrypted Presentation
Follow the steps below to load an encrypted presentation:
Create a LoadOptions object and pass it to the Presentation constructor.
Assign a password to the LoadOptions.Password property.
The Presentation API cannot determine the format of encrypted files, so you must also specify the LoadOptions.DocumentFormat property.
The following code snippet opens an encrypted presentation:
using DevExpress.Docs.Office;
using DevExpress.Docs.Presentation;
// Specify the path to the encrypted presentation.
var inputPath = @"D:\presentation.pptx";
// Read encrypted presentation bytes.
byte[] sourceBytes = await File.ReadAllBytesAsync(inputPath);
// Open the encrypted presentation and specify its format.
Presentation presentation = new Presentation(sourceBytes, new LoadOptions { Password = "Pa$$w0rd", DocumentFormat = DocumentFormat.Pptx });
Protect a Presentation from Editing
A write-protected presentation opens in read-only mode. To edit it, you must enter the correct password.
To enable write protection, follow the steps below:
Create a WriteProtectionOptions object and pass a password and hash algorithm to the
WriteProtectionOptionsconstructor.Pass the created
WriteProtectionOptionsobject to the Presentation.Protect method. TheProtectmethod makes the presentation read-only (the Presentation.ProtectionMode property value is set toDocumentProtectionMode.ReadOnly).
The following code snippet enables read-only protection for a presentation:
using DevExpress.Docs.Office;
using DevExpress.Docs.Presentation;
// Specify source and destination paths.
var inputPath = @"..\..\..\data\my-presentation.pptx";
// Read source presentation bytes.
byte[] sourceBytes = await File.ReadAllBytesAsync(inputPath);
// Load the presentation.
Presentation presentation = new Presentation(sourceBytes);
// Enable write protection.
presentation.Protect(new WriteProtectionOptions("Pa$$w0rd", HashAlgorithmType.Sha512));
var outputPath = @"D:\presentation.pptx";
// Save the protected presentation.
using FileStream outputStream = new(outputPath, FileMode.Create, FileAccess.Write, FileShare.None);
presentation.SaveDocument(outputStream);
Remove Encryption and Protection
Call Presentation.RemoveEncryption and Presentation.RemoveProtection methods to remove security settings.
The following code snippet removes encryption and write protection: