Aspose::Words::Settings::WriteProtection Class Reference

Detailed Description

Specifies write protection settings for a document.

Write protection specifies whether the author has recommended that the document is to be opened as read-only and/or require a password to modify a document.

Write protection is different from document protection. Write protection is specified in Microsoft Word in the options of the Save As dialog box.

You do not create instances of this class directly. You access document protection settings via the WriteProtection property.

Examples

Shows how to protect a document with a password.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"Hello world! This document is protected.");
// Enter a password up to 15 characters in length, and then verify the document's protection status.
doc->get_WriteProtection()->SetPassword(u"MyPassword");
doc->get_WriteProtection()->set_ReadOnlyRecommended(true);
ASSERT_TRUE(doc->get_WriteProtection()->get_IsWriteProtected());
ASSERT_TRUE(doc->get_WriteProtection()->ValidatePassword(u"MyPassword"));
// Protection does not prevent the document from being edited programmatically, nor does it encrypt the contents.
doc->Save(ArtifactsDir + u"Document.WriteProtection.docx");
doc = MakeObject<Document>(ArtifactsDir + u"Document.WriteProtection.docx");
ASSERT_TRUE(doc->get_WriteProtection()->get_IsWriteProtected());
builder = MakeObject<DocumentBuilder>(doc);
builder->MoveToDocumentEnd();
builder->Writeln(u"Writing text in a protected document.");
ASSERT_EQ(String(u"Hello world! This document is protected.") + u"\rWriting text in a protected document.", doc->GetText().Trim());

#include <Aspose.Words.Cpp/Settings/WriteProtection.h>

+ Inheritance diagram for Aspose::Words::Settings::WriteProtection:

Public Member Functions

bool get_IsWriteProtected ()
 Returns true when a write protection password is set. More...
 
bool get_ReadOnlyRecommended () const
 Specifies whether the document author has recommended that the document be opened as read-only. More...
 
virtual const TypeInfoGetType () const override
 
virtual bool Is (const TypeInfo &target) const override
 
void set_ReadOnlyRecommended (bool value)
 Setter for get_ReadOnlyRecommended. More...
 
void SetPassword (String password)
 Sets the write protection password for the document. More...
 
bool ValidatePassword (String password)
 Returns true if the specified password is the same as the write-protection password the document was protected with. If document is not write-protected with password then returns false. More...
 

Static Public Member Functions

static const TypeInfoType ()
 

Member Function Documentation

◆ get_IsWriteProtected()

bool Aspose::Words::Settings::WriteProtection::get_IsWriteProtected ( )

Returns true when a write protection password is set.

Examples

Shows how to protect a document with a password.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"Hello world! This document is protected.");
// Enter a password up to 15 characters in length, and then verify the document's protection status.
doc->get_WriteProtection()->SetPassword(u"MyPassword");
doc->get_WriteProtection()->set_ReadOnlyRecommended(true);
ASSERT_TRUE(doc->get_WriteProtection()->get_IsWriteProtected());
ASSERT_TRUE(doc->get_WriteProtection()->ValidatePassword(u"MyPassword"));
// Protection does not prevent the document from being edited programmatically, nor does it encrypt the contents.
doc->Save(ArtifactsDir + u"Document.WriteProtection.docx");
doc = MakeObject<Document>(ArtifactsDir + u"Document.WriteProtection.docx");
ASSERT_TRUE(doc->get_WriteProtection()->get_IsWriteProtected());
builder = MakeObject<DocumentBuilder>(doc);
builder->MoveToDocumentEnd();
builder->Writeln(u"Writing text in a protected document.");
ASSERT_EQ(String(u"Hello world! This document is protected.") + u"\rWriting text in a protected document.", doc->GetText().Trim());

◆ get_ReadOnlyRecommended()

bool Aspose::Words::Settings::WriteProtection::get_ReadOnlyRecommended ( ) const

Specifies whether the document author has recommended that the document be opened as read-only.

Examples

