如何在 PDF 中使用 C# 配置打印设置 | IronPrint

How to Add Gradient Effect to Text

This article was translated from English: Does it need improvement?
Translated
View the article in English

A gradient effect on text involves applying a smooth transition of colors across the characters or background of text, creating a blend from one color to another or multiple colors. This effect adds depth, visual interest, and a dynamic appearance to the text, making it stand out and enhancing its aesthetic appearance. Gradient effects can be linear (colors transition in a straight line) or radial (colors transition from a central point outward).

Quickstart: Add a Gradient Effect to Text with IronWord

Here’s a simple example showing how to use IronWord to apply a built-in gradient effect to text in just a few lines—perfect if you want to get started quickly and see visual results instantly.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronWord with NuGet Package Manager

    PM > Install-Package IronWord

  2. Copy and run this code snippet.

    var doc = new IronWord.WordDocument();
    doc.AddText("Test").Style = new IronWord.Models.TextStyle(){ TextEffect = new IronWord.Models.TextEffect(){ GradientEffect = IronWord.Models.Gradient.DefaultGray } };
    doc.SaveAs("out.docx");
  3. Deploy to test on your live environment

    Start using IronWord in your project today with a free trial
    arrow pointer

Add Gradient Effect

To specify the gradient effect for the text, create the TextStyle object and populate the GradientEffect property with a Gradient object. Finally, add new text with the style by assigning the TextStyle object to the TextEffect property.

: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")
$vbLabelText   $csharpLabel
Add gradient effect

Gradient Effect Properties

The gradient effect provides a range of adjustable attributes to meet diverse design requirements. See the following list for detailed descriptions of each property:

GradientStop

  • Color: Gets or sets the scheme color of the gradient stop.
  • StopPoint: Gets or sets the position of the gradient stop.

Gradient stops are points within a gradient where a specific color is defined.

Gradient

  • StopPoints: Gets or sets the list of gradient stops defining the gradient fill.
  • LinearShadeScaled: Gets or sets a value indicating whether the linear shade is scaled.
  • LinearShadeAngle: Gets or sets the angle of the linear shade.
: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")
$vbLabelText   $csharpLabel
Customized gradient effect

常见问题解答

如何使用 C# 在 Word 文档中为文本添加渐变效果?

您可以通过使用 IronWord 的 C# 库在 Word 文档中添加渐变效果。首先,下载库,然后将文字效果应用于新的或现有的文本。通过 Gradient 类的静态命名实例使用预设的渐变效果,并通过配置 GradientEffect 属性进行自定义。

使用 IronWord 应用渐变效果的步骤是什么?

使用 IronWord 应用渐变效果,请按以下步骤操作:下载 C# 库,将渐变应用于文本,使用 Gradient 类中的预设渐变效果,配置 GradientEffect 属性,最后导出编辑后的文档。

如何在 IronWord 中创建具有渐变效果的 TextStyle 对象?

在 IronWord 中,创建一个 TextStyle 对象,并用 Gradient 对象填充 GradientEffect 属性。将此 TextStyle 分配给 TextEffect 属性以应用于文本。

IronWord 中的渐变停止的目的是什么?

IronWord 中的渐变停止是在渐变中定义特定颜色的点。可以通过 GradientStop 类进行控制,该类包含例如 Color 用于方案颜色和 StopPoint 用于渐变停止的位置。

在 IronWord 中渐变停止可以自定义吗?

是的,您可以通过向 Gradient 对象的 StopPoints 列表中添加具有特定颜色和位置的 GradientStop 对象来自定义 IronWord 中的渐变停止,从而实现个性化的渐变效果。

IronWord 中 Gradient 类的关键属性是什么?

IronWord 中的 Gradient 类包含关键属性,例如 StopPoints(定义渐变停止),LinearShadeScaled(指示线性阴影是否缩放)和 LinearShadeAngle(设置线性阴影的角度)。

使用 IronWord 添加渐变效果后如何导出 Word 文档?

在使用 IronWord 为文本添加渐变效果后,您可以通过将其另存为所需格式的新文件来导出编辑的 Word 文档,并应用渐变效果。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 25,807 | 版本: 2025.11 刚刚发布