Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
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.
Aspose.Words has the public class 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 readOnlyRecommended property and the setPassword method.
In Microsoft Word, you can create a Read-Only document in a similar way using both:
Users can also restrict document editing by selecting ProtectionType as ReadOnly, but this is another feature that provides more advanced protection capabilities. There is such a function in Microsoft Word, respectively, it is implemented in Aspose.Words.
ProtectionType will be described in detail in one of the following articles – “Restrict Document Editing”.
The readOnlyRecommended property is password-secured, so if you do not set a password before applying the readOnlyRecommended 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 setPassword method.
If you need to check if a document has a write protection password that restricts it from editing, you can use the isWriteProtected property.
The following code example shows how to make a document read-only:
If you do not want a user to open your document as read-only, you can simply set the readOnlyRecommended property to False or select ProtectionType as NoProtection.
The following code example shows how to remove read-only access for a document:
Q: How can I make a document read‑only without requiring a password?
A: Set the readOnlyRecommended property to true on the document’s WriteProtection object and do not call setPassword. The document will open in read‑only mode, but users can still remove the recommendation by changing the setting in Word.
Q: How do I set a password that forces users to open the document as read‑only?
A: After loading the document, call document.writeProtection.setPassword("MyPassword") and then set document.writeProtection.readOnlyRecommended = true. When the file is opened, Word will prompt for the password before allowing edits.
Q: How can I programmatically verify whether a document is write‑protected?
A: Use the isWriteProtected property:
const Document = require("aspose.words").Document;
const doc = new Document("input.docx");
const protected = doc.writeProtection.isWriteProtected;
console.log("Write protected:", protected);
Q: What is the correct way to remove the read‑only recommendation from a document?
A: Set readOnlyRecommended to false and optionally clear the password with setPassword(""). Example:
const doc = new Document("protected.docx");
doc.writeProtection.readOnlyRecommended = false;
doc.writeProtection.setPassword("");
doc.save("unprotected.docx");
Q: What is the difference between readOnlyRecommended and ProtectionType.ReadOnly?
A: readOnlyRecommended only suggests that the document be opened in read‑only mode and can be combined with a password; it does not enforce editing restrictions. ProtectionType.ReadOnly (used via document.protectionType = ProtectionType.ReadOnly) applies a stronger protection that disables editing features in Word, similar to the “Restrict Editing” feature. Use ProtectionType when you need to prevent any modifications, not just suggest read‑only.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.