[]
        
(Showing Draft Content)

Document Protection

Document protection is an essential feature in Word documents to restrict unauthorized access. The GcWord library provides the DocumentProtection class to protect the Word documents. With the properties in the DocumentProtection class, you can now add editing restrictions, write protections, document theme, style protections etc. to your document.

Password

With Password protection in GcWord, you can perform one of the following:

  • Open the document in edit mode in case you provide the password.

  • Open the document in ReadOnly mode without providing the password. You can make changes to the document but cannot save them in the same file.

Password protection dialog

To set a password:

  1. Load a document using the Load method of GcWordDocument class.

  2. Set a password using the SetPassword method of Password class, which can be accessed using the WritePassword property of DocumentProtection class.

     //Load the Word document
     GcWordDocument doc = new GcWordDocument();
    doc.Load("SampleDoc.docx"));
    
    //Set the write protection password for the document
    doc.Settings.DocumentProtection.WritePassword.SetPassword("abc");
    
    //Save the document
    doc.Save("PasswordProtection.docx");

Note: User passwords or protection settled in this way can be removed using raw docx modification in any moment. It is not "strong" cryptographic protection.

ReadOnly

This is a special ReadOnly mode that recommends users to open the document as read-only. When this mode is set, the below dialog appears in Word:

A dialog prompting the user to open the document as read-only

  • Yes - Opens the file in ReadOnly mode. You can make changes to the document but cannot save them in the same file.

  • No - Opens the document in edit mode.

  • Cancel - Closes the document.

To set ReadOnlyRecommended mode:

  1. Load a document using the Load method of GcWordDocument class.

  2. Set the ReadOnlyRecommended property of the DocumentProtection class to true.

    //Load the Word document
    GcWordDocument doc = new GcWordDocument();
    doc.Load("SampleDoc.docx"));
    
    //Recommends users to open a document as read-only
    doc.Settings.DocumentProtection.ReadOnlyRecommended = true;
    
    //Save the document
    doc.Save("ReadOnlyRecommendedpassword.docx");

Mark as Final

Mark as Final mode makes the document read-only. However, you can make changes in the document by clicking the 'Edit Anyway' option in dialog box.

To set a document to Mark as Final:

  1. Load a document using the Load method of GcWordDocument class.

  2. Set the MarkAsFinal property of the DocumentProtection class to true.

    //Load the Word document
    GcWordDocument doc = new GcWordDocument();
    doc.Load("SampleDoc.docx"));
    
    //Marks a document as Final
    doc.Settings.DocumentProtection.MarkAsFinal = true;
    
    //Save the document
    doc.Save("MarkAsFinal.docx");

Editing Restrictions

GcWord supports editing restrictions to limit the user's ability to edit text in the document. You can apply certain edit modes to the document by using the EditProtectionMode enumeration.

The IsActive property of EditProtection class should be set to true in order to enforce the editing restrictions. You can also set a password using the Password property of EditProtection class which enables only the users with password to remove the editing restrictions from the document.

Edit modes

Description

AllowOnlyReading

Allows read-only access to the document except regions(ranges) with defined editor rights.

AllowOnlyComments

Allows only comments to be added to the document.

AllowOnlyFormFields

Allows content to be added to the document only through form fields

NoProtection

Does not apply any protection to the document.

  1. Load a document using the Load method of GcWordDocument class.

  2. Set the enum value of EditProtectionMode to AllowOnlyComments using the EditMode property of EditProtection class.

  3. Set the IsActive property of EditProtection class to true.

  4. Set the password using SetPassword method of Password class which can be accessed using EditProtection property of DocumentProtection class

    //Load the Word document
    GcWordDocument doc = new GcWordDocument();
    doc.Load("SampleDoc.docx"));
    
    //Specify the protection mode for a document
    doc.Settings.DocumentProtection.EditProtection.EditMode = EditProtectionMode.AllowOnlyComments;
    
    //Set a password so that  users who know the password can remove the protection and work on the document
    doc.Settings.DocumentProtection.EditProtection.Password.SetPassword("abc123");
    
    //enforce document protection
    doc.Settings.DocumentProtection.EditProtection.IsActive = true;
    
    //Save the document
    doc.Save("EditingRestrictions.docx");

Editable Ranges

Editable ranges are specific regions in a read-only document, which can be edited or modified by users or groups with editing rights. To enable editable ranges, the IsActive property of EditProtection class should be set to true and the edit mode should be set to AllowOnlyReading. The whole document behaves as ReadOnly, except the editable ranges for defined users or groups.

