For some important Word documents, in order to prevent the data from being viewed by others or to prevent the document from being modified, we can select the document to open to add a password or set the permissions of the document, etc., in the following article, we will describe how to use the class library free spire.doc for. NET to encrypt, decrypt and set permissions on the operation of the document, mainly divided into the following points to elaborate
- Add Word password protection
- Unblock word password protection
- Set Word Document Action permissions
- Set Word section content editing permissions
tool use : after installing free Spire.doc, refer to Spire.Doc.dll in the program (DLL can be obtained in the Bin folder under installation path)
1. Word Document Encryption
Using Spire.doc;namespace encryptword_doc{ class program { static void Main (string[] args) { // Initializes an instance of the document class and loads the Word document that needs to be encrypted document doc = new document (@ "C:\Users\Administrator\Desktop\sample.docx"); Set the password doc to open the Word document . Encrypt ("abc123"); Save and open document Doc. SaveToFile ("Encrypted file. docx", fileformat.docx2013); System.Diagnostics.Process.Start ("Encrypting file. docx");}}
After you have debugged the run program, the following document results are visible:
After you enter your password correctly, you can open the document.
2. Word Document decryption
Using Spire.doc;namespace decryptword_doc{ class program { static void Main (string[] args) { // Initializes a document class instance document DOC = new document (); Load the Word document DOC with a password parameter of "adc123" . LoadFromFile ("Encrypted file. docx", fileformat.docx2013, "abc123"); Call Method Removeencryption () to remove password-protected doc. Removeencryption (); Save the document and open Doc. SaveToFile ("Decrypt file. docx", fileformat.docx2013); System.Diagnostics.Process.Start ("Decrypt file. docx");}}
When the document is opened, no password is protected.
3. Word document operation permission settings
(There are 4 different types of operation permissions available, you can choose the appropriate type according to your needs)
Using Spire.doc;namespace editpermissions_doc{ class program { static void Main (string[] args) { //Create a new document class object and load the Word document that needs to be set edit permissions Document doc = new document (@ "C:\Users\Administrator\Desktop\sample.docx"); Do not allow any changes (read only), set the unblock edit password to Doc. Protect (protectiontype.allowonlyreading, "123"); Only allow the form to be filled in, set the unblock edit password doc. Protect (Protectiontype.allowonlyformfields, "123"); Only allow annotations, set the unblock edit password doc. Protect (protectiontype.allowonlycomments, "123"); Only allow revisions, set the unblock edit password doc. Protect (Protectiontype.allowonlyrevisions, "123"); Save and Preview file doc. SaveToFile ("Limited rights document 3.docx", fileformat.docx2013); System.Diagnostics.Process.Start ("Limited rights document 3.docx");}}}
4. Set partial document content permissions
(You can also set editable permissions for parts of the document if they don't want to be modified by others, refer to the code below.) )
Using Spire.doc;namespace lockspecifiedsections_doc{ class program { static void Main (string[] args) { //Create a document class object Document doc = new document (); Initializes 2 instances of the section class and adds the text content to sections S1 = doc. AddSection (); Section s2 = doc. AddSection (); S1. Addparagraph (). AppendText ("section 1"); S2. Addparagraph (). AppendText ("section 2"); Set protection mode and password doc. Protect (Protectiontype.allowonlyformfields, "123"); Set the protection property of Section2 to False, that is, you can edit S2. Protectform = false; Save and open document Doc. SaveToFile ("Protect_section.docx", fileformat.docx2010); System.Diagnostics.Process.Start ("Protect_section.docx");}}}
At this point, in the generated document, Section1 here is set to edit permissions, if you want to edit the need to enter a password.
All of the above is for reference in this section of all code operations on the Word document encryption, decryption, and document operation permission settings. If you like, welcome reprint (Reproduced please specify the source).
Thanks for browsing!
C # set up Word document protection (encryption, decryption, permission settings)