How to Add Hyperlinks and Bookmarks to Your Own Document
Hyperlinks and bookmarks are essential tools when it comes to crafting professional, navigable documents. Whether you’re linking to external websites or navigating within the same file, these features make your content more dynamic and accessible.
In this complete guide, you’ll learn how to add a hyperlink in Word, create bookmarks, and implement both using the UniOffice SDK. Plus, we’ll show you how to style these links and target them effectively.
Let’s explore how to elevate your document experience with just a few lines of Go code.
Why Use Hyperlinks and Bookmarks?
Hyperlinks and bookmarks improve document usability.
Hyperlinks connect your Word document to websites, email addresses, or other sections within the file.
Bookmarks allow you to tag specific content, enabling easy navigation via links.
Combining the two creates a seamless reading experience for your audience.
Before You Begin
Before diving into the code, you’ll need a few prerequisites:
A valid UniCloud account to get your API key.
A local Go development environment. If you’re new, refer to the official UniOffice SDK setup guide.
Once you’re set up, you’re ready to create engaging documents with hyperlink bookmark functionality.
Clone the Example Code
Start by cloning the UniOffice examples repository from GitHub.
git clone github.com https://github.com/unidoc/unioffice-examples
Navigate to the correct directory:
cd unioffice-examples/document/hyperlink/
Here you’ll find everything needed to insert hyperlinks and bookmarks using Go.
Setting Up: Required Imports
Let’s break down the imports you’ll use to build this document automation.
import (
"os"
"github.com/unidoc/unioffice/v2/color"
"github.com/unidoc/unioffice/v2/common/license"
"github.com/unidoc/unioffice/v2/document"
"github.com/unidoc/unioffice/v2/schema/soo/wml"
)
These packages enable license authentication, color styling, document creation, and working with XML schema for Word.
Authenticating Your License
Use your UNIDOC_LICENSE_API_KEY to authenticate.
func init() {
err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
if err != nil {
panic(err)
}
}
This ensures that your UniOffice operations are authorized.
Create and Style Your Hyperlink
Let’s walk through how to create a hyperlink in Word using code.
We’ll also define a hyperlink style for consistency.
doc := document.New()
defer doc.Close()
hlStyle := doc.Styles.AddStyle("Hyperlink", wml.ST_StyleTypeCharacter, false)
hlStyle.SetName("Hyperlink")
hlStyle.SetBasedOn("DefaultParagraphFont")
hlStyle.RunProperties().Color().SetThemeColor(wml.ST_ThemeColorHyperlink)
clr := color.FromHex("#0563C1")
hlStyle.RunProperties().Color().SetColor(clr)
hlStyle.RunProperties().SetUnderline(wml.ST_UnderlineSingle, clr)
This section handles everything from style name to font color, underline, and more.
Inserting a Paragraph with Bookmark
Now let’s insert a paragraph and create a bookmark.
This is useful if you want to link to a specific section in your document.
para := doc.AddParagraph()
run := para.()
run.AddText("Hello World! ")
bm := para.AddBookmark("_bookmark1")
This snippet does two things: writes text and sets a bookmark labeled _bookmark1.
Now we’re ready to insert a link in Word.
Add Hyperlink to an External URL
Let’s cover how to hyperlink in Word to an external website like Google.
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")
This snippet demonstrates how to insert a link with a custom tooltip, text, and target URL.
The result: a clean, styled external hyperlink
Add Hyperlink to a Bookmark
Next, link back to the bookmark we created earlier.
This is where you’ll see the power of hyperlink bookmark integration.
addBlankLines(para)
hl = para.AddHyperLink()
hl.SetTargetBookmark(bm)
run = hl.AddRun()
run.AddText("Click Here to jump to the bookmark")
By using SetTargetBookmark(bm)
, you create a direct jump to the “Hello World!” text.
This is how to insert hyperlink in Word documents for internal navigation.
Insert Blank Lines (Helper Function)
To space out your elements, add this utility function.
func addBlankLines(p document.Paragraph) {
run := p.AddRun()
for i := 0; i < 4; i++ {
run.AddBreak()
}
}
It’s a small touch, but it helps structure your content clearly.
Save the Word File
Once your hyperlinks and bookmarks are in place, save the document.
doc.SaveToFile("hyperlink.docx")
You’ve just created a styled Word document using Go that includes both inserted hyperlinks
and bookmarks
.
Run the Code
To generate the document, use the following terminal command:
go run main.go
This will output a file named hyperlink.docx
with functional links and bookmarks.
Open it in Microsoft Word to test navigation and verify styles.
What’s Inside the Output File?
Your Word document will have:
A paragraph titled “Hello World!”
A hyperlink to Google
A hyperlink to the “Hello World!” bookmark
Styled links with tooltip support
Structured formatting using blank lines
The result is a clean, interactive, and professional document.
Best Practices for Hyperlinks and Bookmarks
When working with headers and footers in Word, or just general content:
Always use descriptive text in hyperlinks.
Keep bookmark names short but meaningful.
Avoid using raw URLs as link text.
Group related links under headings or sections.
Style your hyperlinks consistently across the document.
These tips ensure that readers can navigate your file effortlessly.
Advanced Ideas
You can go even further by:
Linking to other Word documents or PDFs
Embedding mailto: links for email addresses
Creating a dynamic table of contents using bookmarks
Linking to document sections within headers and footers
If you’re also working with headers and footers, read our complete guide on adding headers and footers in Word.
Common Questions
How to create a hyperlink in Word manually?
In Microsoft Word:
Select the text.
Press
Ctrl + K
or right-click and choose Link.Enter the URL or bookmark name.
How to add a bookmark in Word?
Select the text you want to bookmark.
Go to
Insert
→Bookmark
.Give it a name and click Add.
Can I link bookmarks inside headers or footers?
Yes. Bookmarks can be linked from anywhere in the document, including within headers and footers.
Use UniOffice for Automating Word Documents
UniOffice SDK gives you full control to automate Word document creation, styling, and navigation.
This includes adding:
Hyperlinks to websites
Internal bookmarks
Headers, footers, tables, images, and more
Explore more examples in the UniOffice GitHub repository.
Final Thoughts
Now that you know how to insert hyperlinks, how to create a bookmark in Word, and combine the two—you’re ready to build truly interactive documents.
With the UniOffice SDK, you can automate every element of document formatting, structure, and interactivity using Go.