Write-Protect Presentations in .NET
Introduction
A write-protection password restricts modification of a presentation but does not encrypt its content. Users can load and view a write-protected presentation without the password. Depending on the application, they may also be able to edit the content and save it under a different name, so write protection should not be treated as a confidentiality mechanism.
An opening password serves a different purpose: it encrypts the presentation and is required to load its content. To encrypt a presentation or validate an opening password, see Password-Protect Presentations.
The workflows in this article apply to both PPT and PPTX presentations. The examples use PPTX files; when saving to PPT, use the .ppt extension and the corresponding PPT save format.
Set Write Protection on a Presentation
Use IProtectionManager.SetWriteProtection to assign a password for modifying a presentation. Saving the presentation persists the protection setting.
The following example sets write protection on a PPTX presentation:
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation("pres.pptx");
presentation.ProtectionManager.SetWriteProtection("modify_password");
presentation.Save("write-protected-pres.pptx", SaveFormat.Pptx);
Load a Write-Protected Presentation
Because write protection does not encrypt presentation content, no password is required to load the presentation. The password is relevant only when validating authorization to modify the protected presentation.
using System;
using Aspose.Slides;
using var presentation = new Presentation("write-protected-pres.pptx");
Console.WriteLine("Slide count: " + presentation.Slides.Count);
Do not pass a write-protection password to LoadOptions.Password. That property accepts an opening password for encrypted content. If a presentation has both protection types, supply the opening password to load it and handle the write-protection password separately.
Remove Write Protection from a Presentation
Use IProtectionManager.RemoveWriteProtection to remove the modification restriction, then save the presentation.
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation("write-protected-pres.pptx");
presentation.ProtectionManager.RemoveWriteProtection();
presentation.Save("write-protection-removed.pptx", SaveFormat.Pptx);
Check Whether a Presentation Is Write Protected
To inspect a file without creating a complete Presentation instance, call IPresentationFactory.GetPresentationInfo and inspect IPresentationInfo.IsWriteProtected. The property uses NullableBool and returns NullableBool.True when write protection is detected.
using System;
using Aspose.Slides;
var presentationInfo = PresentationFactory.Instance.GetPresentationInfo("write-protected-pres.pptx");
if (presentationInfo.IsWriteProtected == NullableBool.True)
{
Console.WriteLine("The presentation is write protected.");
}
else
{
Console.WriteLine("Write protection was not detected.");
}
The stream overload of IPresentationFactory.GetPresentationInfo provides the same information for a presentation supplied as a stream.
Validate a Write-Protection Password
Use IPresentationInfo.CheckWriteProtection to validate a modification password without loading the complete presentation. Check IPresentationInfo.IsWriteProtected first so that the application requests or validates a password only when write protection is present.
using System;
using Aspose.Slides;
var presentationInfo = PresentationFactory.Instance.GetPresentationInfo("write-protected-pres.pptx");
if (presentationInfo.IsWriteProtected != NullableBool.True)
{
Console.WriteLine("The presentation is not write protected.");
}
else if (presentationInfo.CheckWriteProtection("modify_password"))
{
Console.WriteLine("The write-protection password is correct.");
}
else
{
Console.WriteLine("The write-protection password is incorrect.");
}
IPresentationInfo.CheckWriteProtection validates only the write-protection password. It does not validate an opening password or determine whether encrypted content can be loaded. Conversely, IPresentationInfo.CheckPassword validates only an opening password. If a complete presentation has already been loaded, IProtectionManager.CheckWriteProtection provides the equivalent write-protection check through its protection manager.
In production applications, do not log passwords or include them in diagnostic messages. Avoid unnecessary repeated validation attempts, and retain passwords in memory only as long as needed.
FAQ
Does write protection encrypt a presentation?
No. It restricts modification but leaves the presentation content available for loading and viewing.
Is the write-protection password required to open a presentation?
No. Only an opening password is required to load encrypted presentation content.
Can a presentation have both an opening password and a write-protection password?
Yes. Supply the opening password through the load options to open the encrypted presentation, and validate the write-protection password separately when modification authorization is required.