Strict Mode
ECMA-376 defines two conformance classes for a DOCX file, Transitional and
Strict. UniOffice writes Transitional, which is what Word produces by default
and what every consumer accepts. SetStrict(true) changes the class the
document declares.
| Call | w:conformance on w:document |
|---|---|
none (document.New default) | transitional |
doc.SetStrict(false) | transitional |
doc.SetStrict(true) | strict |
doc.SetConformance(v) | whatever sharedTypes.ST_ConformanceClass value you pass |
SetStrict is a two-value shortcut for SetConformance. Reach for
SetConformance only if you need ST_ConformanceClassUnset, which is not a
useful state (see Limitations).
Setting it
doc, err := document.Open("document.docx")
if err != nil {
log.Fatal(err)
}
defer doc.Close()
doc.SetStrict(true)
if err := doc.SaveToFile("conformance_strict.docx"); err != nil {
log.Fatal(err)
}The setting lives on the document rather than on the save call, so one document can be written twice with different classes. That is all the example does: set it false, save, set it true, save again.
What changes in the file
One attribute value, and nothing else. Comparing the two files the example
produces, every part of the package is byte-identical except word/document.xml,
and the difference there is w:conformance="transitional" against
w:conformance="strict" on the root element. Part names, content types and the
document body are untouched.
In particular the markup keeps the Transitional namespaces
(http://schemas.openxmlformats.org/wordprocessingml/2006/main and friends) in
both files. UniOffice does not rewrite parts into the Strict namespaces under
http://purl.oclc.org/ooxml/, and it does not inspect the document for
constructs that Strict disallows. The attribute is a declaration about the file,
not a conversion of it, so a validator checking the package against the Strict
schema will not be satisfied by setting it. Reading works in both directions:
the unmarshalers accept either namespace, so a Strict file from another producer
opens normally.
Limitations
document.Open reports Transitional whatever the file said. The conformance
value is captured from the freshly constructed document before the package is
decoded and restored afterwards, so the value in the file is discarded. Open a
Strict document, change a word, save it, and you have written a Transitional
one. Call SetStrict(true) again on every round trip where the class matters.
Append can downgrade a document. If the document being appended is not Strict,
the result takes that document’s class, so appending a Transitional document
into a Strict one leaves you with Transitional.
SetConformance(sharedTypes.ST_ConformanceClassUnset) does not omit the
attribute; it writes w:conformance="". Pre-save validation flags the attribute
as mandatory and logs a warning, then the save proceeds anyway.
The setter exists only on document.Document. Spreadsheets and presentations
carry the same conformance attribute in their schemas but expose no equivalent
call.
Run the example
The example opens the document.docx bundled with it and writes
conformance_transitional.docx and conformance_strict.docx side by side. Unzip
both and diff word/document.xml to see the whole of the difference.
git clone https://github.com/unidoc/unioffice-examples.git
cd unioffice-examples/document/set-strict
go run main.goIf this is your first time using UniOffice, follow the getting started guide to create an API key and set up your development environment.