Aspose.Words

Protecting Documents

When a document is protected, the user can make only limited changes, such as adding annotations, making revisions, or completing a form.

Even if a document is protected with a password, Aspose.Words does not require the password to open, modify or unprotect this document.

When you use Aspose.Words to protect a document, you have an option of keeping the existing password or specifying a new password.

Documents protected in Microsoft Word can be easily unprotected even by users without a password. When a document is protected, it can be opened in Microsoft Word, saved as RTF or WordprocessingML document and then the protection password can be removed using Notepad or any plain text editor. Then, the user can open the document again in Microsoft Word and save as an unprotected DOC.

If you need to make sure the document is really protected from changes, consider digitally signing the document. Aspose.Words supports detecting digital signatures for DOC, OOXML and ODT documents. Aspose.Words also preserves a digital signature applied to the VBA project (macros) contained in a document. For further details see the Working with Digital Signatures article.

Protecting a Document

Use the Document.Protect(ProtectionType) method to protect a document from changes. This method accepts a ProtectionType parameter and optionally a password by passing one as the second parameter Document.Protect(ProtectionType, String).

Example ProtectDocument

Shows how to protect a document.

[Java]

 

Document doc = new Document();

doc.protect(ProtectionType.ALLOW_ONLY_FORM_FIELDS, "password");

 

 

Unprotecting a Document

Calling Document.Unprotect unprotects the document even if it has a protection password.

Example UnprotectDocument

Shows how to unprotect any document. Note that the password is not required.

[Java]

 

doc.unprotect();

 

 

Getting the Protection Type

You can retrieve the type of document protection by getting the value of the Document.ProtectionType property.

Example GetProtectionType

Shows how to get protection type currently set in the document.

[Java]

 

Document doc = new Document(getMyDir() + "Document.doc");

int protectionType = doc.getProtectionType();