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:
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.
In Microsoft Word, you can restrict editing in a similar way using both:
- Restrict Editing (File → Info → Protect Document)
- Alternative feature – “Restrict Editing” (Review → Protect → Restrict Editing)
The following code example shows how to add password protection to your document:
import aspose.words as aw doc = aw.Document() # Apply document protection. doc.protect(aw.ProtectionType.NO_PROTECTION, "password") doc.save(docs_base.artifacts_dir + "DocumentProtection.PasswordProtection.docx");
The following code example shows how to restrict editing in a document so only editing in form fields is possible:
import aspose.words as aw # Insert two sections with some text. doc = aw.Document() builder = aw.DocumentBuilder(doc) builder.writeln("Text added to a document.") # A document protection only works when document protection is turned and only editing in form fields is allowed. doc.protect(aw.ProtectionType.ALLOW_ONLY_FORM_FIELDS, "password") # Save the protected document. doc.save(docs_base.artifacts_dir + "DocumentProtection.AllowOnlyFormFieldsProtect.docx");
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:
import aspose.words as aw doc = aw.Document() builder = aw.DocumentBuilder(doc) builder.writeln("Text added to a document.") # Documents can have protection removed either with no password, or with the correct password. doc.unprotect() doc.protect(aw.ProtectionType.READ_ONLY, "newPassword") doc.unprotect("newPassword") doc.save(docs_base.artifacts_dir + "DocumentProtection.RemoveDocumentProtection.docx");
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:
import aspose.words as aw # Upload a document and make it as read-only. doc = aw.Document(docs_base.my_dir + "Document.docx") builder = aw.DocumentBuilder(doc) doc.protect(aw.ProtectionType.READ_ONLY, "MyPassword") builder.writeln("Hello world! Since we have set the document's protection level to read-only, we cannot edit this paragraph without the password.") # Start an editable range. edRangeStart = builder.start_editable_range() # An EditableRange object is created for the EditableRangeStart that we just made. editableRange = edRangeStart.editable_range # Put something inside the editable range. builder.writeln("Paragraph inside first editable range") # An editable range is well-formed if it has a start and an end. edRangeEnd = builder.end_editable_range() builder.writeln("This paragraph is outside any editable ranges, and cannot be edited."); doc.save(docs_base.artifacts_dir + "DocumentProtection.UnrestrictedEditableRegions.docx");
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:
import aspose.words as aw # Insert two sections with some text. doc = aw.Document() builder = aw.DocumentBuilder(doc) builder.writeln("Section 1. Unprotected.") builder.insert_break(aw.BreakType.SECTION_BREAK_CONTINUOUS) builder.writeln("Section 2. Protected.") # Section protection only works when document protection is turned and only editing in form fields is allowed. doc.protect(aw.ProtectionType.ALLOW_ONLY_FORM_FIELDS, "password") # By default, all sections are protected, but we can selectively turn protection off. doc.sections.protected_for_forms = False doc.save(docs_base.artifacts_dir + "DocumentProtection.UnrestrictedSection.docx"); doc = aw.Document(docs_base.artifacts_dir + "DocumentProtection.UnrestrictedSection.docx"); self.assertFalse(doc.sections.protected_for_forms) self.assertTrue(doc.sections.protected_for_forms);