To create editable ranges:

  1. Create a new document and add a paragraph using the Paragraphs property of RangeBase class

  2. Add editable range for the first paragraph using Add method of EditableRangeCollection class. To grant editing rights to everyone, Everyone value of EditorGroup enumeration has been passed to the GroupEditor method.

  3. Add second paragraph and add editable range to it using Add method of EditableRangeCollection class. To grant editing rights to a specific user,the user IDhas been passed to UserEditor method.

  4. Add third paragraph to the document.

  5. Set the enum value of EditProtectionMode to AllowOnlyReading using the EditMode property of EditProtection class.

  6. Set the IsActive property of EditProtection class to true.

  7. Set the password using SetPassword method of Password class which can be accessed using EditProtection property of DocumentProtection class

    GcWordDocument doc = new GcWordDocument();
    
    //add first paragraph
    var firstPara = doc.Body.Paragraphs.Add("First paragraph");
    
    //add new EditableRange for the first paragraph and allow everyone to modify it
    firstPara.GetRange().EditableRanges.Add(new GroupEditor(EditorGroup.Everyone));
    
    //add second paragraph
    var secondPara = doc.Body.Paragraphs.Add("Second paragraph");
    
    //add new EditableRange for the second paragraph and allow a specific user to to modify it
    secondPara.GetRange().EditableRanges.Add(new UserEditor("grapecity\\abc.xyz"));
    
    //add third paragraph
    doc.Body.Paragraphs.Add("Third paragraph");
    
    //set document region protection mode
    doc.Settings.DocumentProtection.EditProtection.EditMode = EditProtectionMode.AllowOnlyReading;
    //enforce document protection
    doc.Settings.DocumentProtection.EditProtection.IsActive = true;
    //Set EditProtection Password
    doc.Settings.DocumentProtection.EditProtection.Password.SetPassword("123");
    
    //now, first paragraph can be edited by everyone.
    //Second paragraph can be edited by a specific user
    //rest of the document is ReadOnly
    doc.Save("EditableRanges.docx");

To delete Editable ranges:

  1. Load the document and access the editable paragraph using the Paragraphs property of RangeBase class

  2. Delete the editable range using the Delete method of EditableRange class.

    GcWordDocument doc = new GcWordDocument();
    
    //Load the document
    doc.Load("EditableRanges.docx");
    
    //Get the editable paragraph
    var para = doc.Body.Paragraphs[1];
    
    //Delete the EditableRange using the Delete method
    para.GetRange().EditableRanges[0].Delete();
    
    //Save the document
    doc.Save("EditableRange_Deleted.docx");

Formatting Restrictions

Formatting restrictions provide various properties to restrict the formatting changes that can be applied to a document. These properties are described below:

Properties

Description

EditProtection.LimitFormattingToUnlockedStyles

Styles with "locked" attribute are not available for use.

EditProtection.AllowAutoformatToOverrideFormatRestrictions

Auto-formatting can override formatting restrictions.

DocumentProtection.BlockThemeOrSchemeSwitching

Blocks the ability to change the theme.

DocumentProtection.BlockQuickStylesetSwitching

Blocks the ability to change the Style Set.

To set formatting restrictions using the properties of EditProtection class:

The IsActive property of EditProtection class should be set to true to enforce the formatting restrictions. You can also set a password using the Password property of EditProtection class which enables only the users with password to remove these formatting restrictions from the document.

  1. Load a document using the Load method of GcWordDocument class.

  2. Lock the Heading1 and hyperlink style by setting the Locked property of Style class to true.

  3. Set the LimitFormattingToUnlockedStyles and AllowAutoformatToOverrideFormatRestrictions properties of EditProtection class to true.

  4. Set the IsActive property of EditProtection class to true.

  5. Set the password using SetPassword method of Password class which can be accessed using EditProtection property of DocumentProtection class.

     //Load the Word document
     GcWordDocument doc = new GcWordDocument();
    doc.Load("SampleDoc.docx"));
    
    //Lock the Heading1 style (Note that this style will not be available for use
    //in the Word document when the LimitFormattingToUnlockedStyles property of
    //the EditProtection class is set to true)
     Style heading1_Style = doc.Styles[BuiltInStyleId.Heading1];
     heading1_Style.Locked = true;
    
     //Lock the Hyperlink style (Note that this style will not be available for use
     //in the Word document when the LimitFormattingToUnlockedStyles property of
     //the EditProtection class is set to true. However this style will be available
     //when AllowAutoformatToOverrideFormatRestrictions property of the EditProtection
     //class is set to true automatically formatting hyperlinks) 
     Style hyperlink_style = doc.Styles[BuiltInStyleId.Hyperlink];
     hyperlink_style.Locked = true;
    
     //Limit formatting to the unlocked styles
     doc.Settings.DocumentProtection.EditProtection.LimitFormattingToUnlockedStyles = true;
    
     //Allow using locked styles when automatically formatting text such as hyperlinks or automatic bullets          
     doc.Settings.DocumentProtection.EditProtection.AllowAutoformatToOverrideFormatRestrictions = true;
    
     //enforce formatting restrictions
     doc.Settings.DocumentProtection.EditProtection.IsActive = true;
    
     //Set a password so that users who know the password can remove the protection and work on the document
     doc.Settings.DocumentProtection.EditProtection.Password.SetPassword("123");
    
     //Save the document
     doc.Save("FormattingRestrictions.docx");

To set formatting restrictions using the properties of DocumentProtection class:

Note: These restrictions can be removed by any user as passwords cannot be set on these properties.

  1. Load a document using the Load method of GcWordDocument class.

  2. Set the BlockThemeOrSchemeSwitching or BlockQuickStylesetSwitching properties of the DocumentProtection class to true.

    //Load the Word document
     GcWordDocument doc = new GcWordDocument();
     doc.Load("SampleDoc.docx"));
    
    //Prevent users from changing the themes used in the document
     doc.Settings.DocumentProtection.BlockThemeOrSchemeSwitching = true;
    
     //Prevent users from changing the current style set
     doc.Settings.DocumentProtection.BlockQuickStylesetSwitching = true;
    
    //Save the document
     doc.Save("FormattingRestrictions.docx");