WriteProtectionSetPassword Method

Sets the write protection password for the document.

Namespace:  Aspose.Words.Settings
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 21.6.0

ExpandedSyntax

public void SetPassword(
	string password
)

Parameters

password
Type: SystemString
The password to set. Cannot be null, but can be an empty string.

ExpandedRemarks

If a password is set, Microsoft Word will require the user to enter it or open the document as read-only.

ExpandedExamples

Shows how to protect a document with a password.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Hello world! This document is protected.");

// Enter a password up to 15 characters in length, and then verify the document's protection status.
doc.WriteProtection.SetPassword("MyPassword");
doc.WriteProtection.ReadOnlyRecommended = true;

Assert.IsTrue(doc.WriteProtection.IsWriteProtected);
Assert.IsTrue(doc.WriteProtection.ValidatePassword("MyPassword"));

// Protection does not prevent the document from being edited programmatically, nor does it encrypt the contents.
doc.Save(ArtifactsDir + "Document.WriteProtection.docx");
doc = new Document(ArtifactsDir + "Document.WriteProtection.docx");

Assert.IsTrue(doc.WriteProtection.IsWriteProtected);

builder = new DocumentBuilder(doc);
builder.MoveToDocumentEnd();
builder.Writeln("Writing text in a protected document.");

Assert.AreEqual("Hello world! This document is protected." +
                "\rWriting text in a protected document.", doc.GetText().Trim());

ExpandedSee Also