Our Blog

DocX – Password protect Microsoft Word document programmatically

Following code is an example on how to add protection to Microsoft Word document using C# without needing to have Microsoft Word installed.


Following prerequisites are required to create Microsoft Word document programmatically:

DocX package from Codeplex or straight from GitHub sources
Visual Studio 2013 / Visual Studio 2015 (Free Community Edition will do)
Basic understanding on how to code in C# (C Sharp)
Basic tutorial on how to start coding with DocX package can be found on our blog post DocX – A short tutorial for beginners HelloWorld()

Example code

Following code provides basic example usage

Example Output

After running above code 8 files should be created with multiple options


After opening Microsoft Word .docx document and trying to edit it (HelloWorldWithPasswordReadOnly.docx) following Restrict Editing option shows up requiring user to stop protection before being able to continue.


Tags: , , , , , , , , ,

This is a unique website which will require a more modern browser to work! Please upgrade today!