-
Notifications
You must be signed in to change notification settings - Fork 114
Prevent Document Edition
Olivier Nizet edited this page Jun 13, 2017
·
1 revision
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.
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.
![](Prevent Document Edition_protected.png) Example as Word prevent edition
You can enforce the rule edition with a strong password. Take a look at this sample.