java.lang.Objectcom.aspose.words.WriteProtection
public class WriteProtection
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 Example:
Document doc = new Document();
Assert.assertFalse(doc.getWriteProtection().isWriteProtected());
Assert.assertFalse(doc.getWriteProtection().getReadOnlyRecommended());
// Enter a password that's 15 or less characters long
doc.getWriteProtection().setPassword("docpassword123");
Assert.assertTrue(doc.getWriteProtection().isWriteProtected());
Assert.assertFalse(doc.getWriteProtection().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(getArtifactsDir() + "Document.WriteProtection.docx");
// Re-open our document
Document docProtected = new Document(getArtifactsDir() + "Document.WriteProtection.docx");
DocumentBuilder docProtectedBuilder = new DocumentBuilder(docProtected);
docProtectedBuilder.moveToDocumentEnd();
// We can programmatically edit this document without using our password
Assert.assertTrue(docProtected.getWriteProtection().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(getArtifactsDir() + "Document.WriteProtectionEditedAfter.docx");
Property Getters/Setters Summary | ||
---|---|---|
boolean | isWriteProtected() | |
Returns true when a write protection password is set. | ||
boolean | getReadOnlyRecommended() | |
void | setReadOnlyRecommended(boolean value) | |
Specifies whether the document author has recommended that the document be opened as read-only. |
Method Summary | ||
---|---|---|
void | setPassword(java.lang.String password) | |
Sets the write protection password for the document. | ||
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. |
Property Getters/Setters Detail |
---|
isWriteProtected | |
public boolean isWriteProtected() |
Example:
Shows how to protect a document with a password.Document doc = new Document(); Assert.assertFalse(doc.getWriteProtection().isWriteProtected()); Assert.assertFalse(doc.getWriteProtection().getReadOnlyRecommended()); // Enter a password that's 15 or less characters long doc.getWriteProtection().setPassword("docpassword123"); Assert.assertTrue(doc.getWriteProtection().isWriteProtected()); Assert.assertFalse(doc.getWriteProtection().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(getArtifactsDir() + "Document.WriteProtection.docx"); // Re-open our document Document docProtected = new Document(getArtifactsDir() + "Document.WriteProtection.docx"); DocumentBuilder docProtectedBuilder = new DocumentBuilder(docProtected); docProtectedBuilder.moveToDocumentEnd(); // We can programmatically edit this document without using our password Assert.assertTrue(docProtected.getWriteProtection().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(getArtifactsDir() + "Document.WriteProtectionEditedAfter.docx");
getReadOnlyRecommended/setReadOnlyRecommended | |
public boolean getReadOnlyRecommended() / public void setReadOnlyRecommended(boolean value) |
Example:
Shows how to protect a document with a password.Document doc = new Document(); Assert.assertFalse(doc.getWriteProtection().isWriteProtected()); Assert.assertFalse(doc.getWriteProtection().getReadOnlyRecommended()); // Enter a password that's 15 or less characters long doc.getWriteProtection().setPassword("docpassword123"); Assert.assertTrue(doc.getWriteProtection().isWriteProtected()); Assert.assertFalse(doc.getWriteProtection().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(getArtifactsDir() + "Document.WriteProtection.docx"); // Re-open our document Document docProtected = new Document(getArtifactsDir() + "Document.WriteProtection.docx"); DocumentBuilder docProtectedBuilder = new DocumentBuilder(docProtected); docProtectedBuilder.moveToDocumentEnd(); // We can programmatically edit this document without using our password Assert.assertTrue(docProtected.getWriteProtection().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(getArtifactsDir() + "Document.WriteProtectionEditedAfter.docx");
Method Detail |
---|
setPassword | |
public void setPassword(java.lang.String password) |
If a password is set, Microsoft Word will require the user to enter it or open the document as read-only.
password
- The password to set. Cannot be null, but can be an empty string.Example:
Sets the write protection password for the document.Document doc = new Document(); doc.getWriteProtection().setPassword("pwd");
validatePassword | |
public boolean validatePassword(java.lang.String password) |
Example:
Shows how to protect a document with a password.Document doc = new Document(); Assert.assertFalse(doc.getWriteProtection().isWriteProtected()); Assert.assertFalse(doc.getWriteProtection().getReadOnlyRecommended()); // Enter a password that's 15 or less characters long doc.getWriteProtection().setPassword("docpassword123"); Assert.assertTrue(doc.getWriteProtection().isWriteProtected()); Assert.assertFalse(doc.getWriteProtection().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(getArtifactsDir() + "Document.WriteProtection.docx"); // Re-open our document Document docProtected = new Document(getArtifactsDir() + "Document.WriteProtection.docx"); DocumentBuilder docProtectedBuilder = new DocumentBuilder(docProtected); docProtectedBuilder.moveToDocumentEnd(); // We can programmatically edit this document without using our password Assert.assertTrue(docProtected.getWriteProtection().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(getArtifactsDir() + "Document.WriteProtectionEditedAfter.docx");