DOCX
媒体
Word 文档中的嵌入式媒体支持
Word 文档可以嵌入多种媒体类型。本包通过 ImageRun 提供图片支持,这是 DOCX 文件中使用的主要媒体类型。
支持的媒体类型
| 类型 | 格式 | 组件 |
|---|---|---|
| 栅格图片 | JPG, PNG, GIF, BMP, TIF, ICO | ImageRun |
| 矢量图片 | SVG(带回退) | ImageRun |
| EMF/WMF | EMF, WMF | ImageRun |
图片插入
图片是最常用的嵌入媒体。
import { Paragraph, ImageRun } from "@office-open/docx";
new Paragraph({
children: [
new ImageRun({
type: "png",
data: imageBuffer,
transformation: { width: 200, height: 150 },
}),
],
});
视频和音频
OOXML 规范支持通过媒体关系嵌入视频和音频,但播放取决于打开文档的应用程序。对于复杂的媒体嵌入场景,可以考虑:
- 使用
Media类管理媒体关系 - 将视频缩略图作为图片嵌入,并添加指向在线视频 URL 的超链接
ImageRun 选项
| 选项 | 类型 | 说明 |
|---|---|---|
type | string | 图片格式("png"、"jpg"、"gif"、"bmp"、"svg") |
data | Buffer | Uint8Array | string | 图片数据(缓冲区或 base64) |
transformation | object | { width, height }(像素) |
floating | object | 浮动定位(锚定或内联) |
altText | object | { title, description } 用于无障碍 |
浮动图片
使用浮动模式在页面上自由定位图片:
new Paragraph({
children: [
new ImageRun({
type: "png",
data: imageBuffer,
transformation: { width: 200, height: 150 },
floating: {
horizontalPosition: { relative: "page", align: "center" },
verticalPosition: { relative: "page", offset: 1440 },
wrap: { type: "none" },
},
}),
],
});