如何为文本添加渐变效果
在文本上应用渐变效果涉及在字符或文本背景上实现颜色的平滑过渡,从一种颜色过渡到另一种颜色或多种颜色。 这种效果增加了文本的深度、视觉兴趣和动态外观,使文本更加突出并增强了其审美外观。 渐变效果可以是线性的(颜色在直线上过渡)或径向的(颜色从中心点向外过渡)。
开始使用 IronWord
立即在您的项目中开始使用IronWord,并享受免费试用。
如何为文本添加渐变效果
- 下载可为文本添加渐变效果的 C# 库
- 将文字效果应用于新创建或现有文字
- 通过使用Gradient类的静态命名实例来使用预设的渐变效果
- 配置GradientEffect属性以自定义文本轮廓
- 将编辑过的 Word 文档导出为新文件
添加渐变效果
要为文本指定渐变效果,请创建TextStyle对象,并使用Gradient对象填充GradientEffect属性。 最后,通过将TextStyle对象分配给TextEffect属性,以这种风格添加新文本。
:path=/static-assets/word/content-code-examples/how-to/text-effect-gradient-effect.cs
using IronWord;
using IronWord.Models;
// Create new Word document
WordDocument doc = new WordDocument();
// Create and configure text style
TextStyle textStyle = new TextStyle();
textStyle.TextEffect = new TextEffect()
{
GradientEffect = Gradient.DefaultGray,
};
// Add text with style
doc.AddText("Hello World").Style = textStyle;
// Export new Word document
doc.SaveAs("gradientEffect.docx");
Imports IronWord
Imports IronWord.Models
' Create new Word document
Private doc As New WordDocument()
' Create and configure text style
Private textStyle As New TextStyle()
textStyle.TextEffect = New TextEffect() With {.GradientEffect = Gradient.DefaultGray}
' Add text with style
doc.AddText("Hello World").Style = textStyle
' Export new Word document
doc.SaveAs("gradientEffect.docx")

渐变效果属性
渐变效果提供了一系列可调整的属性,以满足多样化的设计需求。 请参阅以下列表,了解每个属性的详细描述:
渐变中止
- 颜色:获取或设置渐变停止的方案颜色。
StopPoint:获取或设置渐变停止的位置。
渐变停止点是在渐变中定义特定颜色的点。
渐变
- StopPoints:获取或设置定义渐变填充的渐变停止点列表。
- LinearShadeScaled:获取或设置一个值,指示线性阴影是否已缩放。
- LinearShadeAngle:获取或设置线性渐变的角度。
:path=/static-assets/word/content-code-examples/how-to/text-effect-customized-gradient-effect.cs
using IronWord;
using IronWord.Models;
using System.Collections.Generic;
// Create new Word document
WordDocument doc = new WordDocument();
// Create gradient stops
GradientStop firstGradientStop = new GradientStop()
{
Color = IronWord.Models.Color.Aqua,
StopPoint = 1
};
GradientStop secondGradientStop = new GradientStop()
{
Color = IronWord.Models.Color.OrangeRed,
StopPoint = 10
};
// Create and configure text style
TextStyle textStyle = new TextStyle();
textStyle.TextEffect = new TextEffect()
{
GradientEffect = new Gradient()
{
StopPoints = new List<GradientStop> { firstGradientStop, secondGradientStop },
LinearShadeAngle = 45,
LinearShadeScaled = true,
}
};
// Add text with style
doc.AddText("Hello World").Style = textStyle;
// Export new Word document
doc.SaveAs("customizedGradientEffect.docx");
Imports IronWord
Imports IronWord.Models
Imports System.Collections.Generic
' Create new Word document
Private doc As New WordDocument()
' Create gradient stops
Private firstGradientStop As New GradientStop() With {
.Color = IronWord.Models.Color.Aqua,
.StopPoint = 1
}
Private secondGradientStop As New GradientStop() With {
.Color = IronWord.Models.Color.OrangeRed,
.StopPoint = 10
}
' Create and configure text style
Private textStyle As New TextStyle()
textStyle.TextEffect = New TextEffect() With {
.GradientEffect = New Gradient() With {
.StopPoints = New List(Of GradientStop) From {firstGradientStop, secondGradientStop},
.LinearShadeAngle = 45, .LinearShadeScaled = True
}
}
' Add text with style
doc.AddText("Hello World").Style = textStyle
' Export new Word document
doc.SaveAs("customizedGradientEffect.docx")
