Pinch
Pinch

Reputation: 4207

How to lock track changes (with a password) in word 2013 using C#?

I know there are several docx creation/modification approaches in C#

OpenXML, DocX API, etc.

I've figured out how to enable tracked changes using Open XML, just haven't figured out to lock the tracked changes (a feature in word 2013).

Upvotes: -1

Views: 1277

Answers (2)

EFrank
EFrank

Reputation: 1900

Have a look at How to set the editing restrictions in Word using Open XML SDK 2.0

I don't have any working code for you, but the password hash needs to be stored in DocumentProtection.Hash. Hope that this hint helps moving you along!

Upvotes: 1

Nick
Nick

Reputation: 798

This code works fine for me:

static void Main(string[] args)
{
    using (var document = WordprocessingDocument.Open(@"D:\DocTest\Test1.docx", true))
    {
        AddTrackingLock(document);
    }
}

private static void AddTrackingLock(WordprocessingDocument document)
{
    var documentSettings = document.MainDocumentPart.DocumentSettingsPart;
    var documentProtection = documentSettings
                                .Settings
                                .FirstOrDefault(it =>
                                        it is DocumentProtection &&
                                        (it as DocumentProtection).Edit == DocumentProtectionValues.TrackedChanges)
                                as DocumentProtection;

    if (documentProtection == null)
    {
        var documentProtectionElement = new DocumentProtection();
        documentProtectionElement.Edit = DocumentProtectionValues.TrackedChanges;
        documentProtectionElement.Enforcement = OnOffValue.FromBoolean(true);
        documentSettings.Settings.AppendChild(documentProtectionElement);
    }
    else
    {
        documentProtection.Enforcement = OnOffValue.FromBoolean(true);
    }
}

Upvotes: 1

Related Questions