Protect a Document with a Password
Sometimes, you may need a document protection option to keep documents safe when sharing them. For example, this can be useful in some scenarios as sending documents to third-party vendors, using cloud-based products to exchange documents, or as part of a copyright protection strategy.
Aspose.Words allows you to add password protection to your document. It’s important to understand that protection features are password-secured, but at the same time this password can be easily removed and therefore it does not encrypt the document.
This article explains the password protection option to control what other users are able to do with a document.
How to Add Document Protection
Aspose.Words allows you to protect your documents from changes using the Protect method or the SetPassword method. Both of them are not security features and do not encrypt the document. You need to use the Password property to encrypt the content of your document and protect it with a password.
If you need to detect if a document is encrypted and if a password is required, you can use the IsEncrypted property. This will allow you to perform some action before loading a document, for example, inform the user to provide a password.
The following code example shows how to add password protection to your document without any encryption:
// Create a new document and protect it with a password. Document doc = new Document(); // Apply Document Protection. doc.Protect(ProtectionType.NoProtection, "password"); // Save the document. doc.Save(ArtifactsDir + "Document.Protect.docx");
The following code example shows how to encrypt a document with a required password:
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.Write("Hello world!"); // DocSaveOptions only applies to Doc and Dot save formats. DocSaveOptions options = new DocSaveOptions(SaveFormat.Doc); // Set a password with which the document will be encrypted, and which will be required to open it. options.Password = "MyPassword"; // If the document contains a routing slip, we can preserve it while saving by setting this flag to true. options.SaveRoutingSlip = true; doc.Save(ArtifactsDir + "DocSaveOptions.SaveAsDoc.doc", options);
How to Remove Document Protection
Aspose.Words allows you to remove protection from a document. You can either remove the document protection without knowing the actual password or specify the correct password to unlock the document by using the Unprotect method.
The following code example shows how to remove protection from your document:
// Create a new document and protect it with a password. Document doc = new Document(); // Add text. DocumentBuilder builder = new DocumentBuilder(protectedDoc); builder.Writeln("Text added to a document."); // Documents can have protection removed either with no password, or with the correct password. doc.Unprotect(); doc.Protect(ProtectionType.ReadOnly, "newPassword"); doc.Unprotect("newPassword"); // Save the document. doc.Save(ArtifactsDir + "Document.UnProtect.docx");