Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SaveAsTemplate and SaveToFileAsTemplate are added for presentation #400

Merged
merged 2 commits into from
Jun 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion presentation/presentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,15 @@ func (p *Presentation) AddDefaultSlideWithLayout(l SlideLayout) (Slide, error) {

// Save writes the presentation out to a writer in the Zip package format
func (p *Presentation) Save(w io.Writer) error {
return p.save(w, false)
}

// SaveAsTemplate writes the presentation out to a writer in the Zip package format as a template
func (p *Presentation) SaveAsTemplate(w io.Writer) error {
return p.save(w, true)
}

func (p *Presentation) save(w io.Writer, isTemplate bool) error {
if err := p.x.Validate(); err != nil {
log.Printf("validation error in document: %s", err)
}
Expand All @@ -395,6 +404,14 @@ func (p *Presentation) Save(w io.Writer) error {
r.Properties().SetSolidFill(color.Red)
}

if isTemplate {
p.ContentTypes.RemoveOverride("application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml")
p.ContentTypes.EnsureOverride("/ppt/presentation.xml", "application/vnd.openxmlformats-officedocument.presentationml.template.main+xml")
} else {
p.ContentTypes.RemoveOverride("application/vnd.openxmlformats-officedocument.presentationml.template.main+xml")
p.ContentTypes.EnsureOverride("/ppt/presentation.xml", "application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml")
}

dt := unioffice.DocTypePresentation

z := zip.NewWriter(w)
Expand Down Expand Up @@ -476,12 +493,21 @@ func (p *Presentation) Save(w io.Writer) error {

// SaveToFile writes the Presentation out to a file.
func (p *Presentation) SaveToFile(path string) error {
return p.saveToFile(path, false)
}

// SaveToFileAsTemplate writes the Presentation out to a file as a template.
func (p *Presentation) SaveToFileAsTemplate(path string) error {
return p.saveToFile(path, true)
}

func (p *Presentation) saveToFile(path string, isTemplate bool) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
return p.Save(f)
return p.save(f, isTemplate)
}

func (p *Presentation) Validate() error {
Expand Down