DocX

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.

Prerequisites

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

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

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.

This post was last modified on %s = human-readable time difference 12:24

Przemyslaw Klys

System Architect with over 14 years of experience in the IT field. Skilled, among others, in Active Directory, Microsoft Exchange and Office 365. Profoundly interested in PowerShell. Software geek.

Recent Posts

Upgrade Azure Active Directory Connect fails with unexpected error

Today, I made the decision to upgrade my test environment and update the version of…

1 month ago

Mastering Active Directory Hygiene: Automating Stale Computer Cleanup with CleanupMonster

Have you ever looked at your Active Directory and wondered, "Why do I still have…

2 months ago

Active Directory Replication Summary to your Email or Microsoft Teams

Active Directory replication is a critical process that ensures the consistent and up-to-date state of…

7 months ago

Syncing Global Address List (GAL) to personal contacts and between Office 365 tenants with PowerShell

Hey there! Today, I wanted to introduce you to one of the small but excellent…

11 months ago

Active Directory Health Check using Microsoft Entra Connect Health Service

Active Directory (AD) is crucial in managing identities and resources within an organization. Ensuring its…

1 year ago

Seamless HTML Report Creation: Harness the Power of Markdown with PSWriteHTML PowerShell Module

In today's digital age, the ability to create compelling and informative HTML reports and documents…

1 year ago