Free Support Forum -

Document Protection Object


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.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;


     // change protection
     var protectedDocument = new Document("SectionProtected.docx");

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

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


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.