mirror of
https://github.com/dolanmiu/docx.git
synced 2026-11-30 16:47:55 +01:00
* chore: organise the demos into a folder for each topic The demos were one flat list of 124 numbered files, which made it hard to find the one you wanted. Each is now in a folder for its topic, such as demo/tables/basic-table.ts, with a name that says what it shows instead of a number. npm run demo lists every demo and narrows the list as you type. It also takes a demo's path or name, a topic, or search words. The demos' images moved to demo/assets/images, as images is now a topic. The docs' links, the Demos workflow, the shape render script and its reference images use the new paths and names. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ * fix: list only a topic's own demos, and drop stale output file names npm run demo text also offered the text-boxes demos and others with "text" in their name, so a topic now searches for "text/". Three demos logged their old numbered file name after writing My Document.docx. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
118 lines
3.2 KiB
Markdown
118 lines
3.2 KiB
Markdown
# Hyperlinks
|
|
|
|
!> Hyperlinks require an understanding of [Paragraphs](usage/paragraph.md) and [Text](usage/text.md).
|
|
|
|
There are two types of hyperlinks: internal (pointing to a bookmark inside the document) and external (pointing to an external url).
|
|
|
|
## Internal
|
|
|
|
To create an internal hyperlink you need first to create a `Bookmark`, which contains the content that will be the destination of the hyperlink.
|
|
|
|
A bookmark is composed of an anchor (an identifier) and the text displayed. After creating a bookmark just add it to a paragraph. For example, creating a bookmarked heading:
|
|
|
|
```ts
|
|
const chapter1 = new Paragraph({
|
|
heading: HeadingLevel.HEADING_1,
|
|
children: [
|
|
new Bookmark({
|
|
id: "anchorForChapter1",
|
|
children: [new TextRun("Chapter 1")],
|
|
}),
|
|
],
|
|
});
|
|
```
|
|
|
|
Then you can create an hyperlink pointing to that bookmark with an `InternalHyperLink`:
|
|
|
|
```ts
|
|
const link = new InternalHyperlink({
|
|
children: [
|
|
new TextRun({
|
|
text: "See Chapter 1",
|
|
style: "Hyperlink",
|
|
}),
|
|
],
|
|
anchor: "anchorForChapter1",
|
|
});
|
|
```
|
|
|
|
### Page reference
|
|
|
|
You can also get the page number of the bookmark by creating a page reference to it:
|
|
|
|
```ts
|
|
const paragraph = new Paragraph({
|
|
children: [new TextRun("Chapter 1 can be seen on page "), new PageReference("anchorForChapter1")],
|
|
});
|
|
```
|
|
|
|
### Numbered item reference
|
|
|
|
You can also create cross references for numbered items with `NumberedItemReference`.
|
|
|
|
```ts
|
|
const paragraph = new Paragraph({
|
|
children: [new TextRun("See Paragraph "), new NumberedItemReference("anchorForParagraph1", "1.1")],
|
|
});
|
|
```
|
|
|
|
> [!NOTE]
|
|
> The `NumberedItemReference` currently needs a cached value (in this case `1.1`)
|
|
|
|
## External
|
|
|
|
To create an external hyperlink you just need to specify the url and the text of the link, then add it to a paragraph:
|
|
|
|
```ts
|
|
const paragraph = new Paragraph({
|
|
children: [
|
|
new ExternalHyperlink({
|
|
children: [
|
|
new TextRun({
|
|
text: "This is an external link!",
|
|
style: "Hyperlink",
|
|
}),
|
|
],
|
|
link: "https://docx.js.org",
|
|
}),
|
|
],
|
|
});
|
|
```
|
|
|
|
## Related Topics
|
|
|
|
- **[Bookmarks](usage/bookmarks.md)** - For detailed coverage of bookmarks, internal hyperlinks, and page references
|
|
|
|
## Demo
|
|
|
|
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/links-and-bookmarks/hyperlinks.ts ":include")
|
|
|
|
_Source: https://github.com/dolanmiu/docx/blob/master/demo/links-and-bookmarks/hyperlinks.ts_
|
|
|
|
## Styling hyperlinks
|
|
|
|
It is possible to set the style of the text of both internal and external hyperlinks. This can be done applying run formatting on any of the `TextRun` children of the hyperlink. Use the `style: "Hyperlink"` property to show the default link styles, which can be combined with any other style.
|
|
|
|
Example:
|
|
|
|
```ts
|
|
const styledLink = new ExternalHyperlink({
|
|
children: [
|
|
new TextRun({
|
|
text: "This is a ",
|
|
style: "Hyperlink",
|
|
}),
|
|
new TextRun({
|
|
text: "bold",
|
|
bold: true,
|
|
style: "Hyperlink",
|
|
}),
|
|
new TextRun({
|
|
text: " link!",
|
|
style: "Hyperlink",
|
|
}),
|
|
],
|
|
link: "https://docx.js.org",
|
|
});
|
|
```
|