Reputation: 13
I am trying to find a way to programmatically open a presentation that is both open-protected and edit-protected.
I do know how to achieve one or the latter separately but not both without having to handle a pop-up window asking for edit-password.
To open open-protected file:
Presentation presentation = ppApp.Presentations.Open($"{presentationFile}::{password}::", MsoTriState.msoFalse, MsoTriState.msoFalse, WithWindow: MsoTriState.msoFalse);
To open edit-protected file:
var presentation = ppApp.ProtectedViewWindows.Open($"{presentationFile}", editPassword, MsoTriState.msoFalse);
Actual Question: How to open a presentation protected by both open & edit (known) passwords, remove them and save the presentation?"
Any advise would be welcome, thank you!
Upvotes: 0
Views: 1103
Reputation: 67
With Aspose.Slides for .NET, you can easily remove protection used for a presentation. The following code example shows you how to do this:
// Load a presentation.
var loadOptions = new LoadOptions { Password = "my_password" };
using var presentation = new Presentation("protected.pptx", loadOptions);
// Remove all protection.
presentation.ProtectionManager.RemoveEncryption();
presentation.ProtectionManager.RemoveWriteProtection();
presentation.ProtectionManager.ReadOnlyRecommended = false;
// Save the unprotected presentation to a file.
presentation.Save("unprotected.pptx", SaveFormat.Pptx);
This is a paid library, but you can get a temporary license or use a trial mode to evaluate all features. Alternatively, you can use Aspose.Slides Cloud SDK for .NET to manage presentations. The code example below shows you how to do the same using Aspose.Slides Cloud:
var slidesApi = new SlidesApi("my_client_id", "my_client_secret");
var inputFileName = "protected.pptx";
var outputFileName = "unprotected.pptx";
// Upload the protected presentation to a default storage.
using var inputStream = File.OpenRead(inputFileName);
slidesApi.UploadFile(inputFileName, inputStream);
// Remove all protection.
slidesApi.DeleteProtection(inputFileName, "my_password");
// Download the unprotected presentation and save to a file.
using var outputStream = slidesApi.DownloadFile(inputFileName);
using var resultStream = File.OpenWrite(outputFileName);
outputStream.CopyTo(resultStream);
This is also a paid product based on REST, but you can make 150 free API calls per month for managing presentations and any purposes. I work as a Support Developer and will be glad to answer any questions of these libraries on Aspose.Slides forum.
Upvotes: 0
Reputation: 49455
These two approaches are entirely different.
Files displayed in a Protected View window cannot be edited and are restricted from running active content such as Visual Basic for Applications macros and data connections. For more information about Protected View windows, see What is Protected View?.
The ProtectedViewWindows.Open method allows specifying the read password. It opens and returns a ProtectedViewWindow
object from the ProtectedViewWindows
collection. The ProtectedViewWindow.Edit method changes the password of the ProtectedViewWindow
object.
Because a Protected View window is designed to protect the user from potentially malicious code, the operations that you can perform by using a Presentation
object returned by a ProtectedViewWindow
object will be limited. Operations that are not allowed will return an error.
If we speak about password-protected files, assuming you know the password, you can open the file with something like:
Presentations.Open("c:\temp\protected_presentation.pptx::password::")
And set the password on a presentation with eg:
ActivePresentation.Password = "Hide_me"
So, for example, a raw sketch:
Sub TestTest()
Dim oPPTApp As Object
Dim oPPTPres As Object
Set oPPTApp = CreateObject("PowerPoint.Application")
If Not oPPTApp Is Nothing Then
Set oPPTPres = oPPTApp.presentations.Open("C:\temp\test.pptx::opensesame::")
MsgBox oPPTPres.slides(1).Shapes(1).TextFrame.TextRange.Text
oPPTPres.Close
oPPTApp.Quit
End If
End Sub
Upvotes: 2