SetPassword

WriteProtection.SetPassword method

Ställer in lösenordet för skrivskydd för dokumentet.

public void SetPassword(string password)
ParameterTypBeskrivning
passwordStringLösenordet som ska ställas in. Kan intenull, men kan vara en tom sträng.

Anmärkningar

Om ett lösenord är inställt kräver Microsoft Word att användaren anger det eller öppnar dokumentet som skrivskyddat.

Exempel

Visar hur man skyddar ett dokument med ett lösenord.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Hello world! This document is protected.");
// Ange ett lösenord på upp till 15 tecken och verifiera sedan dokumentets skyddsstatus.
doc.WriteProtection.SetPassword("MyPassword");
doc.WriteProtection.ReadOnlyRecommended = true;

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

// Skyddet hindrar inte dokumentet från att redigeras programmatiskt, och krypterar inte heller innehållet.
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());

Se även