幻灯片中的卡片、按钮、标签等元素,常用圆角矩形来呈现:四角圆润的轮廓比直角更柔和,也更容易与页面主体区分。手工逐个绘制并调整圆角弧度,图形一多很难保持一致,换配色时又要重复操作。
本文使用 Spire.Presentation for JavaScript 在浏览器中向 PowerPoint 演示文稿添加圆角矩形。整个处理过程基于 WebAssembly,在用户设备本地完成,无需安装 Office,也不依赖后端服务,演示文稿不必离开浏览器。
本文介绍两个核心功能点:
有关安装和项目配置,请参考 React 项目中集成 Spire.Presentation for JavaScript。以下示例默认 Spire.Presentation for JavaScript 已安装、WebAssembly 模块已初始化。
在 PowerPoint 幻灯片中添加圆角矩形
圆角矩形是最常用的强调图形之一,适合承载标题、按钮或引述。向幻灯片添加圆角矩形,需要先给出位置与大小,再设定圆角半径,最后调整填充与轮廓,图形才能既落在预期区域,又与页面配色协调。
| 写法 | 得到的结果 |
|---|---|
slide.Shapes.AppendShape({ shapeType, rectangle }) |
在指定矩形区域内添加一个圆角矩形 |
ShapeType.RoundCornerRectangle |
四角均为圆角的矩形类型 |
shape.SetRoundRadius(radius) |
设定圆角矩形的圆角半径 |
shape.Fill.FillType 与 shape.Fill.SolidColor.Color |
把填充设为纯色并指定填充色 |
shape.Line.FillType 与 shape.Line.SolidFillColor.Color |
把轮廓设为实线并指定轮廓色 |
具体操作步骤如下:
- 获取 Spire.Presentation 的 WASM 模块并确认已就绪,再创建
Presentation对象并取得第一张幻灯片。 - 用
Shapes.AppendShape()指定ShapeType.RoundCornerRectangle与矩形区域,添加一个圆角矩形。 - 调用
shape.SetRoundRadius(),按形状宽度的比例设定圆角半径。 - 通过
Fill.FillType与Fill.SolidColor.Color设置填充,通过Line.FillType与Line.SolidFillColor.Color设置轮廓。 - 调用
Presentation.SaveToFile()保存结果文件,并用Dispose()释放演示文稿,随后从 VFS 读出文件并触发下载。
在 PowerPoint 幻灯片中添加圆角矩形的代码如下:
function App() {
const addRoundedRectangle = async () => {
// 获取 Spire.Presentation WASM 模块
const pptModule = window.wasmModule?.spirepresentation;
// 检查模块是否就绪
if (!pptModule) {
alert('Spire.Presentation is not ready yet');
return;
}
// 创建演示文稿,默认带有一张空白幻灯片
const ppt = new pptModule.Presentation();
const slide = ppt.Slides.get_Item(0);
// 在指定矩形区域内添加一个四角圆角矩形
const shape = slide.Shapes.AppendShape({
shapeType: pptModule.ShapeType.RoundCornerRectangle,
rectangle: pptModule.RectangleF.FromLTRB(210, 150, 510, 330),
});
// 按宽度的三分之一设定圆角半径
shape.SetRoundRadius(shape.Width / 3);
// 设置纯色填充与轮廓色
shape.Fill.FillType = pptModule.FillFormatType.Solid;
shape.Fill.SolidColor.Color = pptModule.Color.get_LightBlue();
shape.Line.FillType = pptModule.FillFormatType.Solid;
shape.Line.SolidFillColor.Color = pptModule.Color.get_SkyBlue();
shape.Line.Width = 3;
// 保存结果文件
const outputFileName = 'AddRoundedRectangle.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={addRoundedRectangle}>
添加圆角矩形
</button>
</div>
);
}
export default App;
在空白幻灯片中添加圆角矩形后的演示文稿:

