DocumentProtect Method (ProtectionType, String)

Protects the document from changes and optionally sets a protection password.

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.5
Syntax
public void Protect(
	ProtectionType type,
	string password
)

Parameters

type
Type: Aspose.WordsProtectionType
Specifies the protection type for the document.
password
Type: SystemString
The password to protect the document with. Specify null or empty string if you want to protect the document without a password.
Remarks

When a document is protected, the user can make only limited changes, such as adding annotations, making revisions, or completing a form.

Note that document protection is different from write protection. Write protection is specified using the WriteProtection.

Examples
Shows how to protect a document.
// Create a new document and protect it with a password
Document doc = new Document();
doc.Protect(ProtectionType.ReadOnly, "password");
Assert.AreEqual(ProtectionType.ReadOnly, doc.ProtectionType);

// If we open this document with Microsoft Word and wish to edit it, 
// we will first need to stop the protection, which can only be done with the password
doc.Save(ArtifactsDir + "Document.Protect.docx");

// Note that the protection only applies to Microsoft Word users opening out document
// The document can still be opened and edited programmatically without a password, despite its protection status
// Encryption offers a more robust option for protecting document content
Document protectedDoc = new Document(ArtifactsDir + "Document.Protect.docx");
Assert.AreEqual(ProtectionType.ReadOnly, protectedDoc.ProtectionType);

DocumentBuilder builder = new DocumentBuilder(protectedDoc);
builder.Writeln("Text added to a protected document.");

// Documents can have protection removed either with no password, or with the correct password
doc.Unprotect();
Assert.AreEqual(ProtectionType.NoProtection, doc.ProtectionType);

doc.Protect(ProtectionType.ReadOnly, "newPassword");
doc.Unprotect("newPassword");
Assert.AreEqual(ProtectionType.NoProtection, doc.ProtectionType);
See Also