This project has moved. For the latest updates, please go here.
Sometimes, you just want to generate a static report from Word but do not want to allow the readers to modify the document. With OpenXml, it's quite easy.

Simple Lock

This code is mainly to prevent from end-user, but this is only a simple lock and can be disabled easily.

/// <summary>
/// Locks the document to prevent the users writing any accidental modifications.
/// </summary>
public static void PreventDocumentEdition(WordprocessingDocument document)
{
      document.ExtendedFilePropertiesPart.Properties.DocumentSecurity.Text = "8";
      document.ExtendedFilePropertiesPart.Properties.Save();

      Settings settings = document.MainDocumentPart.DocumentSettingsPart.Settings;
      settings.Append(
           new DocumentProtection() {
               Edit = DocumentProtectionValues.ReadOnly,
               Enforcement = new OnOffValue(true)
      });

      settings.Save();
}

You have the option to also allow only Track Changes..
Look at the MSDN about DocumentProtectionValues.

protected.png
Example as Word prevent edition

Password Protected

You can enforce the rule edition with a strong password.
Take a look at this sample.

Last edited Dec 18, 2015 at 3:49 PM by onizet, version 2