绘制不同圆角组合的矩形
并非所有圆角矩形都四角同值。流程箭头需要单侧圆角、标签页常用对角圆角、示意图里的连接块则常用一侧两角圆角。Spire.Presentation for JavaScript 预置了多种圆角组合的形状类型,逐一添加后统一设定半径,就能让一组图形保持协调的弧度。
| 写法 | 得到的结果 |
|---|---|
ShapeType.RoundCornerRectangle |
四角均为圆角 |
ShapeType.OneRoundCornerRectangle |
仅一个角为圆角 |
ShapeType.OneSnipOneRoundCornerRectangle |
一个圆角与一个切角 |
ShapeType.TwoDiagonalRoundCornerRectangle |
对角线上的两个圆角 |
ShapeType.TwoSamesideRoundCornerRectangle |
同一侧的两个圆角 |
shape.SetRoundRadius(radius) |
统一设定各形状的圆角半径 |
具体操作步骤如下:
- 获取 Spire.Presentation 的 WASM 模块并确认已就绪,再创建
Presentation对象并取得第一张幻灯片。 - 用
Shapes.AppendShape()逐个添加五种圆角组合的矩形。 - 对每个形状分别调用
shape.SetRoundRadius(),设置圆角半径。 - 为每个形状分别设置不同的填充色与轮廓色,便于区分不同的圆角样式。
- 调用
Presentation.SaveToFile()保存结果文件,并用Dispose()释放演示文稿,随后从 VFS 读出文件并触发下载。
绘制不同圆角组合矩形的代码如下:
function App() {
const addRoundedVariants = async () => {
// 获取 Spire.Presentation WASM 模块
const pptModule = window.wasmModule?.spirepresentation;
// 检查模块是否就绪
if (!pptModule) {
alert('Spire.Presentation is not ready yet');
return;
}
// 创建演示文稿,默认带有一张空白幻灯片
const ppt = new pptModule.Presentation();
const slide = ppt.Slides.get_Item(0);
// 五种圆角组合的矩形,逐个添加并各自设定配色
const shape1 = slide.Shapes.AppendShape({
shapeType: pptModule.ShapeType.RoundCornerRectangle,
rectangle: pptModule.RectangleF.FromLTRB(60, 90, 220, 220),
});
shape1.SetRoundRadius(shape1.Width / 3);
shape1.Fill.FillType = pptModule.FillFormatType.Solid;
shape1.Fill.SolidColor.Color = pptModule.Color.get_LightBlue();
shape1.Line.FillType = pptModule.FillFormatType.Solid;
shape1.Line.SolidFillColor.Color = pptModule.Color.get_SkyBlue();
shape1.Line.Width = 2;
// 单角圆角矩形:浅绿填充、海绿轮廓
const shape2 = slide.Shapes.AppendShape({
shapeType: pptModule.ShapeType.OneRoundCornerRectangle,
rectangle: pptModule.RectangleF.FromLTRB(270, 90, 430, 220),
});
shape2.SetRoundRadius(shape2.Width / 3);
shape2.Fill.FillType = pptModule.FillFormatType.Solid;
shape2.Fill.SolidColor.Color = pptModule.Color.get_LightGreen();
shape2.Line.FillType = pptModule.FillFormatType.Solid;
shape2.Line.SolidFillColor.Color = pptModule.Color.get_SeaGreen();
shape2.Line.Width = 2;
// 单角加切角矩形:浅黄填充、橙色轮廓
const shape3 = slide.Shapes.AppendShape({
shapeType: pptModule.ShapeType.OneSnipOneRoundCornerRectangle,
rectangle: pptModule.RectangleF.FromLTRB(480, 90, 640, 220),
});
shape3.SetRoundRadius(shape3.Width / 3);
shape3.Fill.FillType = pptModule.FillFormatType.Solid;
shape3.Fill.SolidColor.Color = pptModule.Color.get_LightYellow();
shape3.Line.FillType = pptModule.FillFormatType.Solid;
shape3.Line.SolidFillColor.Color = pptModule.Color.get_Orange();
shape3.Line.Width = 2;
// 对角两角矩形:浅粉填充、紫红轮廓
const shape4 = slide.Shapes.AppendShape({
shapeType: pptModule.ShapeType.TwoDiagonalRoundCornerRectangle,
rectangle: pptModule.RectangleF.FromLTRB(60, 290, 220, 420),
});
shape4.SetRoundRadius(shape4.Width / 3);
shape4.Fill.FillType = pptModule.FillFormatType.Solid;
shape4.Fill.SolidColor.Color = pptModule.Color.get_LightPink();
shape4.Line.FillType = pptModule.FillFormatType.Solid;
shape4.Line.SolidFillColor.Color = pptModule.Color.get_MediumVioletRed();
shape4.Line.Width = 2;
// 同侧两角矩形:浅青填充、军校蓝轮廓
const shape5 = slide.Shapes.AppendShape({
shapeType: pptModule.ShapeType.TwoSamesideRoundCornerRectangle,
rectangle: pptModule.RectangleF.FromLTRB(270, 290, 430, 420),
});
shape5.SetRoundRadius(shape5.Width / 3);
shape5.Fill.FillType = pptModule.FillFormatType.Solid;
shape5.Fill.SolidColor.Color = pptModule.Color.get_LightCyan();
shape5.Line.FillType = pptModule.FillFormatType.Solid;
shape5.Line.SolidFillColor.Color = pptModule.Color.get_CadetBlue();
shape5.Line.Width = 2;
// 保存结果文件
const outputFileName = 'RoundedRectangleVariants.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={addRoundedVariants}>
绘制圆角组合
</button>
</div>
);
}
export default App;
绘制五种圆角组合矩形后的演示文稿:

