com.aspose.words
Class WriteProtection

java.lang.Object
    extended by com.aspose.words.WriteProtection
All Implemented Interfaces:
java.lang.Cloneable

public class WriteProtection 
extends java.lang.Object

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 Document.WriteProtection property.

Example:

Shows how to protect a document with a password.
Document doc = new Document();

// Enter a password that's up to 15 characters long
doc.getWriteProtection().setPassword("MyPassword");

Assert.assertTrue(doc.getWriteProtection().isWriteProtected());
Assert.assertTrue(doc.getWriteProtection().validatePassword("MyPassword"));

// This flag applies to RTF documents and will be ignored by Microsoft Word
doc.getWriteProtection().setReadOnlyRecommended(true);

DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Write protection does not prevent us from editing the document programmatically.");

// 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(getArtifactsDir() + "Document.WriteProtection.docx");

Property Getters/Setters Summary
booleanisWriteProtected()
           Returns true when a write protection password is set.
booleangetReadOnlyRecommended()
voidsetReadOnlyRecommended(boolean value)
           Specifies whether the document author has recommended that the document be opened as read-only.
 
Method Summary
voidsetPassword(java.lang.String password)
           Sets the write protection password for the document.
booleanvalidatePassword(java.lang.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.
 

Property Getters/Setters Detail

isWriteProtected

public boolean isWriteProtected()
Returns true when a write protection password is set.

Example:

Shows how to protect a document with a password.
Document doc = new Document();

// Enter a password that's up to 15 characters long
doc.getWriteProtection().setPassword("MyPassword");

Assert.assertTrue(doc.getWriteProtection().isWriteProtected());
Assert.assertTrue(doc.getWriteProtection().validatePassword("MyPassword"));

// This flag applies to RTF documents and will be ignored by Microsoft Word
doc.getWriteProtection().setReadOnlyRecommended(true);

DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Write protection does not prevent us from editing the document programmatically.");

// 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(getArtifactsDir() + "Document.WriteProtection.docx");

getReadOnlyRecommended/setReadOnlyRecommended

public boolean getReadOnlyRecommended() / public void setReadOnlyRecommended(boolean value)
Specifies whether the document author has recommended that the document be opened as read-only.

Example:

Shows how to protect a document with a password.
Document doc = new Document();

// Enter a password that's up to 15 characters long
doc.getWriteProtection().setPassword("MyPassword");

Assert.assertTrue(doc.getWriteProtection().isWriteProtected());
Assert.assertTrue(doc.getWriteProtection().validatePassword("MyPassword"));

// This flag applies to RTF documents and will be ignored by Microsoft Word
doc.getWriteProtection().setReadOnlyRecommended(true);

DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Write protection does not prevent us from editing the document programmatically.");

// 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(getArtifactsDir() + "Document.WriteProtection.docx");

Method Detail

setPassword

public void setPassword(java.lang.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:
password - The password to set. Cannot be null, but can be an empty string.

Example:

Shows how to protect a document with a password.
Document doc = new Document();

// Enter a password that's up to 15 characters long
doc.getWriteProtection().setPassword("MyPassword");

Assert.assertTrue(doc.getWriteProtection().isWriteProtected());
Assert.assertTrue(doc.getWriteProtection().validatePassword("MyPassword"));

// This flag applies to RTF documents and will be ignored by Microsoft Word
doc.getWriteProtection().setReadOnlyRecommended(true);

DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Write protection does not prevent us from editing the document programmatically.");

// 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(getArtifactsDir() + "Document.WriteProtection.docx");

validatePassword

public boolean validatePassword(java.lang.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.

Example:

Shows how to protect a document with a password.
Document doc = new Document();

// Enter a password that's up to 15 characters long
doc.getWriteProtection().setPassword("MyPassword");

Assert.assertTrue(doc.getWriteProtection().isWriteProtected());
Assert.assertTrue(doc.getWriteProtection().validatePassword("MyPassword"));

// This flag applies to RTF documents and will be ignored by Microsoft Word
doc.getWriteProtection().setReadOnlyRecommended(true);

DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Write protection does not prevent us from editing the document programmatically.");

// 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(getArtifactsDir() + "Document.WriteProtection.docx");

See Also:
          Aspose.Words Documentation - the home page for the Aspose.Words Product Documentation.
          Aspose.Words Support Forum - our preferred method of support.