Restrict Document Editing

Sometimes you may need to limit the ability to edit a document and only allow certain actions with it. This can be useful to prevent other people from editing sensitive and confidential information in your document.

Aspose.Words allows you to restrict editing a document by setting a restriction type. In addition, Aspose.Words also enables you to specify write protection settings for a document.

This article explains how to use Aspose.Words to select a restriction type, how to add or remove protection, and how to make unrestricted editable regions.

Select Editing Restriction Type

Aspose.Words allows you to control the way you restrict the content using the ProtectionType enumeration parameter. This will enable you to select an exact type of protection such as the following:

  • AllowOnlyComments
  • AllowOnlyFormFields
  • AllowOnlyRevisions
  • ReadOnly
  • NoProtection

All types are password-secured, and if this password is not entered correctly, a user will not be able to legally change the content of your document. Thus, if your document is returned to you without a requirement to provide the necessary password, this is a sign that something is wrong.

If you did not set a password when choosing the security type, other users can simply ignore the protection of your document.

Add Document Protection

Adding protection to your document is a simple process, as all you need to do is apply one of the protection methods detailed in this section.

Aspose.Words allows you to protect your documents from changes using the Document.protect method. This method is not a security feature and does not encrypt a document.

The following code example shows how to add password protection to your document:

The following code example shows how to restrict editing in a document so only editing in form fields is possible:

Remove Document Protection

Aspose.Words allows you to remove protection from a document with simple and direct document modification. You can either remove the document protection without knowing the actual password or provide the correct password to unlock the document by using the unprotect method. Both removing ways have no difference.

The following code example shows how to remove protection from your document:

Specify Unrestricted Editable Regions

You can restrict editing of your document and at the same time allow changes to selected parts of it. So, anyone who opens your document will be able to access these unrestricted parts and make changes to the content.

Aspose.Words allows you to mark the parts that can be changed in your document using the start_editable_range and end_editable_range methods.

The following code example shows how to mark the whole document as read-only and specify editable regions in it:

You can also choose different document editing restrictions for different sections.

The following code example shows how to add a restriction for the entire document, and then remove the restriction for one of the sections:

Limitations and Considerations

  • Aspose.Words protection is not encryption — it stores only a password hash in the document properties and does not secure the document content.
  • Protection does not prevent programmatic access or modification — any Aspose.Words application can remove protection or modify the document regardless of restrictions.
  • The document.protect(...) method only sets a metadata flag; it does not prevent changes via API calls such as document.unprotect() or document_builder.start_editable_range().
  • Editable ranges do not override section‑level protection settings — they apply only to the document‑level protection state and require correct section break types to function as expected.