常见问题
如何给圆角矩形添加文字并让文字居中
原因:圆角矩形常用来承载标题或按钮文案,添加形状后需要写入文字并设置段落对齐,文字才能在图形内水平居中。
解决:用 AppendTextFrame() 写入文字,再把第一段的对齐方式设为 TextAlignmentType.Center:
shape.AppendTextFrame('圆角矩形文字');
shape.TextFrame.Paragraphs.get_Item(0).Alignment = pptModule.TextAlignmentType.Center;
如何修改已打开演示文稿中的圆角矩形
原因:处理既有文件时,圆角矩形往往和别的形状混在同一张幻灯片里,需要先定位再修改。
解决:用 LoadFromFile() 打开演示文稿,遍历 slide.Shapes,通过 shape.ShapeType 判断是否为 RoundCornerRectangle,再修改其填充或圆角半径:
const ppt = new pptModule.Presentation();
ppt.LoadFromFile('Sample.pptx');
const slide = ppt.Slides.get_Item(0);
for (let i = 0; i < slide.Shapes.Count; i++) {
const shape = slide.Shapes.get_Item(i);
if (shape.ShapeType === pptModule.ShapeType.RoundCornerRectangle) {
shape.Fill.FillType = pptModule.FillFormatType.Solid;
shape.Fill.SolidColor.Color = pptModule.Color.get_LightGreen();
shape.SetRoundRadius(shape.Width / 4);
}
}
ppt.SaveToFile({ file: 'Modified.pptx', fileFormat: pptModule.FileFormat.Pptx2013 });
ppt.Dispose();
如何删除演示文稿中的圆角矩形
原因:若边遍历边按下标删除,移除元素后下标会前移,容易漏删或越界。
解决:先取到形状对象,再用 slide.Shapes.Remove(shape) 删除,并把循环下标回退一位:
for (let i = 0; i < slide.Shapes.Count; i++) {
const shape = slide.Shapes.get_Item(i);
if (shape.ShapeType === pptModule.ShapeType.RoundCornerRectangle) {
slide.Shapes.Remove(shape);
i--;
}
}
获取免费许可证
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。







