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

add links to paragraph and setting font #55

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion common/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var (

const (
XMLNS = `http://schemas.openxmlformats.org/package/2006/relationships`
HyperLinkStyle = "a1"
HyperLinkStyle = "Hyperlink"
)

const (
Expand Down
189 changes: 189 additions & 0 deletions docx/hyperlink.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package docx

import (
"github.com/gomutex/godocx/wml/ctypes"
"github.com/gomutex/godocx/wml/stypes"
)

type Hyperlink struct {
root *RootDoc // root is the root document to which this hyperlink belongs.
ct *ctypes.Hyperlink // ct is the underlying hyperlink element from the wml/ctypes package.
}

func newHyperlink(root *RootDoc, ct *ctypes.Hyperlink) *Hyperlink {
return &Hyperlink{root: root, ct: ct}
}

// getProp returns the hyperlink properties. If not initialized, it creates and returns a new instance.
func (r *Hyperlink) getProp() *ctypes.RunProperty {
if r.ct.Run.Property == nil {
r.ct.Run.Property = &ctypes.RunProperty{}
}
return r.ct.Run.Property
}

// Sets the color of the Hyperlink.
//
// Example:
//
// modifiedHyperlink := hyperlink.Color("FF0000")
//
// Parameters:
// - colorCode: A string representing the color code (e.g., "FF0000" for red).
//
// Returns:
// - *Hyperlink: The modified Hyperlink instance with the updated color.
func (r *Hyperlink) Color(colorCode string) *Hyperlink {
r.getProp().Color = ctypes.NewColor(colorCode)
return r
}

// Sets the size of the Hyperlink.

// This method takes an integer parameter representing the desired font size.
// It updates the size property of the Hyperlink instance with the specified size,
// Example:

// modifiedHyperlink := hyperlink.Size(12)

// Parameters:
// - size: An integer representing the font size.

// Returns:
// - *Hyperlink: The modified Hyperlink instance with the updated size.
func (r *Hyperlink) Size(size uint64) *Hyperlink {
r.getProp().Size = ctypes.NewFontSize(size * 2)
return r
}

// Font sets the font for the hyperlink.
func (r *Hyperlink) Font(font string) *Hyperlink {
if r.getProp().Fonts == nil {
r.getProp().Fonts = &ctypes.RunFonts{}
}

r.getProp().Fonts.Ascii = font
r.getProp().Fonts.HAnsi = font
return r
}

// Shading sets the shading properties (type, color, fill) for the hyperlink
func (r *Hyperlink) Shading(shdType stypes.Shading, color, fill string) *Hyperlink {
r.getProp().Shading = ctypes.NewShading().SetShadingType(shdType).SetColor(color).SetFill(fill)
return r
}

// AddHighlight sets the highlight color for the hyperlink.
func (r *Hyperlink) Highlight(color string) *Hyperlink {
r.getProp().Highlight = ctypes.NewCTString(color)
return r
}

// AddBold enables bold formatting for the hyperlink.
func (r *Hyperlink) Bold(value bool) *Hyperlink {
r.getProp().Bold = ctypes.OnOffFromBool(value)
return r
}

// Italic enables or disables italic formatting for the hyperlink.
func (r *Hyperlink) Italic(value bool) *Hyperlink {
r.getProp().Italic = ctypes.OnOffFromBool(value)
return r
}

// Specifies that the contents of this hyperlink shall be displayed with a single horizontal line through the center of the line.
func (r *Hyperlink) Strike(value bool) *Hyperlink {
r.getProp().Strike = ctypes.OnOffFromBool(value)
return r
}

// Specifies that the contents of this hyperlink shall be displayed with two horizontal lines through each character displayed on the line
func (r *Hyperlink) DoubleStrike(value bool) *Hyperlink {
r.getProp().DoubleStrike = ctypes.OnOffFromBool(value)
return r
}

// Display All Characters As Capital Letters
// Any lowercase characters in this text hyperlink shall be formatted for display only as their capital letter character equivalents
func (r *Hyperlink) Caps(value bool) *Hyperlink {
r.getProp().Caps = ctypes.OnOffFromBool(value)
return r
}

// Specifies that all small letter characters in this text hyperlink shall be formatted for display only as their capital letter character equivalents
func (r *Hyperlink) SmallCaps(value bool) *Hyperlink {
r.getProp().Caps = ctypes.OnOffFromBool(value)
return r
}

// Outline enables or disables outline formatting for the hyperlink.
func (r *Hyperlink) Outline(value bool) *Hyperlink {
r.getProp().Outline = ctypes.OnOffFromBool(value)
return r
}

// Shadow enables or disables shadow formatting for the hyperlink.
func (r *Hyperlink) Shadow(value bool) *Hyperlink {
r.getProp().Shadow = ctypes.OnOffFromBool(value)
return r
}

// Emboss enables or disables embossing formatting for the hyperlink.
func (r *Hyperlink) Emboss(value bool) *Hyperlink {
r.getProp().Emboss = ctypes.OnOffFromBool(value)
return r
}

// Imprint enables or disables imprint formatting for the hyperlink.
func (r *Hyperlink) Imprint(value bool) *Hyperlink {
r.getProp().Imprint = ctypes.OnOffFromBool(value)
return r
}

// Do Not Check Spelling or Grammar
func (r *Hyperlink) NoGrammer(value bool) *Hyperlink {
r.getProp().NoGrammar = ctypes.OnOffFromBool(value)
return r
}

// Use Document Grid Settings For Inter-Character Spacing
func (r *Hyperlink) SnapToGrid(value bool) *Hyperlink {
r.getProp().SnapToGrid = ctypes.OnOffFromBool(value)
return r
}

// Hidden Text
func (r *Hyperlink) HideText(value bool) *Hyperlink {
r.getProp().Vanish = ctypes.OnOffFromBool(value)
return r
}

// Spacing sets the spacing between characters in the hyperlink.
func (r *Hyperlink) Spacing(value int) *Hyperlink {
r.getProp().Spacing = ctypes.NewDecimalNum(value)
return r
}

// Underline sets the underline style for the hyperlink.
func (r *Hyperlink) Underline(value stypes.Underline) *Hyperlink {
r.getProp().Underline = ctypes.NewGenSingleStrVal(value)
return r
}

// Style sets the style of the Hyperlink.
func (r *Hyperlink) Style(value string) *Hyperlink {
r.getProp().Style = ctypes.NewRunStyle(value)
return r
}

// VerticalAlign sets the vertical alignment for the hyperlink text.
//
// Parameter: A value from the stypes.VerticalAlignRun type indicating the desired vertical alignment. One of:
//
// VerticalAlignRunBaseline, VerticalAlignRunSuperscript, VerticalAlignRunSubscript
//
// Returns: The modified Hyperlink instance with the updated vertical alignment.
func (r *Hyperlink) VerticalAlign(value stypes.VerticalAlignRun) *Hyperlink {
r.getProp().VertAlign = ctypes.NewGenSingleStrVal(value)
return r
}
54 changes: 25 additions & 29 deletions docx/paragraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,35 +223,31 @@ func (p *Paragraph) GetStyle() (*ctypes.Style, error) {
return style, nil
}

// func (p *Paragraph) AddLink(text string, link string) *Hyperlink {
// rId := p.rootRef.addLinkRelation(link)

// runChildren := []*RunChild{}
// runChildren = append(runChildren, &RunChild{
// InstrText: &text,
// })
// run := &Run{
// Children: runChildren,
// RunProperty: &RunProperty{
// RunStyle: &RunStyle{
// Val: constants.HyperLinkStyle,
// },
// },
// }

// paraChild := &ParagraphChild{
// Run: run,
// }

// hyperLink := &Hyperlink{
// ID: rId,
// }
// hyperLink.Children = append(hyperLink.Children, paraChild)

// p.Children = append(p.Children, &ParagraphChild{Link: hyperLink})

// return hyperLink
// }
func (p *Paragraph) AddLink(text string, link string) *Hyperlink {
rId := p.root.Document.addLinkRelation(link)

runChildren := []ctypes.RunChild{}
runChildren = append(runChildren, ctypes.RunChild{
Text: ctypes.TextFromString(text),
})
run := &ctypes.Run{
Children: runChildren,
Property: &ctypes.RunProperty{
Style: &ctypes.CTString{
Val: constants.HyperLinkStyle,
},
},
}

hyperLink := &ctypes.Hyperlink{
ID: rId,
Run: run,
}

p.ct.Children = append(p.ct.Children, ctypes.ParagraphChild{Link: hyperLink})

return newHyperlink(p.root, hyperLink)
}

// AddDrawing adds a new drawing (image) to the Paragraph.
//
Expand Down
11 changes: 11 additions & 0 deletions docx/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ func (r *Run) Size(size uint64) *Run {
return r
}

// Font sets the font for the run.
func (r *Run) Font(font string) *Run {
if r.getProp().Fonts == nil {
r.getProp().Fonts = &ctypes.RunFonts{}
}

r.getProp().Fonts.Ascii = font
r.getProp().Fonts.HAnsi = font
return r
}

// Shading sets the shading properties (type, color, fill) for the run
func (r *Run) Shading(shdType stypes.Shading, color, fill string) *Run {
r.getProp().Shading = ctypes.NewShading().SetShadingType(shdType).SetColor(color).SetFill(fill)
Expand Down
Binary file modified templates/default.docx
Binary file not shown.
6 changes: 3 additions & 3 deletions wml/ctypes/para.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ type ParagraphChild struct {
}

type Hyperlink struct {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main hyperlink,omitempty"`
ID string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr"`
// Run Run
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main hyperlink,omitempty"`
ID string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr"`
Run *Run
Children []ParagraphChild
}

Expand Down