Skip to content
Restrict Editing

Restrict Editing

Word’s Review > Restrict Editing pane writes a single w:documentProtection element into settings.xml. UniOffice exposes it as DocumentProtection, which you get from doc.Settings. Three things decide its behavior: which edits are still allowed, whether the restriction is enforced, and the password needed to stop enforcing it.

prot := doc.Settings.DocumentProtection()
prot.SetEdit(wml.ST_DocProtectReadOnly)
prot.Enforce(true)
prot.SetPassword("unioffice")

wml is github.com/unidoc/unioffice/v2/schema/soo/wml.

SetEdit valueWhat Word still allows
wml.ST_DocProtectReadOnlyNothing. The document opens read-only.
wml.ST_DocProtectCommentsAdding comments.
wml.ST_DocProtectTrackedChangesEdits, each one recorded as a tracked change.
wml.ST_DocProtectFormsFilling in form fields and nothing else. See Forms.

Enforce(true) is what turns the setting on. Without it Word reads the edit mode and ignores it, the same way a sheet’s cell locks do nothing until the sheet is locked. IsEnforced and Edit read both values back.

The password

SetPassword stores a salted SHA-512 hash with 100000 iterations, using the w:cryptAlgorithmSid, w:cryptSpinCount, w:hash and w:salt attributes Word itself writes. The password a user types into Word’s Stop Protection dialog is checked against that hash. Each call generates a fresh random salt, so saving the same password twice gives different attribute values.

What gets hashed is not the password itself. Word first reduces it to a 32-bit value derived from its first 15 characters, and hashes that, and UniOffice does the same so that Word accepts the password. Two things follow. A longer password is cut to 15 characters, so after SetPassword("unioffice-release-2026") the string unioffice-relea also unlocks the document. And with only 2^32 possible values, different passwords can unlock the same document. Excel’s salted hash, written by SheetProtection.SetPassword, covers the full password and has neither problem. The legacy 16-bit hash that SheetProtection.SetLegacyPassword writes, and that Excel 2007 files carry, is weaker than Word’s.

The password is optional. Enforcement without one gives a restriction any user can switch off from the Restrict Editing pane, which is often all that is needed to stop accidental changes to a template.

For a document that came from Word, VerifyPassword checks a candidate password against whichever hash form the file carries. It returns false when the document has no password at all, so a restriction enforced without one never passes this check:

doc, err := document.Open("contract.docx")
if err != nil {
    return err
}
defer doc.Close()

if doc.Settings.X().DocumentProtection != nil {
    prot := doc.Settings.DocumentProtection()
    if prot.IsEnforced() && prot.VerifyPassword(password) {
        prot.Enforce(false)
    }
}

The nil check matters in v2.15.0, where Settings.DocumentProtection() adds an empty w:documentProtection element to a document that has none. Checking the raw settings first leaves an unprotected document as it was.

CallEffect
prot.Enforce(false)Lifts the restriction, keeping the mode and password for later.
prot.ClearPassword()Removes the password, keeping the mode and enforcement.
doc.Settings.ClearDocumentProtection()Removes the whole element.

Limitations

This is not encryption. The text of the document sits in document.xml as usual, and anyone who unzips the file can read it, or delete w:documentProtection from settings.xml and edit freely. UniOffice itself does not enforce the restriction either: a program can change a protected document through the API without knowing the password. Use Restrict Editing to stop accidental edits in Word. When the content has to stay private, save the file with password encryption instead, or as well.

Word’s per-user exceptions, the parts of a read-only document named users may still edit, are w:permStart and w:permEnd ranges in the body. There is no typed API for them.

Presentations have no equivalent. PowerPoint’s only edit restriction is the password to modify, which is a different element and is not exposed.

Run the example

The example creates a one-paragraph document, restricts it to read-only and enforces that with the password unioffice. It then checks the stored hash with VerifyPassword, printing true for unioffice and false for a wrong password, and saves restrict-editing.docx. The w:documentProtection element in the output’s settings.xml carries w:edit="readOnly", w:enforcement="true", w:cryptAlgorithmSid="14" and w:cryptSpinCount="100000", with the hash and salt beside them.

git clone https://github.com/unidoc/unioffice-examples.git
cd unioffice-examples/document/restrict-editing
go run main.go

If this is your first time using UniOffice, follow the getting started guide to create an API key and set up your development environment.

View the full source
Last updated on