在 PowerPoint中,形状是突出显示幻灯片中重要信息或关键消息的绝佳工具。它们提供了一种有效的方式来吸引注意力,创造视觉线索,并积极吸引观众的关注。通过有策略性地添加形状,您可以提升 PowerPoint 演示文稿的影响力,并给观众留下深刻的印象。在本文中,我们将演示如何使用 Spire.Presentation for .NET 在 C# 和 VB.NET 中向 PowerPoint 演示文稿添加各种类型的形状。
安装 Spire.Presentation for .NET
首先,您需要添加 Spire.Presentation for .NET 包中包含的 DLL 文件作为 .NET 项目中的引用。DLL 文件可以从此链接下载或通过 NuGet 安装。
PM> Install-Package Spire.Presentation
在 C# 和 VB.NET 中向 PowerPoint 演示文稿添加形状
您可以使用 Spire.Presentation for .NET 提供的 ISlide.Shapes.AppendShape(ShapeType shapeType, RectangleF rectangle) 方法,轻松地将各种类型的形状(如矩形、圆形、三角形、箭头和阴影)添加到 PowerPoint 演示文稿中。详细步骤如下:
- 初始化 Presentation 类的实例。
- 使用 Presentation.LoadFromFile(string fileName) 方法加载 PowerPoint 演示文稿。
- 使用 Presentation.Slides[int index] 属性获取特定幻灯片。
- 使用 ISlide.Shapes.Append(ShapeType shapeType, RectangleF rectangle) 方法将各种类型的形状附加到幻灯片,然后设置形状的样式。
- 使用 Presentation.SaveToFile(string fileName, FileFormat fileFormat) 方法保存 PowerPoint 演示文稿。
- C#
- VB.NET
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;
namespace AddShapes
{
internal class Program
{
static void Main(string[] args)
{
//初始化Presentation类的实例
Presentation presentation = new Presentation();
//加载PowerPoint演示文稿
presentation.LoadFromFile("C:\\Users\\Administrator\\Desktop\\演示文稿.pptx");
//获取特定幻灯片
ISlide slide = presentation.Slides[0];
//将三角形附加到幻灯片
IAutoShape shape = slide.Shapes.AppendShape(ShapeType.Triangle, new RectangleF(185, 130, 100, 100));
//用纯色填充形状
shape.Fill.FillType = FillFormatType.Solid;
shape.Fill.SolidColor.Color = Color.LightGreen;
//设置形状的轮廓颜色
shape.ShapeStyle.LineColor.Color = Color.White;
//将椭圆形状附加到幻灯片
shape = slide.Shapes.AppendShape(ShapeType.Ellipse, new RectangleF(370, 130, 150, 100));
//用图片填充形状
string picUrl = @"图片.jpg";
shape.Fill.FillType = FillFormatType.Picture;
shape.Fill.PictureFill.Picture.Url = picUrl;
shape.Fill.PictureFill.FillType = PictureFillType.Stretch;
//设置形状的轮廓颜色
shape.ShapeStyle.LineColor.Color = Color.CornflowerBlue;
//将心形附加到幻灯片
shape = slide.Shapes.AppendShape(ShapeType.Heart, new RectangleF(600, 130, 130, 100));
//向形状添加文本
shape.TextFrame.Text = "爱心";
//用纯色填充形状
shape.Fill.FillType = FillFormatType.Solid;
shape.Fill.SolidColor.Color = Color.Red;
//设置形状的轮廓颜色
shape.ShapeStyle.LineColor.Color = Color.LightGray;
//将五角星形附加到幻灯片
shape = slide.Shapes.AppendShape(ShapeType.FivePointedStar, new RectangleF(160, 270, 150, 150));
//用渐变色填充形状
shape.Fill.FillType = FillFormatType.Gradient;
shape.Fill.SolidColor.Color = Color.Black;
//设置形状的轮廓颜色
shape.ShapeStyle.LineColor.Color = Color.White;
//将矩形形状附加到幻灯片
shape = slide.Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(400, 290, 100, 120));
//用纯色填充形状
shape.Fill.FillType = FillFormatType.Solid;
shape.Fill.SolidColor.Color = Color.Pink;
//设置形状的轮廓颜色
shape.ShapeStyle.LineColor.Color = Color.LightGray;
//将箭头附加到幻灯片
shape = slide.Shapes.AppendShape(ShapeType.BentUpArrow, new RectangleF(600, 300, 150, 100));
//用渐变色填充形状
shape.Fill.FillType = FillFormatType.Gradient;
shape.Fill.Gradient.GradientStops.Append(1f, KnownColors.Olive);
shape.Fill.Gradient.GradientStops.Append(0, KnownColors.PowderBlue);
//设置形状的轮廓颜色
shape.ShapeStyle.LineColor.Color = Color.White;
//保存结果到PowerPoint演示文稿
presentation.SaveToFile("添加形状.pptx", FileFormat.Pptx2010);
presentation.Dispose();
}
}
}
Imports Spire.Presentation
Imports Spire.Presentation.Drawing
Imports System.Drawing
Namespace AddShapes
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'初始化Presentation类的实例
Dim presentation As Presentation = New Presentation()
'加载PowerPoint演示文稿
presentation.LoadFromFile("C:\Users\Administrator\Desktop\演示文稿.pptx")
'获取特定幻灯片
Dim slide As ISlide = presentation.Slides(0)
'将三角形附加到幻灯片
Dim shape As IAutoShape = slide.Shapes.AppendShape(ShapeType.Triangle, New RectangleF(185, 130, 100, 100))
'用纯色填充形状
shape.Fill.FillType = FillFormatType.Solid
shape.Fill.SolidColor.Color = Color.LightGreen
'设置形状的轮廓颜色
shape.ShapeStyle.LineColor.Color = Color.White
'将椭圆形状附加到幻灯片
shape = slide.Shapes.AppendShape(ShapeType.Ellipse, New RectangleF(370, 130, 150, 100))
'用图片填充形状
Dim picUrl = "图片.jpg"
shape.Fill.FillType = FillFormatType.Picture
shape.Fill.PictureFill.Picture.Url = picUrl
shape.Fill.PictureFill.FillType = PictureFillType.Stretch
'设置形状的轮廓颜色
shape.ShapeStyle.LineColor.Color = Color.CornflowerBlue
'将心形附加到幻灯片
shape = slide.Shapes.AppendShape(ShapeType.Heart, New RectangleF(600, 130, 130, 100))
'向形状添加文本
shape.TextFrame.Text = "爱心"
'用纯色填充形状
shape.Fill.FillType = FillFormatType.Solid
shape.Fill.SolidColor.Color = Color.Red
'设置形状的轮廓颜色
shape.ShapeStyle.LineColor.Color = Color.LightGray
'将五角星形附加到幻灯片
shape = slide.Shapes.AppendShape(ShapeType.FivePointedStar, New RectangleF(160, 270, 150, 150))
'用渐变色填充形状
shape.Fill.FillType = FillFormatType.Gradient
shape.Fill.SolidColor.Color = Color.Black
'设置形状的轮廓颜色
shape.ShapeStyle.LineColor.Color = Color.White
'将矩形形状附加到幻灯片
shape = slide.Shapes.AppendShape(ShapeType.Rectangle, New RectangleF(400, 290, 100, 120))
'用纯色填充形状
shape.Fill.FillType = FillFormatType.Solid
shape.Fill.SolidColor.Color = Color.Pink
'设置形状的轮廓颜色
shape.ShapeStyle.LineColor.Color = Color.LightGray
'将箭头附加到幻灯片
shape = slide.Shapes.AppendShape(ShapeType.BentUpArrow, New RectangleF(600, 300, 150, 100))
'用渐变色填充形状
shape.Fill.FillType = FillFormatType.Gradient
shape.Fill.Gradient.GradientStops.Append(1F, KnownColors.Olive)
shape.Fill.Gradient.GradientStops.Append(0, KnownColors.PowderBlue)
'设置形状的轮廓颜色
shape.ShapeStyle.LineColor.Color = Color.White
'保存结果到PowerPoint演示文稿
presentation.SaveToFile("添加形状.pptx", FileFormat.Pptx2010)
presentation.Dispose()
End Sub
End Class
End Namespace
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。