DOCX

批注与修订

文档批注、修订跟踪和结构化文档标签(内容控件)

批注

comment 子元素添加批注,把它放在所批注文字的旁边:

{
  "sections": [
    {
      "children": [
        {
          "paragraph": {
            "children": [
              "这段文字被",
              {
                "comment": {
                  "author": "Author Name",
                  "initials": "AN",
                  "children": ["请审阅此部分。"],
                  "wrap": [{ "text": "批注" }]
                }
              },
              ""
            ]
          }
        }
      ]
    }
  ]
}

children 是批注回复正文(存入 comments part,可为任意段落内容);wrap 是批注范围所覆盖的文档文字(行内 run/文本)。

属性类型说明
authorstring批注作者(默认为空)
initialsstring作者缩写
dateDate | string创建时间(默认为当前)
children(string | ParagraphOptions)[]批注回复正文
wrap(string | RunOptions)[]范围所包裹的锚定文字

显式标记(高级)

当范围跨越多个段落或表格,或需要续接已有文档的 id 时,可显式书写标记。commentRangeStartcommentRangeEndcommentReference 必须共用同一个 id,且与 comments 条目匹配:

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

await generateDocument({
  comments: {
    children: [
      {
        id: 0,
        author: "Author Name",
        date: new Date(),
        children: ["请审阅此部分。"],
      },
    ],
  },
  sections: [
    {
      children: [
        {
          paragraph: {
            children: [
              { commentRangeStart: { id: 0 } },
              "这段文字附带批注。",
              { commentRangeEnd: { id: 0 } },
              { commentReference: 0 },
            ],
          },
        },
      ],
    },
  ],
});

修订跟踪

跟踪文档中的插入、删除和移动:

{
  "sections": [
    {
      "children": [
        {
          "paragraph": {
            "children": [
              "原始文本,",
              {
                "insertion": {
                  "id": 0,
                  "author": "Author",
                  "date": "2024-01-15T10:30:00Z",
                  "text": "插入的文本"
                }
              },
              ",以及",
              {
                "deletion": {
                  "id": 1,
                  "author": "Author",
                  "date": "2024-01-15T10:30:00Z",
                  "text": "删除的文本"
                }
              },
              ""
            ]
          }
        }
      ]
    }
  ]
}

移动修订

moveFrom(源)和 moveTo(目标)子元素跟踪移动的内容。同一次移动的 moveFrommoveTo 必须共用同一个 name,Word 才能将它们配对:

{
  "sections": [
    {
      "children": [
        {
          "paragraph": {
            "children": [
              "这段文字被 ",
              {
                "moveFrom": {
                  "name": "my-move",
                  "author": "Author",
                  "date": "2024-01-15T10:30:00Z",
                  "wrap": [{ "text": "移动的文本" }]
                }
              },
              ""
            ]
          }
        },
        {
          "paragraph": {
            "children": [
              "它出现在这里:",
              {
                "moveTo": {
                  "name": "my-move",
                  "author": "Author",
                  "date": "2024-01-15T10:30:00Z",
                  "wrap": [{ "text": "移动的文本" }]
                }
              },
              ""
            ]
          }
        }
      ]
    }
  ]
}

wrap 是移动 run 所承载的移动内容(行内 run/文本)。authordate 同时作用于范围起始标记和移动 run。

属性类型说明
namestring移动名称——from/to 配对须共用
authorstring移动作者
datestringISO 8601 时间戳
wrap(string | RunOptions)[]移动 run 承载的移动内容
colFirstnumber表格单元格范围的起始列
colLastnumber表格单元格范围的结束列
displacedByCustomXml"before" | "after"相邻 customXml 的位移方向

显式标记(高级)

当需要跨段范围、显式 id 续编或独立放置标记时,可分别书写范围标记与移动 run。moveFromRangeStart/moveFromRangeEnd 标记源、moveToRangeStart/moveToRangeEnd 标记目标;movedFrom/movedTo 以修订 run 的形式承载被移动的文本:

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

await generateDocument({
  sections: [
    {
      children: [
        {
          paragraph: {
            children: [
              {
                moveFromRangeStart: {
                  id: 0,
                  name: "my-move",
                  author: "Author",
                  date: "2024-01-15T10:30:00Z",
                },
              },
              {
                movedFrom: {
                  id: 1,
                  author: "Author",
                  date: "2024-01-15T10:30:00Z",
                  children: ["移动的文本"],
                },
              },
              { moveFromRangeEnd: { id: 0 } },
            ],
          },
        },
        {
          paragraph: {
            children: [
              {
                moveToRangeStart: {
                  id: 2,
                  name: "my-move",
                  author: "Author",
                  date: "2024-01-15T10:30:00Z",
                },
              },
              {
                movedTo: {
                  id: 3,
                  author: "Author",
                  date: "2024-01-15T10:30:00Z",
                  children: ["移动的文本"],
                },
              },
              { moveToRangeEnd: { id: 2 } },
            ],
          },
        },
      ],
    },
  ],
});

范围起始标记接受 id(必填)以及 nameauthordate 和表格列范围(colFirst/colLast);范围结束标记只需 { id }

结构化文档标签(内容控件)

使用 SDT 创建可编辑区域和表单控件:

{
  "sections": [
    {
      "children": [
        {
          "sdt": {
            "properties": { "alias": "FullName", "tag": "full-name", "richText": true },
            "children": [{ "paragraph": { "children": ["John Doe"] } }]
          }
        },
        {
          "sdt": {
            "properties": {
              "alias": "Color",
              "tag": "combo-color",
              "comboBox": {
                "items": [
                  { "displayText": "Red", "value": "red" },
                  { "displayText": "Blue", "value": "blue" }
                ],
                "lastValue": "Red"
              }
            },
            "children": [{ "paragraph": { "children": ["Red"] } }]
          }
        }
      ]
    }
  ]
}

SDT 支持多种控件类型,适用于表单、模板和文档自动化。

文档保护

使用设置限制文档编辑:

{
  "features": {
    "updateFields": true,
    "documentProtection": {
      "edit": "readOnly",
      "password": "123"
    }
  },
  "sections": [
    {
      "children": [{ "paragraph": { "children": ["受保护的文档内容。"] } }]
    }
  ]
}

保护类型:"readOnly""comments""trackedChanges""forms"

提供 password 时,会自动计算带随机盐值和 100,000 次迭代的 SHA-512 哈希。也可以直接提供预计算的哈希值。

选项类型说明
editstring保护类型
passwordstring明文密码(自动计算哈希)
algorithmNamestring哈希算法(如 "SHA-512"
hashValuestringBase64 编码的密码哈希
saltValuestringBase64 编码的盐值
spinCountnumber哈希迭代次数
formattingboolean限制格式化
hashstring旧版密码哈希 (w:hash)
saltstring旧版密码盐值 (w:salt)
cryptoAlgorithmClassstring加密算法类别
cryptoAlgorithmSidnumber加密算法 SID
cryptoAlgorithmTypestring加密算法类型
cryptoProviderstring加密提供程序
cryptoProviderTypestring加密提供程序类型
cryptoProviderTypeExtensionnumber提供程序类型扩展
cryptoProviderTypeExtensionSourcestring提供程序类型扩展源
algorithmExtensionIdnumber算法扩展 ID
algorithmExtensionSourcestring算法扩展源
cryptoSpinCountnumber旧版加密迭代次数

修订跟踪选项

使用 insertiondeletion 时的选项:

选项类型说明
idnumber修订 ID
authorstring修订作者名称
datestringISO 8601 时间戳

两者也接受所有 TextRun 属性(textbolditaliccolor 等)。

Copyright © 2026