mirror of
https://github.com/fumiama/go-docx.git
synced 2026-06-25 05:20:46 +08:00
add api AppendFile
This commit is contained in:
52
structdoc.go
52
structdoc.go
@@ -284,23 +284,39 @@ newdoclop:
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Run) copymedia(to *Docx) *Run {
|
||||||
|
nr := *r
|
||||||
|
nr.Children = make([]interface{}, 0, len(r.Children))
|
||||||
|
nr.file = to
|
||||||
|
for _, rc := range r.Children {
|
||||||
|
if d, ok := rc.(*Drawing); ok {
|
||||||
|
nr.Children = append(nr.Children, d.copymedia(to))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
nr.Children = append(nr.Children, rc)
|
||||||
|
}
|
||||||
|
return &nr
|
||||||
|
}
|
||||||
|
|
||||||
func (p *Paragraph) copymedia(to *Docx) (np Paragraph) {
|
func (p *Paragraph) copymedia(to *Docx) (np Paragraph) {
|
||||||
np = *p
|
np = *p
|
||||||
np.Children = make([]interface{}, 0, len(p.Children))
|
np.Children = make([]interface{}, 0, len(p.Children))
|
||||||
np.file = to
|
np.file = to
|
||||||
for _, pc := range p.Children {
|
for _, pc := range p.Children {
|
||||||
if r, ok := pc.(*Run); ok {
|
if r, ok := pc.(*Run); ok {
|
||||||
nr := *r
|
np.Children = append(np.Children, r.copymedia(to))
|
||||||
nr.Children = make([]interface{}, 0, len(r.Children))
|
continue
|
||||||
nr.file = to
|
}
|
||||||
for _, rc := range r.Children {
|
if h, ok := pc.(*Hyperlink); ok {
|
||||||
if d, ok := rc.(*Drawing); ok {
|
tgt, err := p.file.ReferTarget(h.ID)
|
||||||
nr.Children = append(nr.Children, d.copymedia(to))
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
|
||||||
nr.Children = append(nr.Children, rc)
|
|
||||||
}
|
}
|
||||||
np.Children = append(np.Children, &nr)
|
rid := to.addLinkRelation(tgt)
|
||||||
|
np.Children = append(np.Children, &Hyperlink{
|
||||||
|
ID: rid,
|
||||||
|
Run: *h.Run.copymedia(to),
|
||||||
|
})
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
np.Children = append(np.Children, pc)
|
np.Children = append(np.Children, pc)
|
||||||
@@ -330,3 +346,19 @@ func (t *Table) copymedia(to *Docx) (nt Table) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AppendFile appends all contents in af to f
|
||||||
|
func (f *Docx) AppendFile(af *Docx) {
|
||||||
|
for _, item := range af.Document.Body.Items {
|
||||||
|
switch o := item.(type) {
|
||||||
|
case *Paragraph:
|
||||||
|
np := o.copymedia(f)
|
||||||
|
f.Document.Body.Items = append(f.Document.Body.Items, &np)
|
||||||
|
case *Table:
|
||||||
|
nt := o.copymedia(f)
|
||||||
|
f.Document.Body.Items = append(f.Document.Body.Items, &nt)
|
||||||
|
default:
|
||||||
|
f.Document.Body.Items = append(f.Document.Body.Items, o)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
22
unpack.go
22
unpack.go
@@ -23,7 +23,9 @@ package docx
|
|||||||
import (
|
import (
|
||||||
"archive/zip"
|
"archive/zip"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -108,9 +110,23 @@ func (f *Docx) parseDocRelation(file *zip.File) error {
|
|||||||
defer zf.Close()
|
defer zf.Close()
|
||||||
|
|
||||||
f.docRelation.Xmlns = XMLNS_R
|
f.docRelation.Xmlns = XMLNS_R
|
||||||
//TODO: find last rID
|
err = xml.NewDecoder(zf).Decode(&f.docRelation)
|
||||||
f.rID = 100000
|
if err != nil {
|
||||||
return xml.NewDecoder(zf).Decode(&f.docRelation)
|
return err
|
||||||
|
}
|
||||||
|
for _, r := range f.docRelation.Relationship {
|
||||||
|
if !strings.HasPrefix(r.ID, "rId") {
|
||||||
|
return errors.New("invalid rel ID: " + r.ID)
|
||||||
|
}
|
||||||
|
id, err := strconv.ParseUint(r.ID[3:], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if f.rID < uintptr(id) {
|
||||||
|
f.rID = uintptr(id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseMedia add the media into Docx struct
|
// parseMedia add the media into Docx struct
|
||||||
|
|||||||
Reference in New Issue
Block a user