Document protection is an essential feature in Word documents to restrict unauthorized access. The DsWord 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.
With Password protection in DsWord, you can perform one of the following:
To set a password:
C# |
Copy Code |
---|---|
//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"); |
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:
To set ReadOnlyRecommended mode:
C# |
Copy Code |
---|---|
//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 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:
C# |
Copy Code |
---|---|
//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"); |
DsWord 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. |
C# |
Copy Code |
---|---|
//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 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:
C# |
Copy Code |
---|---|
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("google\\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:
C# |
Copy Code |
---|---|
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 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.
C# |
Copy Code |
---|---|
//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:
C# |
Copy Code |
---|---|
//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"); |