Related blog post: Lock & Unlock Word Documents using C#
Last active
November 27, 2021 09:26
-
-
Save GroupDocsGists/3e111e3379902f9852f9e9d72308aeb1 to your computer and use it in GitHub Desktop.
Password Protect Word Documents using C#. Add, Remove, and Change Password using .NET API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Password Protect Word Documents using C# | |
*/ | |
string filePath = @"path/document.docx"; | |
AddPasswordOptions addOptions = new AddPasswordOptions("mySECRETpassWORD"); | |
using (Merger merger = new Merger(filePath)) | |
{ | |
merger.AddPassword(addOptions); | |
merger.Save(@"path/protected-document.docx"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Change password of the protected DOC/DOCX documents in C# | |
*/ | |
string filePath = @"path/protected-document.docx"; | |
LoadOptions loadOptions = new LoadOptions("mySECRETpassWORD"); | |
UpdatePasswordOptions updateOptions = new UpdatePasswordOptions("TOPSECRET_pa22WORD"); | |
using (Merger merger = new Merger(filePath, loadOptions)) | |
{ | |
merger.UpdatePassword(updateOptions); | |
merger.Save(@"path/pwd-changed-document.docx"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Remove password from Word document using C# | |
*/ | |
string filePath = @"path/protected-document.docx"; | |
LoadOptions loadOptions = new LoadOptions("mySECRETpassWORD"); | |
using (Merger merger = new Merger(filePath, loadOptions)) | |
{ | |
merger.RemovePassword(); | |
merger.Save(@"path/no-pwd-document.docx"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment