Skip to content

Commit

Permalink
document.SetConformance is added with an example (unidoc#399)
Browse files Browse the repository at this point in the history
* document.SetConformance is added with an example

* SetStrict is added as shortcut

* 2020

* preserve conformance attr

* fix
  • Loading branch information
zgordan-vv authored Jun 5, 2020
1 parent cd8c690 commit 56b03a8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
Binary file added _examples/document/set-strict/document.docx
Binary file not shown.
21 changes: 21 additions & 0 deletions _examples/document/set-strict/main.go
Original file line number Diff line number Diff line change
@@ -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")
}
20 changes: 20 additions & 0 deletions document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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 modified document/testdata/issue198.docx.golden
Binary file not shown.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module github.com/unidoc/unioffice

go 1.12

0 comments on commit 56b03a8

Please sign in to comment.