Skip to main content
You are viewing help content for pre-release software. This document and the features it describes are subject to change.
All docs
V26.2
  • Manage Document Security

    • 8 minutes to read

    The Word Processing Document API allows you to encrypt documents and to protect them from edit operations. Encryption and protection are independent of each other. You can apply both to the same document.

    Security Setting Users Need the Password To Method
    Encryption Open the file. WordDocument.Encrypt(options)
    Editing restrictions Remove restrictions and edit restricted content. WordDocument.Protect(options)
    Password to modify Open a document in edit mode. Without a password, users can open the document in read-only mode. WordDocument.Protect(options)

    Encrypt a Document

    Encrypt a document to require a password to open the document file. Word processing applications (for example, Microsoft Word) and the Word Processing Document API cannot open an encrypted file without a password.

    Call the WordDocument.Encrypt method to encrypt a document. Pass an EncryptionOptions object to the Encrypt method to specify the password and the encryption type (EncryptionType):

    Encryption Type Description
    Strong Agile encryption for Open XML files that uses an XML EncryptionInfo structure. The default encryption algorithm is AES-256, and the hashing algorithm is SHA-512. Binary files, such as XLS files, use RC4 CryptoAPI encryption.
    Compatible Standard encryption for Open XML files that uses a binary EncryptionInfo structure. The default encryption algorithm is AES-128, and the hashing algorithm is SHA-1.

    Important

    The Compatible encryption type uses outdated algorithms for compatibility purposes (for earlier versions of Microsoft Office). To protect sensitive data, use the Strong encryption type instead.

    The password must contain between 1 to 255 characters.

    The API encrypts the document when you save the document. DOCX, DOCM, DOTX, DOTM, DOC, and DOT files support encryption. If you save the document in another format (for example, RTF, ODT, or Flat OPC), the API does not encrypt the output.

    The following code snippet encrypts a document with a password and saves the encrypted document to a DOCX file:

    using System.IO;
    using DevExpress.Docs.Office;
    using DevExpress.Docs.Word;
    
    Directory.CreateDirectory("output");
    
    using var inputStream = File.OpenRead("Document.docx");
    using var document = new WordDocument(inputStream);
    
    document.Encrypt(new EncryptionOptions("password", EncryptionType.Strong));
    
    using var outputStream = File.Create(Path.Combine("output", "Document.docx"));
    document.Save(outputStream, DocumentFormat.Docx);
    

    Open an Encrypted Document

    Set the LoadOptions.Password property to the document password and pass the LoadOptions object to the WordDocument(stream, options) or WordDocument(buffer, options) constructor.

    The following code snippet loads a password-encrypted document:

    using System.IO;
    using DevExpress.Docs.Word;
    
    var loadOptions = new LoadOptions {
        Password = "password"
    };
    
    using var stream = File.OpenRead("Document.docx");
    using var document = new WordDocument(stream, loadOptions);
    
    // Process the document.
    

    Refer to the following help topic for additional information: Create, Load, and Save Documents.

    Modify or Remove Encryption

    If you load an encrypted document and save it again, the API encrypts the saved file with the same password and encryption type. To modify the password or the encryption type, call the WordDocument.Encrypt method. The new settings replace the current settings.

    Call the WordDocument.RemoveEncryption() method to save the document without encryption. Protection settings remain.

    The following code snippet opens an encrypted document, removes encryption, and saves the document:

    using System.IO;
    using DevExpress.Docs.Word;
    
    Directory.CreateDirectory("output");
    
    // Open the encrypted document.
    var loadOptions = new LoadOptions {
        Password = "password"
    };
    using var inputStream = File.OpenRead("Document.docx");
    using var document = new WordDocument(inputStream, loadOptions);
    
    // Remove encryption and save the document.
    document.RemoveEncryption();
    using var outputStream = File.Create(Path.Combine("output", "Document.docx"));
    document.Save(outputStream, DocumentFormat.Docx);
    

    Restrict Document Editing

    Call the WordDocument.Protect method to restrict edit operations in a document. Pass a DocumentProtectionOptions object to the Protect method. The DocumentProtectionOptions(password, hashAlgorithm) constructor specifies the password and the algorithm that the API uses to generate the password hash (HashAlgorithmType).

    Use the DocumentProtectionOptions.ProtectionType property to specify editing restrictions:

    Protection Type Description
    ReadOnly Users can read the document but cannot modify document content.
    AllowComments Users can add comments but cannot modify document content.
    TrackedChanges Users can modify documents, and word processing applications track all changes as revisions.
    FillInForms Users can fill in form fields but cannot modify other document content.
    None The document has no editing restrictions.

    If you do not specify a password, users can remove editing restrictions without a password.

    The following code snippet restricts document editing to comments and protects editing restrictions with a password:

    using System.IO;
    using DevExpress.Docs.Office;
    using DevExpress.Docs.Word;
    
    Directory.CreateDirectory("output");
    
    using var inputStream = File.OpenRead("Document.docx");
    using var document = new WordDocument(inputStream);
    
    var protectionOptions = new DocumentProtectionOptions("password", HashAlgorithmType.Sha512) {
        ProtectionType = DocumentProtectionType.AllowComments
    };
    document.Protect(protectionOptions);
    
    using var outputStream = File.Create(Path.Combine("output", "Document.docx"));
    document.Save(outputStream, DocumentFormat.Docx);
    

    Note

    Protection settings apply to users in word processing applications (for example, Microsoft Word). The Word Processing Document API does not check protection settings when your code modifies a protected document.

    Document protection does not encrypt document content. To require a password to open a document, call the WordDocument.Encrypt method.

    Require a Password to Modify a Document

    Set the DocumentProtectionOptions.RequirePasswordToModify property to true to require a password to modify a document. Word processing applications open documents in read-only mode until users enter a password.

    The password that allows modification and the password that allows editing restrictions are the same. If the RequirePasswordToModify property is true, the password cannot be null or an empty string.

    To require a password so users can modify a document without editing restrictions, set the DocumentProtectionOptions.ProtectionType property to None.

    The following code snippet requires a password to modify a document and applies no editing restrictions:

    using System.IO;
    using DevExpress.Docs.Word;
    
    Directory.CreateDirectory("output");
    
    using var inputStream = File.OpenRead("Document.docx");
    using var document = new WordDocument(inputStream);
    
    var protectionOptions = new DocumentProtectionOptions("password") {
        ProtectionType = DocumentProtectionType.None,
        RequirePasswordToModify = true
    };
    document.Protect(protectionOptions);
    
    using var outputStream = File.Create(Path.Combine("output", "Document.docx"));
    document.Save(outputStream, DocumentFormat.Docx);
    

    Check Document Security Settings

    Use the following properties to get the security settings of a loaded or created document:

    Property Description
    EncryptionType Gets the document encryption type.
    ProtectionType Gets the type of editing restrictions that apply to the document.
    ProtectionMode Gets whether users need a password to modify a document.

    The following code snippet loads a document and displays the document’s encryption type, editing restrictions, and protection mode in the console:

    using System;
    using System.IO;
    using DevExpress.Docs.Word;
    
    using var stream = File.OpenRead("Document.docx");
    using var document = new WordDocument(stream);
    
    Console.WriteLine($"Encryption type: {document.EncryptionType?.ToString() ?? "None"}");
    Console.WriteLine($"Editing restrictions: {document.ProtectionType}");
    Console.WriteLine($"Protection mode: {document.ProtectionMode}");
    

    Modify or Remove Protection

    If you call the WordDocument.Protect method for a protected document, the new settings replace all current protection settings, including the modification password.

    Call the WordDocument.RemoveProtection() method to remove editing restrictions and the modification password. You do not need a password to call the RemoveProtection method. Document encryption remains.

    The following code snippet removes editing restrictions and the password from a document:

    using System.IO;
    using DevExpress.Docs.Word;
    
    Directory.CreateDirectory("output");
    
    using var inputStream = File.OpenRead("Document.docx");
    using var document = new WordDocument(inputStream);
    
    document.RemoveProtection();
    
    using var outputStream = File.Create(Path.Combine("output", "Document.docx"));
    document.Save(outputStream, DocumentFormat.Docx);
    

    Next Steps