我们进行PPT幻灯片编辑时,可以通过设置文字或图片的阴影效果,来使幻灯片内容的呈现效果更为立体化,给人视觉上的立体感。Spire.Presentation for .NET支持图片、文字阴影效果。本文将介绍通过C#来设置PPT中文字或图片阴影效果的方法。
示例要点:
?
安装后,注意在编辑代码时,添加引用Spire.Presentation.dll,dll文件可在安装路径下的Bin文件夹下获取。
?
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;
namespace ShadowforText_PPT
{
class Program
{
static void Main(string[] args)
{
//实例化Presentation类的对象,获取第一张幻灯片
Presentation presentation = new Presentation();
ISlide slide = presentation.Slides[0];
//在幻灯片指定位置绘制指定大小的矩形形状,并设置填形状填充
IAutoShape shape = slide.Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(10, 10, 700,520));
shape.ShapeStyle.LineColor.Color = Color.White;
string picPath = "gf.png";
shape.Fill.FillType = FillFormatType.Picture;
shape.Fill.PictureFill.Picture.Url = picPath;
shape.Fill.PictureFill.FillType = PictureFillType.Stretch;
//shape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Picture;
//绘制文本内容到形状,并设置文本格式
shape.AppendTextFrame(" 江南逢李龟年\n 杜甫 \n 岐王宅里寻常见,崔九堂前几度闻。\n 正是江南好风景,落花时节又逢君。");
shape.TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont("隶书");
shape.TextFrame.Paragraphs[0].TextRanges[0].Fill.FillType = FillFormatType.Solid;
shape.TextFrame.Paragraphs[0].TextRanges[0].Fill.SolidColor.Color = Color.Black;
shape.TextFrame.Paragraphs[0].TextRanges[0].FontHeight = 38;
//实例化OuterShadowEffect类的对象,设置文字阴影效果
Spire.Presentation.Drawing.OuterShadowEffect Shadow = new Spire.Presentation.Drawing.OuterShadowEffect();
//Spire.Presentation.Drawing.InnerShadowEffect Shadow = new Spire.Presentation.Drawing.InnerShadowEffect();
Shadow.BlurRadius = 0;
Shadow.Direction = 50;
Shadow.Distance = 10;
Shadow.ColorFormat.Color = Color.Gray;
shape.TextFrame.TextRange.EffectDag.OuterShadowEffect = Shadow;
//shape.TextFrame.TextRange.EffectDag.InnerShadowEffect = Shadow;
//保存文档并打开
presentation.SaveToFile("result.pptx", FileFormat.Pptx2010);
System.Diagnostics.Process.Start("result.pptx");
}
}
}
?
PPT文字阴影添加效果:

?
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;
namespace ShadowforImg_PPT
{
class Program
{
static void Main(string[] args)
{
//创建Presentation实例,并获取第一张幻灯片
Presentation ppt = new Presentation();
ISlide slide = ppt.Slides[0];
//获取图片地址及长宽
string imagePath = "car.png";
Image image = Image.FromFile(imagePath);
float width = (float)image.Width / 2;
float height = (float)image.Height / 2;
//将图片添加至指定位置
RectangleF rect = new RectangleF(300, 80, width, height);
IAutoShape shape = slide.Shapes.AppendShape(ShapeType.Rectangle, rect);
shape.Fill.FillType = FillFormatType.Picture;
shape.Fill.PictureFill.Picture.Url = imagePath;
shape.Fill.PictureFill.FillType = PictureFillType.Stretch;
shape.Line.FillType = FillFormatType.None;
//在不同的位置再次添加图片
rect = new RectangleF(50, 250, width, height);
shape = slide.Shapes.AppendShape(ShapeType.Rectangle, rect);
shape.Fill.FillType = FillFormatType.Picture;
shape.Fill.PictureFill.Picture.Url = imagePath;
shape.Fill.PictureFill.FillType = PictureFillType.Stretch;
shape.Line.FillType = FillFormatType.None;
//实例化InnerShadowEffect类的对象,创建阴影效果
InnerShadowEffect innerShadow = new InnerShadowEffect();
innerShadow.BlurRadius = 25;
innerShadow.Direction = 0;
innerShadow.Distance = 5;
innerShadow.ColorFormat.Color = Color.Black;
//在第二张图片上应用阴影效果
shape.EffectDag.InnerShadowEffect = innerShadow;
//保存文档
ppt.SaveToFile("ImageShadow.pptx", FileFormat.Pptx2010);
System.Diagnostics.Process.Start("ImageShadow.pptx");
}
}
}
?
图片阴影效果:

?
(本文完)
?