# 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…

blog

20 Nov 2015 pl

![DocX – Password protect Microsoft Word document programmatically](/wp-content/uploads/2016/03/DocX-MicrosoftWord.png)

[Zobacz obrazy (2)](#pf-content-image-1-0)

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

### 📋 Prerequisites

Following **prerequisites** are required to create **Microsoft Word** document programmatically:

[DocX](https://docx.codeplex.com) package from **Codeplex **or straight from [GitHub sources](https://github.com/WordDocX/DocX)

- ** 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()](https://evotec.xyz/docx-a-short-tutorial-for-beginners-helloworld/)

### 💻 Example code

Following code provides basic example usage

```csharp
static void HelloWorldProtectedDocument()
{
    Console.WriteLine("\tHelloWorldPasswordProtected()");

    // Create a new document.
    using (DocX document = DocX.Create(@"docs\HelloWorldPasswordProtected.docx"))
    {
        // Insert a Paragraph into this document.
        Paragraph p = document.InsertParagraph();

        // Append some text and add formatting.
        p.Append("Hello World!^011Hello World!")
        .Font(new FontFamily("Times New Roman"))
        .FontSize(32)
        .Color(Color.Blue)
        .Bold();

        // Save this document to disk with different options
        // Protected with password for Read Only
        EditRestrictions erReadOnly = EditRestrictions.readOnly;
        document.AddProtection(erReadOnly, "SomePassword");
        document.SaveAs(@"docs\\HelloWorldPasswordProtectedReadOnly.docx");
        Console.WriteLine("\tCreated: docs\\HelloWorldPasswordProtectedReadOnly.docx\n");

        // Protected with password for Comments
        EditRestrictions erComments = EditRestrictions.comments;
        document.AddProtection(erComments, "SomePassword");
        document.SaveAs(@"docs\\HelloWorldPasswordProtectedCommentsOnly.docx");
        Console.WriteLine("\tCreated: docs\\HelloWorldPasswordProtectedCommentsOnly.docx\n");

        // Protected with password for Forms
        EditRestrictions erForms = EditRestrictions.forms;
        document.AddProtection(erForms, "SomePassword");
        document.SaveAs(@"docs\\HelloWorldPasswordProtectedFormsOnly.docx");
        Console.WriteLine("\tCreated: docs\\HelloWorldPasswordProtectedFormsOnly.docx\n");

        // Protected with password for Tracked Changes
        EditRestrictions erTrackedChanges = EditRestrictions.trackedChanges;
        document.AddProtection(erTrackedChanges, "SomePassword");
        document.SaveAs(@"docs\\HelloWorldPasswordProtectedTrackedChangesOnly.docx");
        Console.WriteLine("\tCreated: docs\\HelloWorldPasswordProtectedTrackedChangesOnly.docx\n");

        // But it's also possible to add restrictions without protecting it with password.

        // Protected with password for Read Only
        document.AddProtection(erReadOnly);
        document.SaveAs(@"docs\\HelloWorldWithoutPasswordReadOnly.docx");
        Console.WriteLine("\tCreated: docs\\HelloWorldWithoutPasswordReadOnly.docx\n");

        // Protected with password for Comments
        document.AddProtection(erComments);
        document.SaveAs(@"docs\\HelloWorldWithoutPasswordCommentsOnly.docx");
        Console.WriteLine("\tCreated: docs\\HelloWorldWithoutPasswordCommentsOnly.docx\n");

        // Protected with password for Forms
        document.AddProtection(erForms);
        document.SaveAs(@"docs\\HelloWorldWithoutPasswordFormsOnly.docx");
        Console.WriteLine("\tCreated: docs\\HelloWorldWithoutPasswordFormsOnly.docx\n");

        // Protected with password for Tracked Changes
        document.AddProtection(erTrackedChanges);
        document.SaveAs(@"docs\\HelloWorldWithoutPasswordTrackedChangesOnly.docx");
        Console.WriteLine("\tCreated: docs\\HelloWorldWithoutPasswordTrackedChangesOnly.docx\n");

    }
}
```

### 📄 Example Output

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

[!\[DocX-HelloWorldPasswordProtected-1\](/wp-content/uploads/2015/11/DocX-HelloWorldPasswordProtected-1.png)](/wp-content/uploads/2015/11/DocX-HelloWorldPasswordProtected-1.png)

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.

[!\[DocX-HelloWorldPasswordProtected-2\](/wp-content/uploads/2015/11/DocX-HelloWorldPasswordProtected-2.png)](/wp-content/uploads/2015/11/DocX-HelloWorldPasswordProtected-2.png)

## Powiązane projekty

Projekty powiązane z tagami, kategoriami i treścią tego wpisu.

[Wszystkie projekty](/projekty/)

Trwa dopasowywanie projektów...

[Powrót do bloga](/blog/)

## Powiązane wpisy

- [Check an API write after a PowerShell timeout](/blog/verify-uncertain-api-outcomes-powershell/)
- [Inspect and convert mixed Office archives with PSWriteOffice](/blog/inspect-and-convert-mixed-office-archives-pswriteoffice/)
- [Compose PDF reports and automate page workflows with PSWriteOffice](/blog/automate-pdf-reports-and-page-workflows-pswriteoffice/)

<!-- PowerForge markdown artifact sha256:8602C6249E084980E23BB164C375A682A848824507A8F9D5F867ABB67AA7AB5D -->
