DOCX
文本框
带 VML 定位和样式的浮动文本框
文本框是通过 VML(v:shape)渲染的浮动容器。使用 textbox 键作为节子元素添加——它接受形状定位(style)、段落级属性(边框、底纹)和 children(正文内容)。
基本文本框
{
"sections": [
{
"children": [
{ "paragraph": { "children": ["文本框旁的正文段落。"] } },
{
"textbox": {
"style": {
"width": "2in",
"height": "1in",
"position": "absolute",
"marginLeft": "1in",
"marginTop": "0.5in",
"wrapStyle": "square"
},
"children": [{ "paragraph": { "children": ["浮动框内的文本。"] } }]
}
}
]
}
]
}
import { generateDocument } from "@office-open/docx";
await generateDocument({
sections: [
{
children: [
{ paragraph: { children: ["文本框旁的正文段落。"] } },
{
textbox: {
style: {
width: "2in",
height: "1in",
position: "absolute",
marginLeft: "1in",
marginTop: "0.5in",
wrapStyle: "square",
},
children: [{ paragraph: { children: ["浮动框内的文本。"] } }],
},
},
],
},
],
});
文本框选项
textbox 是一个节子元素,结合段落级属性与形状几何:
| 选项 | 类型 | 说明 |
|---|---|---|
style | VmlShapeStyle | 形状定位、尺寸、环绕和旋转(见下表) |
children | SectionChild[] | 正文内容——段落、表格等(节接受的任何内容) |
| 段落属性 | 各种 | 边框、底纹及其他 ParagraphOptions(除 style/children) |
VmlShapeStyle
控制浮动形状的布局。长度值接受数字(磅)、UniversalMeasure 字符串("2in"、"5cm")、百分比或 "auto"。
| 属性 | 类型 | 说明 |
|---|---|---|
width | LengthUnit | 容器块宽度(必填) |
height | LengthUnit | 容器块高度 |
left、top | LengthUnit | 块位置 |
marginLeft/Right/Top/Bottom | LengthUnit | 相对于形状锚点的边距 |
position | "static" | "absolute" | "relative" | 定位方案 |
positionHorizontal | "absolute" | "left" | "center" | "right" | "inside" | "outside" | 水平锚点 |
positionHorizontalRelative | "margin" | "page" | "text" | "char" | 水平锚点参照 |
positionVertical | "absolute" | "left" | "center" | "right" | "inside" | "outside" | 垂直锚点 |
positionVerticalRelative | "margin" | "page" | "text" | "char" | 垂直锚点参照 |
wrapStyle | "square" | "none" | 文本环绕方式 |
wrapDistanceTop/Bottom/Left/Right | number | 形状到环绕文本的距离(磅) |
rotation | number | 旋转角度(度) |
flip | "x" | "y" | "xy" | "yx" | 方向翻转 |
visibility | "hidden" | "inherit" | 显示状态 |
zIndex | "auto" | number | 重叠顺序 |
内容
children 接受与文档正文相同的节子元素——段落、表格甚至其他文本框——因此文本框可容纳格式丰富的内容:
{
textbox: {
style: { width: "3in", height: "2in" },
children: [
{ paragraph: { children: [{ text: "标题", bold: true, size: 28 }] } },
{ paragraph: { children: ["框内的支持段落。"] } },
],
},
}