DOCX

链接

Word 文档中的外部超链接、书签和交叉引用

通过超链接和书签为文档添加导航功能。

外部超链接

链接到外部 URL:

import { Paragraph, ExternalHyperlink, TextRun } from "@office-open/docx";

new Paragraph({
    children: [
        new ExternalHyperlink({
            link: "https://example.com",
            children: [
                new TextRun({
                    text: "Visit example.com",
                    underline: { type: "single" },
                    color: "0563C1",
                }),
            ],
        }),
    ],
});

内部超链接

链接到同一文档中的书签:

import { Paragraph, ExternalHyperlink, TextRun } from "@office-open/docx";

new Paragraph({
    children: [
        new ExternalHyperlink({
            link: "https://example.com",
            anchor: "section-intro",
            children: [new TextRun({ text: "Go to Introduction", color: "0563C1" })],
        }),
    ],
});

书签

定义可被链接的书签:

import { Paragraph, Bookmark, TextRun } from "@office-open/docx";

new Paragraph({
    children: [new Bookmark({ id: "section-intro" }), new TextRun("Introduction")],
});

将书签与内部超链接配合使用,可在文档内进行交叉引用。ExternalHyperlinkanchor 属性可以引用书签 ID。

屏幕提示

添加悬停时显示的工具提示:

new ExternalHyperlink({
    link: "https://example.com",
    tooltip: "Click to visit Example",
    children: [new TextRun({ text: "Example Link", color: "0563C1" })],
});
Copyright © 2026