DOCX

链接

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

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

外部超链接

链接到外部 URL:

{
  "sections": [
    {
      "children": [
        {
          "paragraph": {
            "children": [
              {
                "text": "Visit example.com",
                "hyperlink": { "link": "https://example.com" },
                "underline": { "type": "single" },
                "color": "0563C1"
              }
            ]
          }
        }
      ]
    }
  ]
}

内部超链接

使用 hyperlink: { anchor } 链接到同一文档中的书签:

{
  "sections": [
    {
      "children": [
        {
          "paragraph": {
            "children": [
              {
                "text": "Go to Introduction",
                "hyperlink": { "anchor": "section-intro" },
                "color": "0563C1"
              }
            ]
          }
        }
      ]
    }
  ]
}

书签

bookmark 子元素定义书签,把它放在所标记文字的旁边:

{
  "sections": [
    {
      "children": [
        {
          "paragraph": {
            "children": [
              {
                "bookmark": { "name": "section-intro", "wrap": ["Introduction"] }
              }
            ]
          }
        }
      ]
    }
  ]
}

wrap 是书签范围所包裹的锚定文档内容(行内 run/文本)。

属性类型说明
namestring书签名称(引用句柄)
wrap(string | RunOptions)[]书签所包裹的锚定文字
colFirstnumber表格单元格范围的起始列
colLastnumber表格单元格范围的结束列
displacedByCustomXml"before" | "after"相邻 customXml 的位移方向

显式标记(高级)

当范围跨越多个段落或表格,或需要续接已有文档的 id 时,可显式书写标记。bookmarkStartbookmarkEnd 必须共用同一个 id

import { generateDocument } from "@office-open/docx";

await generateDocument({
  sections: [
    {
      children: [
        {
          paragraph: {
            children: [
              { bookmarkStart: { name: "section-intro", id: 0 } },
              "Introduction",
              { bookmarkEnd: 0 },
            ],
          },
        },
      ],
    },
  ],
});

将书签与 hyperlink: { anchor } 配合使用,可在文档内进行交叉引用。anchor 属性可以引用书签的 name

屏幕提示

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

{
  "sections": [
    {
      "children": [
        {
          "paragraph": {
            "children": [
              {
                "text": "Example Link",
                "hyperlink": { "link": "https://example.com", "tooltip": "Click to visit Example" },
                "color": "0563C1"
              }
            ]
          }
        }
      ]
    }
  ]
}
Copyright © 2026