在 .NET 中创建和应用 WordArt 效果

概述

WordArt 效果允许您使用填充、轮廓、阴影、反射、辉光、变换和 3D 格式来为文本设置样式。本文说明如何在未安装 Microsoft Office 的情况下,使用 Aspose.Slides for .NET 在 PowerPoint 演示文稿中创建和自定义这些效果。

创建一个简单的 WordArt 模板并将其应用于文本

以下示例通过设置文本、字体、图案填充和轮廓来构建一个简单的 WordArt 样式。

每个示例都会创建一个新演示文稿并在其第一张幻灯片上添加一个矩形;不需要输入文件。第一个示例将文本设置为 “Aspose.Slides”。形状的位置和尺寸以点为单位测量:

using Aspose.Slides;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var autoShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 20, 20, 400, 200);
var textFrame = autoShape.TextFrame;

var portion = textFrame.Paragraphs[0].Portions[0];
portion.Text = "Aspose.Slides";

将字体设置为 36 磅的 Arial Black,以更明显地显示格式:

using Aspose.Slides;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var autoShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 20, 20, 400, 200);

var portion = autoShape.TextFrame.Paragraphs[0].Portions[0];
portion.Text = "Aspose.Slides";
portion.PortionFormat.LatinFont = new FontData("Arial Black");
portion.PortionFormat.FontHeight = 36;

应用具有深橙色前景和白色背景的 SmallGrid 图案,然后添加宽度为 1 点的黑色文本轮廓:

using System.Drawing;
using Aspose.Slides;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var autoShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 20, 20, 400, 200);

var portion = autoShape.TextFrame.Paragraphs[0].Portions[0];
portion.Text = "Aspose.Slides";
portion.PortionFormat.LatinFont = new FontData("Arial Black");
portion.PortionFormat.FontHeight = 36;

portion.PortionFormat.FillFormat.FillType = FillType.Pattern;
portion.PortionFormat.FillFormat.PatternFormat.ForeColor.Color = Color.DarkOrange;
portion.PortionFormat.FillFormat.PatternFormat.BackColor.Color = Color.White;
portion.PortionFormat.FillFormat.PatternFormat.PatternStyle = PatternStyle.SmallGrid;

portion.PortionFormat.LineFormat.Width = 1;
portion.PortionFormat.LineFormat.FillFormat.FillType = FillType.Solid;
portion.PortionFormat.LineFormat.FillFormat.SolidFillColor.Color = Color.Black;

生成的文本:

简单的 WordArt 模板

应用其他 WordArt 效果

以下示例演示如何对文本应用阴影、反射、辉光、变换和 3D 效果。

应用外部阴影效果

外部阴影通过在文本后方放置阴影来添加深度。您可以自定义其颜色、方向、距离、模糊半径、缩放和倾斜。

此示例调用 EnableOuterShadowEffect 并设置一个黑色阴影,模糊半径为 4 点,方向为 230 度,距离为 30 点。缩放值为 100 可保持阴影大小,而水平倾斜将其倾斜 20 度。Alpha 变换将不透明度设为 32%:

using System.Drawing;
using Aspose.Slides;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var autoShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 20, 20, 400, 200);

var portion = autoShape.TextFrame.Paragraphs[0].Portions[0];
portion.Text = "Aspose.Slides";
portion.PortionFormat.LatinFont = new FontData("Arial Black");
portion.PortionFormat.FontHeight = 36;

portion.PortionFormat.EffectFormat.EnableOuterShadowEffect();
portion.PortionFormat.EffectFormat.OuterShadowEffect.ShadowColor.Color = Color.Black;
portion.PortionFormat.EffectFormat.OuterShadowEffect.ScaleHorizontal = 100;
portion.PortionFormat.EffectFormat.OuterShadowEffect.ScaleVertical = 100;
portion.PortionFormat.EffectFormat.OuterShadowEffect.BlurRadius = 4;
portion.PortionFormat.EffectFormat.OuterShadowEffect.Direction = 230;
portion.PortionFormat.EffectFormat.OuterShadowEffect.Distance = 30;
portion.PortionFormat.EffectFormat.OuterShadowEffect.SkewHorizontal = 20;
portion.PortionFormat.EffectFormat.OuterShadowEffect.SkewVertical = 0;
portion.PortionFormat.EffectFormat.OuterShadowEffect.ShadowColor.ColorTransform.Add(ColorTransformOperation.SetAlpha, 0.32f);