Shows how to protect a document with a password.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"Hello world! This document is protected.");
// Enter a password up to 15 characters in length, and then verify the document's protection status.
doc->get_WriteProtection()->SetPassword(u"MyPassword");
doc->get_WriteProtection()->set_ReadOnlyRecommended(true);
ASSERT_TRUE(doc->get_WriteProtection()->get_IsWriteProtected());
ASSERT_TRUE(doc->get_WriteProtection()->ValidatePassword(u"MyPassword"));
// Protection does not prevent the document from being edited programmatically, nor does it encrypt the contents.
doc->Save(ArtifactsDir + u"Document.WriteProtection.docx");
doc = MakeObject<Document>(ArtifactsDir + u"Document.WriteProtection.docx");
ASSERT_TRUE(doc->get_WriteProtection()->get_IsWriteProtected());
builder = MakeObject<DocumentBuilder>(doc);
builder->MoveToDocumentEnd();
builder->Writeln(u"Writing text in a protected document.");
ASSERT_EQ(String(u"Hello world! This document is protected.") + u"\rWriting text in a protected document.", doc->GetText().Trim());

◆ GetType()

virtual const System::TypeInfo& Aspose::Words::Settings::WriteProtection::GetType ( ) const
overridevirtual

Reimplemented from System::Object.

◆ Is()

virtual bool Aspose::Words::Settings::WriteProtection::Is ( const System::TypeInfo target) const
overridevirtual

Reimplemented from System::Object.

◆ set_ReadOnlyRecommended()

void Aspose::Words::Settings::WriteProtection::set_ReadOnlyRecommended ( bool  value)

◆ SetPassword()

void Aspose::Words::Settings::WriteProtection::SetPassword ( System::String  password)

Sets the write protection password for the document.

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

Parameters
passwordThe password to set. Cannot be null, but can be an empty string.
Examples

Shows how to protect a document with a password.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"Hello world! This document is protected.");
// Enter a password up to 15 characters in length, and then verify the document's protection status.
doc->get_WriteProtection()->SetPassword(u"MyPassword");
doc->get_WriteProtection()->set_ReadOnlyRecommended(true);
ASSERT_TRUE(doc->get_WriteProtection()->get_IsWriteProtected());
ASSERT_TRUE(doc->get_WriteProtection()->ValidatePassword(u"MyPassword"));
// Protection does not prevent the document from being edited programmatically, nor does it encrypt the contents.
doc->Save(ArtifactsDir + u"Document.WriteProtection.docx");
doc = MakeObject<Document>(ArtifactsDir + u"Document.WriteProtection.docx");
ASSERT_TRUE(doc->get_WriteProtection()->get_IsWriteProtected());
builder = MakeObject<DocumentBuilder>(doc);
builder->MoveToDocumentEnd();
builder->Writeln(u"Writing text in a protected document.");
ASSERT_EQ(String(u"Hello world! This document is protected.") + u"\rWriting text in a protected document.", doc->GetText().Trim());

◆ Type()

static const System::TypeInfo& Aspose::Words::Settings::WriteProtection::Type ( )
static

◆ ValidatePassword()

bool Aspose::Words::Settings::WriteProtection::ValidatePassword ( System::String  password)

Returns true if the specified password is the same as the write-protection password the document was protected with. If document is not write-protected with password then returns false.

Examples

Shows how to protect a document with a password.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"Hello world! This document is protected.");
// Enter a password up to 15 characters in length, and then verify the document's protection status.
doc->get_WriteProtection()->SetPassword(u"MyPassword");
doc->get_WriteProtection()->set_ReadOnlyRecommended(true);
ASSERT_TRUE(doc->get_WriteProtection()->get_IsWriteProtected());
ASSERT_TRUE(doc->get_WriteProtection()->ValidatePassword(u"MyPassword"));
// Protection does not prevent the document from being edited programmatically, nor does it encrypt the contents.
doc->Save(ArtifactsDir + u"Document.WriteProtection.docx");
doc = MakeObject<Document>(ArtifactsDir + u"Document.WriteProtection.docx");
ASSERT_TRUE(doc->get_WriteProtection()->get_IsWriteProtected());
builder = MakeObject<DocumentBuilder>(doc);
builder->MoveToDocumentEnd();
builder->Writeln(u"Writing text in a protected document.");
ASSERT_EQ(String(u"Hello world! This document is protected.") + u"\rWriting text in a protected document.", doc->GetText().Trim());