document.SetConformance is added with an example (#399)

* document.SetConformance is added with an example

* SetStrict is added as shortcut

* 2020

* preserve conformance attr

* fix
This commit is contained in:
Vyacheslav Zgordan 2020-06-05 23:38:29 +07:00 committed by GitHub
parent cd8c690655
commit 56b03a8aed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 0 deletions

Binary file not shown.

View File

@ -0,0 +1,21 @@
// Copyright 2020 FoxyUtils ehf. All rights reserved.
package main
import (
"github.com/unidoc/unioffice/document"
st "github.com/unidoc/unioffice/schema/soo/ofc/sharedTypes"
)
func main() {
doc, err := document.Open("document.docx")
if err != nil {
panic(err)
}
doc.SetStrict(false) // document will be saved as Word document (this is a default option for new files)
doc.SaveToFile("conformance_transitional.docx")
doc.SetStrict(true) // document will be saved in the Strict mode
doc.SaveToFile("conformance_strict.docx")
doc.SetConformance(st.ST_ConformanceClassUnset) // Conformance attribute will be unset, which also leads to saving as Word document
doc.SaveToFile("conformance_unset.docx")
}

View File

@ -665,6 +665,7 @@ func Read(r io.ReaderAt, size int64) (*Document, error) {
doc.createCustomProperties()
}
ca := doc.x.ConformanceAttr
decMap := zippkg.DecodeMap{}
decMap.SetOnNewRelationshipFunc(doc.onNewRelationship)
// we should discover all contents by starting with these two files
@ -673,6 +674,7 @@ func Read(r io.ReaderAt, size int64) (*Document, error) {
if err := decMap.Decode(files); err != nil {
return nil, err
}
doc.x.ConformanceAttr = ca
for _, f := range files {
if f == nil {
@ -1150,6 +1152,24 @@ func (d Document) Bookmarks() []Bookmark {
return ret
}
// SetConformance sets conformance attribute of the document
// as one of these values from github.com/unidoc/unioffice/schema/soo/ofc/sharedTypes:
// ST_ConformanceClassUnset, ST_ConformanceClassStrict or ST_ConformanceClassTransitional.
func (d Document) SetConformance(conformanceAttr st.ST_ConformanceClass) {
d.x.ConformanceAttr = conformanceAttr
}
// SetStrict is a shortcut for document.SetConformance,
// as one of these values from github.com/unidoc/unioffice/schema/soo/ofc/sharedTypes:
// ST_ConformanceClassUnset, ST_ConformanceClassStrict or ST_ConformanceClassTransitional.
func (d Document) SetStrict(strict bool) {
if strict {
d.x.ConformanceAttr = st.ST_ConformanceClassStrict
} else {
d.x.ConformanceAttr = st.ST_ConformanceClassTransitional
}
}
func getBool(onOff *wml.CT_OnOff) bool {
return onOff != nil
}

Binary file not shown.

2
go.mod
View File

@ -1 +1,3 @@
module github.com/unidoc/unioffice
go 1.12