幻灯片上的标题、说明与要点大多以文字呈现。若逐页手动插入文本框并调整格式,既费时,也难以在批量生成的文档中保持一致;而把文字直接写进固定的版式占位符,位置与样式的灵活性又受到限制。借助脚本创建文本框,可以在生成演示文稿的同时写入文字并控制排版,让内容与样式稳定复用。
本文使用 Spire.Presentation for JavaScript 在浏览器中向 PowerPoint 幻灯片添加文本框。整个处理过程基于 WebAssembly,在用户设备本地完成,无需安装 Office,也不依赖后端服务,演示文稿不必离开浏览器。
本文介绍两个核心功能点:
有关安装和项目配置,请参考 React 项目中集成 Spire.Presentation for JavaScript。以下示例默认 Spire.Presentation for JavaScript 已安装、WebAssembly 模块已初始化。
在 PowerPoint 幻灯片中添加文本框
文本框承载幻灯片上相对独立的文字,可以按需要放到页面的任意位置,不受版式占位符的限制。添加文本框时,先确定它的位置与大小,再写入文字,内容才能准确落在预期区域;去掉默认的填充与边框后,文本框只呈现文字本身。
| 写法 | 得到的结果 |
|---|---|
slide.Shapes.AppendShape({ shapeType, rectangle }) |
在指定区域内添加一个矩形形状,作为文本框使用 |
RectangleF.FromLTRB(left, top, right, bottom) |
用左上角与右下角坐标定义文本框的位置和大小 |
shape.Fill.FillType = FillFormatType.None |
去掉文本框的填充色 |
shape.Line.FillType = FillFormatType.None |
去掉文本框的边框 |
shape.AppendTextFrame(text) |
向文本框写入一段文字 |
shape.TextFrame.TextRange |
取得文本框中的文字,用于设置字号与颜色 |
shape.TextFrame.Paragraphs.get_Item(index).Alignment |
设置段落在文本框中的水平对齐方式 |
具体操作步骤如下:
- 获取 Spire.Presentation 的 WASM 模块并确认已就绪,将字体文件载入 VFS,再创建
Presentation对象。 - 通过
Presentation.Slides.get_Item(0)取得第一张幻灯片。 - 使用
Shapes.AppendShape()添加ShapeType.Rectangle形状,并用RectangleF.FromLTRB()定义它的位置与大小。 - 将
Fill.FillType与Line.FillType设为FillFormatType.None,去掉文本框的填充与边框。 - 调用
shape.AppendTextFrame()写入文字,再通过TextFrame.TextRange设置字号与文字颜色,并设置段落居中。 - 调用
Presentation.SaveToFile()保存结果文件,并用Dispose()释放演示文稿,随后从 VFS 读出文件并触发下载。
在 PowerPoint 幻灯片中添加文本框的代码如下:
function App() {
const addTextBox = async () => {
// 获取 Spire.Presentation WASM 模块
const pptModule = window.wasmModule?.spirepresentation;
// 检查模块是否就绪
if (!pptModule) {
alert('Spire.Presentation is not ready yet');
return;
}
// 将字体载入 VFS
await window.spire.FetchFileToVFS('ARIAL.TTF', '/Library/Fonts/', `${process.env.PUBLIC_URL}/font/`);
// 创建演示文稿,并取得第一张幻灯片
const ppt = new pptModule.Presentation();
const slide = ppt.Slides.get_Item(0);
// 添加一个矩形形状作为文本框
const shape = slide.Shapes.AppendShape({
shapeType: pptModule.ShapeType.Rectangle,
rectangle: pptModule.RectangleF.FromLTRB(60, 180, 660, 360),
});
// 去掉填充与边框,只保留文字
shape.Fill.FillType = pptModule.FillFormatType.None;
shape.Line.FillType = pptModule.FillFormatType.None;
// 向文本框写入文字
shape.AppendTextFrame('季度工作总结');
// 设置文字字号与颜色
const textRange = shape.TextFrame.TextRange;
textRange.FontHeight = 40;
textRange.Fill.FillType = pptModule.FillFormatType.Solid;
textRange.Fill.SolidColor.Color = pptModule.Color.get_Black();
shape.TextFrame.Paragraphs.get_Item(0).Alignment = pptModule.TextAlignmentType.Center;
// 保存结果文件
const outputFileName = 'AddTextBox_result.pptx';
ppt.SaveToFile({ file: outputFileName, fileFormat: pptModule.FileFormat.Pptx2013 });
ppt.Dispose();
// 从 VFS 读取生成的文件,触发下载
const fileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
const blob = new Blob([fileArray], { type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = outputFileName;
a.click();
URL.revokeObjectURL(url);
};
return (
<div style={{ textAlign: 'center', height: '300px' }}>
<h1>添加文本框</h1>
<button onClick={addTextBox}>
添加文本框
</button>
</div>
);
}
export default App;
在空白幻灯片中添加文本框后的演示文稿:

设置文本框的填充、边框、阴影与旋转
新添加的文本框只是一块文字区域,放在设计好的页面上往往显得单薄。为文本框设置渐变填充、边框与外阴影,再做一点旋转,可以让它成为页面上独立的信息块,与背景和其他元素区分开来;文字颜色也随之调整,保证在深色填充上仍然清晰。
| 写法 | 得到的结果 |
|---|---|
shape.Fill.FillType = FillFormatType.Gradient |
把文本框的填充设为渐变填充 |
shape.Fill.Gradient.GradientShape = GradientShapeType.Linear |
把渐变类型指定为线性渐变 |
shape.Fill.Gradient.GradientStops.Append({ position, color }) |
为渐变填充添加色标,确定渐变的方向与色彩 |
shape.Line.FillType = FillFormatType.Solid |
把文本框的边框设为实线 |
shape.Line.Width = n |
设置边框的粗细 |
shape.Line.SolidFillColor.Color = Color.get_xxx() |
指定边框颜色 |
new OuterShadowEffect() |
创建外阴影效果对象 |
shadowEffect.BlurRadius、shadowEffect.Direction、shadowEffect.Distance |
设置阴影的模糊半径、方向与距离 |
shadowEffect.ColorFormat.Color = Color.get_xxx() |
指定阴影颜色 |
shape.EffectDag.OuterShadowEffect = shadowEffect |
把外阴影应用到文本框 |
shape.Rotation = n |
按角度旋转文本框 |
textRange.FontHeight = n |
设置文本框内文字的字号 |
textRange.Fill.SolidColor.Color = Color.get_xxx() |
设置文字颜色 |
具体操作步骤如下:
- 获取 Spire.Presentation 的 WASM 模块并确认已就绪,将字体文件载入 VFS,再创建
Presentation对象并取得第一张幻灯片。 - 使用
Shapes.AppendShape()添加矩形文本框,并调用shape.AppendTextFrame()写入两行文字。 - 通过
TextFrame.TextRange把文字设为白色、30 号,并用LatinFont与EastAsianFont指定字体,再让各段居中。 - 把
Line.FillType设为FillFormatType.Solid,用Line.Width与Line.SolidFillColor.Color设置白色边框。 - 将
Fill.FillType设为FillFormatType.Gradient,用GradientShape指定线性渐变,再通过GradientStops.Append()添加浅蓝与青灰色标。 - 创建
OuterShadowEffect,设置BlurRadius、Direction、Distance与ColorFormat.Color,赋给EffectDag.OuterShadowEffect。 - 用
Rotation顺时针旋转 5 度,随后保存结果文件、释放演示文稿并从 VFS 读出文件触发下载。
设置文本框样式的代码如下:
function App() {
const styleTextBox = async () => {
// 获取 Spire.Presentation WASM 模块
const pptModule = window.wasmModule?.spirepresentation;
// 检查模块是否就绪
if (!pptModule) {
alert('Spire.Presentation is not ready yet');
return;
}
// 将字体载入 VFS
await window.spire.FetchFileToVFS('ARIAL.TTF', '/Library/Fonts/', `${process.env.PUBLIC_URL}/font/`);
// 创建演示文稿,并取得第一张幻灯片
const ppt = new pptModule.Presentation();
const slide = ppt.Slides.get_Item(0);
// 添加一个矩形文本框
const shape = slide.Shapes.AppendShape({
shapeType: pptModule.ShapeType.Rectangle,
rectangle: pptModule.RectangleF.FromLTRB(80, 150, 630, 370),
});
// 写入两行文字,并设为白色、30 号
shape.AppendTextFrame('你好,世界!\n季度工作总结');
const textRange = shape.TextFrame.TextRange;
textRange.Fill.FillType = pptModule.FillFormatType.Solid;
textRange.Fill.SolidColor.Color = pptModule.Color.get_White();
textRange.FontHeight = 30;
textRange.LatinFont = new pptModule.TextFont('Arial');
for (let i = 0; i < shape.TextFrame.Paragraphs.Count; i++) {
shape.TextFrame.Paragraphs.get_Item(i).Alignment = pptModule.TextAlignmentType.Center;
}
// 设置白色实线边框
shape.Line.FillType = pptModule.FillFormatType.Solid;
shape.Line.Width = 2.5;
shape.Line.SolidFillColor.Color = pptModule.Color.get_White();
// 把文本框填充为浅蓝到青灰的线性渐变
shape.Fill.FillType = pptModule.FillFormatType.Gradient;
shape.Fill.Gradient.GradientShape = pptModule.GradientShapeType.Linear;
shape.Fill.Gradient.GradientStops.Append({ position: 0, color: pptModule.Color.get_LightSkyBlue() });
shape.Fill.Gradient.GradientStops.Append({ position: 1, color: pptModule.Color.get_CadetBlue() });
// 添加外阴影
const shadowEffect = new pptModule.OuterShadowEffect();
shadowEffect.BlurRadius = 20;
shadowEffect.Direction = 30;
shadowEffect.Distance = 8;
shadowEffect.ColorFormat.Color = pptModule.Color.get_LightGray();
shape.EffectDag.OuterShadowEffect = shadowEffect;
// 顺时针旋转 5 度
shape.Rotation = 5;
// 保存结果文件
const outputFileName = 'StyleTextBox_result.pptx';
ppt.SaveToFile({ file: outputFileName, fileFormat: pptModule.FileFormat.Pptx2013 });
ppt.Dispose();
// 从 VFS 读取生成的文件,触发下载
const fileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
const blob = new Blob([fileArray], { type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = outputFileName;
a.click();
URL.revokeObjectURL(url);
};
return (
<div style={{ textAlign: 'center', height: '300px' }}>
<h1>设置文本框样式</h1>
<button onClick={styleTextBox}>
设置文本框样式
</button>
</div>
);
}
export default App;
设置渐变填充、边框、阴影与旋转后的演示文稿:

常见问题
如何让长文字不自动换行
原因:文本框默认会在边界处自动换行,长句被拆成多行后可能打乱排版。
解决:把 WordWrap 设为 false,文字会保持在一行内并超出文本框边界:
shape.TextFrame.WordWrap = false;
如何让文本框中的文字竖排
原因:文本框默认横向排列文字,竖排标题或侧栏标签需要显式指定文字方向。
解决:把 VerticalTextType 设为 VerticalTextType.Vertical,文字整体转为竖排;若希望汉字保持直立,可使用 VerticalTextType.EastAsianVertical:
shape.TextFrame.VerticalTextType = pptModule.VerticalTextType.Vertical;
如何调整文字与文本框边框的距离
原因:文字默认贴近文本框边缘,文字较多或边框较粗时显得拥挤。
解决:通过四个内边距属性分别设置文字与边框的距离:
shape.TextFrame.MarginLeft = 60;
shape.TextFrame.MarginTop = 40;
shape.TextFrame.MarginRight = 20;
shape.TextFrame.MarginBottom = 10;
如何删除幻灯片中的文本框
原因:文本框是幻灯片形状集合中的一项,修改文字内容不会移除文本框本身。
解决:取得文本框对象后,调用形状集合的 Remove() 将其移除:
const shape = slide.Shapes.get_Item(0);
slide.Shapes.Remove(shape);
获取免费许可证
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。







