Open a Document Read-Only

Sometimes, you may have a document that needs a review, but you do not want reviewers to randomly modify your content. Aspose.Words allows you to make the permission of your document read-only so that the content can be copied or read, but not modified. This will prevent content from being removed or added to your document.

This article explains how to make a document read-only.

Make a Document Read-Only

Aspose.Words has the public class aspose.words.settings.WriteProtection that specifies write protection settings for a document. You do not create instances of this class directly.

Write protection shows whether the author has recommended opening a document as read-only and/or requiring a password to modify the document.

Aspose.Words allows you to make a document read-only to restrict editing by using the read_only_recommended property and the set_password method.

The read_only_recommended property is password-secured, so if you do not set a password before applying the read_only_recommended property, then other users can simply open the document as if it were unprotected. You access the document protection settings and set a write protection password via the set_password method.

If you need to check if a document has a write protection password that restricts it from editing, you can use the is_write_protected property.

The following code example shows how to make a document read-only:

import aspose.words as aw

doc = aw.Document()
builder = aw.DocumentBuilder(doc)

builder.write("Open document as read-only")

# Enter a password that's up to 15 characters long.
doc.write_protection.set_password("MyPassword")

# Make the document as read-only.
doc.write_protection.read_only_recommended = True

# Apply write protection as read-only.
doc.protect(aw.ProtectionType.READ_ONLY)
doc.save(docs_base.artifacts_dir + "DocumentProtection.ReadOnlyProtection.docx")

Remove Read-Only Restriction

If you do not want a user to open your document as read-only, you can simply set the read_only_recommened property to False or select ProtectionType as NO_PROTECTION.

The following code example shows how to remove read-only access for a document:

import aspose.words as aw

doc = aw.Document()
            
# Enter a password that's up to 15 characters long.
doc.write_protection.set_password("MyPassword")

# Remove the read-only option.
doc.write_protection.read_only_recommended = False

# Apply write protection without any protection.
doc.protect(aw.ProtectionType.NO_PROTECTION)
doc.save(docs_base.artifacts_dir + "DocumentProtection.RemoveReadOnlyRestriction.docx")