生成的文本:

外部阴影效果

应用反射效果

反射会创建文本的镜像副本。调整其位置、缩放、模糊和不透明度以控制外观。

此示例调用 EnableReflectionEffect 并将反射垂直翻转,缩放为 -100%。它使用 0.5 点的模糊半径和 4.72 点的距离。沿反射的 0% 到 60% 位置,不透明度从 60% 下降到 0.9%:

using Aspose.Slides;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var autoShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 20, 20, 400, 200);

var portion = autoShape.TextFrame.Paragraphs[0].Portions[0];
portion.Text = "Aspose.Slides";
portion.PortionFormat.LatinFont = new FontData("Arial Black");
portion.PortionFormat.FontHeight = 36;

portion.PortionFormat.EffectFormat.EnableReflectionEffect();
portion.PortionFormat.EffectFormat.ReflectionEffect.BlurRadius = 0.5;
portion.PortionFormat.EffectFormat.ReflectionEffect.Distance = 4.72;
portion.PortionFormat.EffectFormat.ReflectionEffect.StartPosAlpha = 0f;
portion.PortionFormat.EffectFormat.ReflectionEffect.EndPosAlpha = 60f;
portion.PortionFormat.EffectFormat.ReflectionEffect.Direction = 90;
portion.PortionFormat.EffectFormat.ReflectionEffect.ScaleHorizontal = 100;
portion.PortionFormat.EffectFormat.ReflectionEffect.ScaleVertical = -100;
portion.PortionFormat.EffectFormat.ReflectionEffect.StartReflectionOpacity = 60f;
portion.PortionFormat.EffectFormat.ReflectionEffect.EndReflectionOpacity = 0.9f;
portion.PortionFormat.EffectFormat.ReflectionEffect.RectangleAlign = RectangleAlignment.BottomLeft;

生成的文本:

反射效果

有关渐变位置、不透明度值和淡入角度的可视化比较,请参阅 控制反射淡入。

应用辉光效果

辉光在文本周围添加柔和的彩色轮廓。调整其颜色、不透明度和半径以控制效果。

此示例调用 EnableGlowEffect 并应用红色辉光,透明度为 54%,半径为 7 点:

using Aspose.Slides;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var autoShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 20, 20, 400, 200);

var portion = autoShape.TextFrame.Paragraphs[0].Portions[0];
portion.Text = "Aspose.Slides";
portion.PortionFormat.LatinFont = new FontData("Arial Black");
portion.PortionFormat.FontHeight = 36;

portion.PortionFormat.EffectFormat.EnableGlowEffect();
portion.PortionFormat.EffectFormat.GlowEffect.Color.Color = System.Drawing.Color.Red;
portion.PortionFormat.EffectFormat.GlowEffect.Color.ColorTransform.Add(ColorTransformOperation.SetAlpha, 0.54f);
portion.PortionFormat.EffectFormat.GlowEffect.Radius = 7;

生成的文本:

辉光效果

应用 WordArt 变换

WordArt 变换可弯曲、拉伸或扭曲一段文本。

将 Transform 设置为 ArchUpPour 以向上弯曲整个文本框:

using Aspose.Slides;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var autoShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 20, 20, 400, 200);

var textFrame = autoShape.TextFrame;
textFrame.Text = "Aspose.Slides";
textFrame.TextFrameFormat.Transform = TextShapeType.ArchUpPour;

生成的文本:

WordArt 变换

对形状和文本应用 3D 效果

您可以对形状或其文本应用 3D 效果。斜角、拉伸、光照和摄像机设置决定最终外观。

以下示例使用 ThreeDFormat 为矩形添加圆形斜角、橙色拉伸和深红色轮廓。斜角尺寸、拉伸高度、轮廓宽度和深度均以点为单位。塑料材质、围绕 Z 轴旋转 40 度的均衡光照以及透视摄像机构成其外观:

using System.Drawing;
using Aspose.Slides;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var autoShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 20, 20, 400, 200);
autoShape.TextFrame.Text = "Aspose.Slides";

autoShape.ThreeDFormat.BevelBottom.BevelType = BevelPresetType.Circle;
autoShape.ThreeDFormat.BevelBottom.Height = 10.5;
autoShape.ThreeDFormat.BevelBottom.Width = 10.5;

autoShape.ThreeDFormat.BevelTop.BevelType = BevelPresetType.Circle;
autoShape.ThreeDFormat.BevelTop.Height = 12.5;
autoShape.ThreeDFormat.BevelTop.Width = 11;

