DocumentWriteProtection Property
Provides access to the document write protection options.

Namespace: Aspose.Words
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 19.11
Syntax
public WriteProtection WriteProtection { get; }

Property Value

Type: WriteProtection
Examples
Shows how to protect a document with a password.
Document doc = new Document();
Assert.IsFalse(doc.WriteProtection.IsWriteProtected);
Assert.IsFalse(doc.WriteProtection.ReadOnlyRecommended);

// Enter a password that's 15 or less characters long
doc.WriteProtection.SetPassword("docpassword123");
Assert.IsTrue(doc.WriteProtection.IsWriteProtected);

Assert.IsFalse(doc.WriteProtection.ValidatePassword("wrongpassword"));

DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("We can still edit the document at this stage.");

// Save the document
// Without the password, we can only read this document in Microsoft Word
// With the password, we can read and write
doc.Save(ArtifactsDir + "Document.WriteProtection.docx");

// Re-open our document
Document docProtected = new Document(ArtifactsDir + "Document.WriteProtection.docx");
DocumentBuilder docProtectedBuilder = new DocumentBuilder(docProtected);
docProtectedBuilder.MoveToDocumentEnd();

// We can programmatically edit this document without using our password
Assert.IsTrue(docProtected.WriteProtection.IsWriteProtected);
docProtectedBuilder.Writeln("Writing text in a protected document.");

// We will still need the password if we want to open this one with Word
docProtected.Save(ArtifactsDir + "Document.WriteProtectionEditedAfter.docx");
See Also