unioffice/document/header.go
Gunnsteinn Hall 5335bf249b
License update (#426)
* update license and terms

* Fixes

* Create ACKNOWLEDGEMENTS.md

* Update ACKNOWLEDGEMENTS.md

* Revert go.mod changes and remove go1.11 tests
2020-08-08 01:01:05 +00:00

131 lines
3.1 KiB
Go

// Copyright 2017 FoxyUtils ehf. All rights reserved.
//
// Use of this software package and source code is governed by the terms of the
// UniDoc End User License Agreement (EULA) that is available at:
// https://unidoc.io/eula/
// A trial license code for evaluation can be obtained at https://unidoc.io.
package document
import (
"errors"
"fmt"
"github.com/unidoc/unioffice"
"github.com/unidoc/unioffice/common"
"github.com/unidoc/unioffice/schema/soo/wml"
)
// Header is a header for a document section.
type Header struct {
d *Document
x *wml.Hdr
}
// X returns the inner wrapped XML type.
func (h Header) X() *wml.Hdr {
return h.x
}
// AddParagraph adds a paragraph to the header.
func (h Header) AddParagraph() Paragraph {
bc := wml.NewEG_ContentBlockContent()
h.x.EG_ContentBlockContent = append(h.x.EG_ContentBlockContent, bc)
p := wml.NewCT_P()
bc.P = append(bc.P, p)
return Paragraph{h.d, p}
}
// Index returns the index of the header within the document. This is used to
// form its zip packaged filename as well as to match it with its relationship
// ID.
func (h Header) Index() int {
for i, hdr := range h.d.headers {
if hdr == h.x {
return i
}
}
return -1
}
// Tables returns the tables defined in the header.
func (h Header) Tables() []Table {
ret := []Table{}
if h.x == nil {
return nil
}
for _, c := range h.x.EG_ContentBlockContent {
for _, t := range h.d.tables(c) {
ret = append(ret, t)
}
}
return ret
}
// Paragraphs returns the paragraphs defined in a header.
func (h Header) Paragraphs() []Paragraph {
ret := []Paragraph{}
for _, ec := range h.x.EG_ContentBlockContent {
for _, p := range ec.P {
ret = append(ret, Paragraph{h.d, p})
}
}
for _, t := range h.Tables() {
for _, r := range t.Rows() {
for _, c := range r.Cells() {
ret = append(ret, c.Paragraphs()...)
}
}
}
return ret
}
// RemoveParagraph removes a paragraph from a footer.
func (h Header) RemoveParagraph(p Paragraph) {
for _, ec := range h.x.EG_ContentBlockContent {
for i, pa := range ec.P {
// do we need to remove this paragraph
if pa == p.x {
copy(ec.P[i:], ec.P[i+1:])
ec.P = ec.P[0 : len(ec.P)-1]
return
}
}
}
}
// Clear clears all content within a header
func (h Header) Clear() {
h.x.EG_ContentBlockContent = nil
}
// AddImage adds an image to the document package, returning a reference that
// can be used to add the image to a run and place it in the document contents.
func (h Header) AddImage(i common.Image) (common.ImageRef, error) {
var hdrRels common.Relationships
for i, hdr := range h.d.headers {
if hdr == h.x {
hdrRels = h.d.hdrRels[i]
}
}
r := common.MakeImageRef(i, &h.d.DocBase, hdrRels)
if i.Data == nil && i.Path == "" {
return r, errors.New("image must have data or a path")
}
if i.Format == "" {
return r, errors.New("image must have a valid format")
}
if i.Size.X == 0 || i.Size.Y == 0 {
return r, errors.New("image must have a valid size")
}
h.d.Images = append(h.d.Images, r)
fn := fmt.Sprintf("media/image%d.%s", len(h.d.Images), i.Format)
rel := hdrRels.AddRelationship(fn, unioffice.ImageType)
r.SetRelID(rel.X().IdAttr)
return r, nil
}