mirror of
https://github.com/unidoc/unioffice.git
synced 2025-04-29 13:49:10 +08:00
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:
parent
dff033708f
commit
1f1ff04071
@ -21,14 +21,31 @@ func main() {
|
|||||||
para := doc.AddParagraph()
|
para := doc.AddParagraph()
|
||||||
run := para.AddRun()
|
run := para.AddRun()
|
||||||
run.AddText("Hello World! ")
|
run.AddText("Hello World! ")
|
||||||
|
bm := para.AddBookmark("_bookmark1")
|
||||||
|
addBlankLines(para)
|
||||||
|
|
||||||
|
// first link to a URL
|
||||||
hl := para.AddHyperLink()
|
hl := para.AddHyperLink()
|
||||||
hl.SetTarget("http://www.google.com")
|
hl.SetTarget("http://www.google.com")
|
||||||
|
|
||||||
run = hl.AddRun()
|
run = hl.AddRun()
|
||||||
run.Properties().SetStyle("Hyperlink")
|
run.Properties().SetStyle("Hyperlink")
|
||||||
run.AddText("Click Here to open google.com")
|
run.AddText("Click Here to open google.com")
|
||||||
hl.SetToolTip("hover to see this")
|
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")
|
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
31
document/bookmark.go
Normal 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
|
||||||
|
}
|
@ -28,12 +28,20 @@ func (h HyperLink) X() *wml.CT_Hyperlink {
|
|||||||
// destination will be used many times.
|
// destination will be used many times.
|
||||||
func (h HyperLink) SetTargetByRef(link common.Hyperlink) {
|
func (h HyperLink) SetTargetByRef(link common.Hyperlink) {
|
||||||
h.x.IdAttr = gooxml.String(common.Relationship(link).ID())
|
h.x.IdAttr = gooxml.String(common.Relationship(link).ID())
|
||||||
|
h.x.AnchorAttr = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetTarget sets the URL target of the hyperlink.
|
// SetTarget sets the URL target of the hyperlink.
|
||||||
func (h HyperLink) SetTarget(url string) {
|
func (h HyperLink) SetTarget(url string) {
|
||||||
rel := h.d.AddHyperlink(url)
|
rel := h.d.AddHyperlink(url)
|
||||||
h.x.IdAttr = gooxml.String(common.Relationship(rel).ID())
|
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.
|
// SetToolTip sets the tooltip text for a hyperlink.
|
||||||
|
@ -157,6 +157,7 @@ func (p Paragraph) insertRun(relativeTo Run, before bool) Run {
|
|||||||
return p.AddRun()
|
return p.AddRun()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddHyperLink adds a new hyperlink to a parapgraph.
|
||||||
func (p Paragraph) AddHyperLink() HyperLink {
|
func (p Paragraph) AddHyperLink() HyperLink {
|
||||||
pc := wml.NewEG_PContent()
|
pc := wml.NewEG_PContent()
|
||||||
p.x.EG_PContent = append(p.x.EG_PContent, pc)
|
p.x.EG_PContent = append(p.x.EG_PContent, pc)
|
||||||
@ -164,3 +165,30 @@ func (p Paragraph) AddHyperLink() HyperLink {
|
|||||||
pc.Hyperlink = wml.NewCT_Hyperlink()
|
pc.Hyperlink = wml.NewCT_Hyperlink()
|
||||||
return HyperLink{p.d, pc.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
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user