autoShape.ThreeDFormat.ExtrusionColor.Color = Color.Orange;
autoShape.ThreeDFormat.ExtrusionHeight = 6;

autoShape.ThreeDFormat.ContourColor.Color = Color.DarkRed;
autoShape.ThreeDFormat.ContourWidth = 1.5;

autoShape.ThreeDFormat.Depth = 3;

autoShape.ThreeDFormat.Material = MaterialPresetType.Plastic;

autoShape.ThreeDFormat.LightRig.Direction = LightingDirection.Top;
autoShape.ThreeDFormat.LightRig.LightType = LightRigPresetType.Balanced;
autoShape.ThreeDFormat.LightRig.SetRotation(0, 0, 40);

autoShape.ThreeDFormat.Camera.CameraType = CameraPresetType.PerspectiveContrastingRightFacing;

生成的形状:

形状 3D 效果

此示例通过 TextFrameFormat.ThreeDFormat 将类似的 3D 格式应用于文本。较小的斜角塑造字母边缘,而拉伸和光照为文本提供深度:

using System.Drawing;
using Aspose.Slides;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var autoShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 20, 20, 400, 200);
var textFrame = autoShape.TextFrame;
textFrame.Text = "Aspose.Slides";

textFrame.TextFrameFormat.ThreeDFormat.BevelBottom.BevelType = BevelPresetType.Circle;
textFrame.TextFrameFormat.ThreeDFormat.BevelBottom.Height = 3.5;
textFrame.TextFrameFormat.ThreeDFormat.BevelBottom.Width = 3.5;

textFrame.TextFrameFormat.ThreeDFormat.BevelTop.BevelType = BevelPresetType.Circle;
textFrame.TextFrameFormat.ThreeDFormat.BevelTop.Height = 4;
textFrame.TextFrameFormat.ThreeDFormat.BevelTop.Width = 4;

textFrame.TextFrameFormat.ThreeDFormat.ExtrusionColor.Color = Color.Orange;
textFrame.TextFrameFormat.ThreeDFormat.ExtrusionHeight = 6;

textFrame.TextFrameFormat.ThreeDFormat.ContourColor.Color = Color.DarkRed;
textFrame.TextFrameFormat.ThreeDFormat.ContourWidth = 1.5;

textFrame.TextFrameFormat.ThreeDFormat.Depth = 3;

textFrame.TextFrameFormat.ThreeDFormat.Material = MaterialPresetType.Plastic;

textFrame.TextFrameFormat.ThreeDFormat.LightRig.Direction = LightingDirection.Top;
textFrame.TextFrameFormat.ThreeDFormat.LightRig.LightType = LightRigPresetType.Balanced;
textFrame.TextFrameFormat.ThreeDFormat.LightRig.SetRotation(0, 0, 40);

textFrame.TextFrameFormat.ThreeDFormat.Camera.CameraType = CameraPresetType.PerspectiveContrastingRightFacing;

生成的文本:

文本 3D 效果

要在保持形状的 3D 格式的同时使文本保持平面且可读,请参阅 保持文本在 3D 形状上平面,了解两种设置的比较以及完整的 C# 示例。

常见问题

我可以在不同的字体或文字(例如阿拉伯文、中文)上使用 WordArt 效果吗?

是的,Aspose.Slides for .NET 支持 Unicode,可与所有主流字体和文字一起使用。无论语言如何,都可以应用阴影、填充和轮廓等 WordArt 效果,但字体的可用性和渲染可能取决于系统字体。

我可以将 WordArt 效果应用于幻灯片母版元素吗?

是的,您可以将 WordArt 效果应用于母版幻灯片上的形状,包括标题占位符、页脚或背景文字。对母版布局的更改将反映在所有关联的幻灯片上。

WordArt 效果会影响演示文稿文件大小吗?

会有轻微影响。阴影、辉光和渐变填充等 WordArt 效果可能会因添加的格式元数据而略微增大文件大小,但差异通常可以忽略不计。

我可以在不保存演示文稿的情况下预览 WordArt 效果的结果吗?

是的,您可以使用 ISlide.GetImage 将包含 WordArt 的幻灯片渲染为图像(例如 PNG、JPEG),或使用 IShape.GetImage 渲染单个形状。这样您可以在保存或导出完整演示文稿之前,在内存中或屏幕上预览结果。