Free Support Forum - aspose.com

Document Protection Object

Hello

We would like a document protection object that includes the password. this could be extracted from a document so the original protection can be put back after so editing.

The password can be kept hidden we don’t want to know it just the ability to put it back as it was. here is some sample code after ProtectionType.NoProtection is set the password is lost, in our example we already know the original password but in production the password is not know as it set inside word.

// Create a blank document
Document doc = new Document();

     // Insert two sections with some text
     DocumentBuilder builder = new DocumentBuilder(doc);
     builder.Writeln("Section 1. Unprotected.");
     builder.InsertBreak(BreakType.SectionBreakContinuous);
     builder.Writeln("Section 2. Protected.");

     // Section protection only works when document protection is turned and only editing in form fields is allowed.
     doc.Protect(ProtectionType.AllowOnlyFormFields, "password");

     // By default, all sections are protected, but we can selectively turn protection off.
     doc.Sections[0].ProtectedForForms = false;

     builder.Document.Save("SectionProtected.docx");

     // change protection
     var protectedDocument = new Document("SectionProtected.docx");
     protectedDocument.Protect(ProtectionType.NoProtection);
     protectedDocument.Save("SectionNotProtected.docx");

     // put back expecting orginial password
     var notProtectedDocument = new Document("SectionNotProtected.docx");
     notProtectedDocument.Protect(ProtectionType.AllowOnlyFormFields);

     // as we have only called the protect with no password change we would like  it to hoild
     var shouldBeTrue = notProtectedDocument.Unprotect("password");

@karrimrabi,

Thanks for your inquiry. Please note that Aspose.Words mimics the behavior of MS Word. If you perform the same scenario using MS Word, you will get the same output. The output document does not contain the password. MS Word removes the password when you unprotect the document.