document: add support for creating bookmarks

- support adding bookmarks
- support hyperlinks that reference a bookmark
- update hyperlink example

Fixes #126
This commit is contained in:
Todd 2017-12-01 12:13:19 -06:00
parent dff033708f
commit 1f1ff04071
4 changed files with 85 additions and 1 deletions

View File

@ -21,14 +21,31 @@ func main() {
para := doc.AddParagraph()
run := para.AddRun()
run.AddText("Hello World! ")
bm := para.AddBookmark("_bookmark1")
addBlankLines(para)
// first link to a URL
hl := para.AddHyperLink()
hl.SetTarget("http://www.google.com")
run = hl.AddRun()
run.Properties().SetStyle("Hyperlink")
run.AddText("Click Here to open google.com")
hl.SetToolTip("hover to see this")
addBlankLines(para)
// second link to a bookmark
hl = para.AddHyperLink()
hl.SetTargetBookmark(bm)
run = hl.AddRun()
run.AddText("Click Here to jump to the bookmark")
doc.SaveToFile("hyperlink.docx")
}
func addBlankLines(p document.Paragraph) {
run := p.AddRun()
for i := 0; i < 4; i++ {
run.AddBreak()
}
}

31
document/bookmark.go Normal file
View File

@ -0,0 +1,31 @@
// Copyright 2017 Baliance. All rights reserved.
//
// Use of this source code is governed by the terms of the Affero GNU General
// Public License version 3.0 as published by the Free Software Foundation and
// appearing in the file LICENSE included in the packaging of this file. A
// commercial license can be purchased by contacting sales@baliance.com.
package document
import "baliance.com/gooxml/schema/soo/wml"
type Bookmark struct {
x *wml.CT_Bookmark
}
// X returns the inner wrapped XML type.
func (b Bookmark) X() *wml.CT_Bookmark {
return b.x
}
// SetName sets the name of the bookmark. This is the name that is used to
// reference the bookmark from hyperlinks.
func (b Bookmark) SetName(name string) {
b.x.NameAttr = name
}
// Name returns the name of the bookmark whcih is the document unique ID that
// identifies the bookmark.
func (b Bookmark) Name() string {
return b.x.NameAttr
}

View File

@ -28,12 +28,20 @@ func (h HyperLink) X() *wml.CT_Hyperlink {
// destination will be used many times.
func (h HyperLink) SetTargetByRef(link common.Hyperlink) {
h.x.IdAttr = gooxml.String(common.Relationship(link).ID())
h.x.AnchorAttr = nil
}
// SetTarget sets the URL target of the hyperlink.
func (h HyperLink) SetTarget(url string) {
rel := h.d.AddHyperlink(url)
h.x.IdAttr = gooxml.String(common.Relationship(rel).ID())
h.x.AnchorAttr = nil
}
// SetTargetBookmark sets the bookmark target of the hyperlink.
func (h HyperLink) SetTargetBookmark(bm Bookmark) {
h.x.AnchorAttr = gooxml.String(bm.Name())
h.x.IdAttr = nil
}
// SetToolTip sets the tooltip text for a hyperlink.

View File

@ -157,6 +157,7 @@ func (p Paragraph) insertRun(relativeTo Run, before bool) Run {
return p.AddRun()
}
// AddHyperLink adds a new hyperlink to a parapgraph.
func (p Paragraph) AddHyperLink() HyperLink {
pc := wml.NewEG_PContent()
p.x.EG_PContent = append(p.x.EG_PContent, pc)
@ -164,3 +165,30 @@ func (p Paragraph) AddHyperLink() HyperLink {
pc.Hyperlink = wml.NewCT_Hyperlink()
return HyperLink{p.d, pc.Hyperlink}
}
// AddBookmark adds a bookmark to a document that can then be used from a hyperlink. Name is a document
// unique name that identifies the bookmark so it can be referenced from hyperlinks.
func (p Paragraph) AddBookmark(name string) Bookmark {
pc := wml.NewEG_PContent()
rc := wml.NewEG_ContentRunContent()
pc.EG_ContentRunContent = append(pc.EG_ContentRunContent, rc)
relt := wml.NewEG_RunLevelElts()
rc.EG_RunLevelElts = append(rc.EG_RunLevelElts, relt)
markEl := wml.NewEG_RangeMarkupElements()
bmStart := wml.NewCT_Bookmark()
markEl.BookmarkStart = bmStart
relt.EG_RangeMarkupElements = append(relt.EG_RangeMarkupElements, markEl)
markEl = wml.NewEG_RangeMarkupElements()
markEl.BookmarkEnd = wml.NewCT_MarkupRange()
relt.EG_RangeMarkupElements = append(relt.EG_RangeMarkupElements, markEl)
p.x.EG_PContent = append(p.x.EG_PContent, pc)
bm := Bookmark{bmStart}
bm.SetName(name)
return bm
}