2014-07-11 57 views

回答

1

此代碼工作正常,我:

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); 
    } 
} 
+0

非常感謝您的回答,但該代碼不強制使用密碼鎖定。 – Pinch

+1

在DocumentProtection中,修改'Hash'屬性,它是'Password Hash.Represents schema中的屬性:w:hash'(來自MSDN文檔:http://msdn.microsoft.com/en-us/library/ documentformat.openxml.wordprocessing.documentprotection_members%28v = office.14%29.aspx) –