如何在 C# 中为文本添加发光效果
发光效果会在字符周围形成光晕,使文字看起来沿轮廓发出柔和的光。在 C# 应用中,尤其是在生成 Word 文档时,可用此效果突出页眉、标题和重要内容。发光效果常用于演示文稿、营销材料以及需要突出文字的专业文档。
快速入门:快速为文本元素添加发光效果创建 Glow 对象,设置其 GlowRadius 和 GlowColor,将其放入 TextEffect,再将该效果赋给文本样式。一行代码即可为 Word 文档中的文本设置发光效果。
-
1使用 IronWord 安装 NuGet Package Manager
-
2复制并运行这段代码。
using IronWord; using IronWord.Models; WordDocument doc = new WordDocument(); TextStyle textStyle = new TextStyle(); textStyle.TextEffect = new TextEffect() { GlowEffect = new Glow() { GlowColor = IronWord.Models.Color.Aqua, GlowRadius = 10 } }; Paragraph paragraph = new Paragraph(); Run textRun = new Run(new TextContent("Glowing Text")); textRun.Style = textStyle; paragraph.AddChild(textRun); doc.AddParagraph(paragraph); doc.SaveAs("glow.docx");Imports IronWord Imports IronWord.Models Dim doc As New WordDocument() Dim textStyle As New TextStyle() textStyle.TextEffect = New TextEffect() With {.GlowEffect = New Glow() With {.GlowColor = IronWord.Models.Color.Aqua, .GlowRadius = 10}} Dim paragraph As New Paragraph() Dim textRun As New Run(New TextContent("Glowing Text")) textRun.Style = textStyle paragraph.AddChild(textRun) doc.AddParagraph(paragraph) doc.SaveAs("glow.docx")Imports IronWord Imports IronWord.Models Dim doc As New WordDocument() Dim textStyle As New TextStyle() textStyle.TextEffect = New TextEffect() With {.GlowEffect = New Glow() With {.GlowColor = IronWord.Models.Color.Aqua, .GlowRadius = 10}} Dim paragraph As New Paragraph() Dim textRun As New Run(New TextContent("Glowing Text")) textRun.Style = textStyle paragraph.AddChild(textRun) doc.AddParagraph(paragraph) doc.SaveAs("glow.docx") -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronWord
为文本添加发光效果的方法(5 步)
- 安装IronWord:
Install-Package IronWord - 创建
TextStyle并使用GlowColor和GlowRadius配置GlowEffect - 创建一个包含
TextContent的Run,并将TextStyle分配给该Run - 使用
AddChild将Run添加到Paragraph - 保存文档
如何在 C# 中为文本添加发光效果?
要应用发光效果,先创建 TextStyle,并为其 TextEffect 属性设置带有 GlowEffect 的文本效果。接着创建 Paragraph,再创建包含 TextContent 的 Run。将 TextStyle 赋给 Run(而非 TextContent),然后使用 AddChild 将 Run 添加到 Paragraph。文档层次结构为 Document → Paragraph → Run → TextContent。这种方法可以控制发光的外观和强度。
为什么要先创建发光对象?
先创建 Glow 对象,可以在应用效果前配置所有发光属性。这符合关注点分离原则,也便于维护代码。独立定义的发光属性可在多个文本元素中复用,并按应用需求动态调整。Iron Software 的其他产品也采用类似方式处理这类效果,使文档处理流程保持一致。
using IronWord;
using IronWord.Models;
using System.Drawing;
public class TextGlowEffectExample
{
public void ApplyGlowEffect()
{
// Create a new Word document
WordDocument doc = new WordDocument();
// Add a paragraph with text
Paragraph paragraph = new Paragraph();
Run textRun = new Run(new TextContent("This text has a bright glow!"));
paragraph.AddChild(textRun);
// Initialize a new Glow object
Glow glow = new Glow();
// Set the properties for the glow effect
glow.GlowRadius = 15; // Radius of the glow effect in points
glow.GlowColor = Color.FromArgb(200, 0, 255, 255); // Semi-transparent cyan
// Create a TextEffect object and assign the glow effect to it
TextEffect textEffect = new TextEffect();
textEffect.GlowEffect = glow;
// Apply the TextEffect to the text
textRun.Style = new TextStyle();
textRun.Style.TextEffect = textEffect;
// Add the paragraph to the document
doc.AddParagraph(paragraph);
// Save the document
doc.SaveAs("glowing-text-example.docx");
}
}using IronWord;
using IronWord.Models;
using System.Drawing;
public class TextGlowEffectExample
{
public void ApplyGlowEffect()
{
// Create a new Word document
WordDocument doc = new WordDocument();
// Add a paragraph with text
Paragraph paragraph = new Paragraph();
Run textRun = new Run(new TextContent("This text has a bright glow!"));
paragraph.AddChild(textRun);
// Initialize a new Glow object
Glow glow = new Glow();
// Set the properties for the glow effect
glow.GlowRadius = 15; // Radius of the glow effect in points
glow.GlowColor = Color.FromArgb(200, 0, 255, 255); // Semi-transparent cyan
// Create a TextEffect object and assign the glow effect to it
TextEffect textEffect = new TextEffect();
textEffect.GlowEffect = glow;
// Apply the TextEffect to the text
textRun.Style = new TextStyle();
textRun.Style.TextEffect = textEffect;
// Add the paragraph to the document
doc.AddParagraph(paragraph);
// Save the document
doc.SaveAs("glowing-text-example.docx");
}
}Imports IronWord
Imports IronWord.Models
Imports System.Drawing
Public Class TextGlowEffectExample
Public Sub ApplyGlowEffect()
' Create a new Word document
Dim doc As New WordDocument()
' Add a paragraph with text
Dim paragraph As New Paragraph()
Dim textRun As New Run(New TextContent("This text has a bright glow!"))
paragraph.AddChild(textRun)
' Initialize a new Glow object
Dim glow As New Glow()
' Set the properties for the glow effect
glow.GlowRadius = 15 ' Radius of the glow effect in points
glow.GlowColor = Color.FromArgb(200, 0, 255, 255) ' Semi-transparent cyan
' Create a TextEffect object and assign the glow effect to it
Dim textEffect As New TextEffect()
textEffect.GlowEffect = glow
' Apply the TextEffect to the text
textRun.Style = New TextStyle()
textRun.Style.TextEffect = textEffect
' Add the paragraph to the document
doc.AddParagraph(paragraph)
' Save the document
doc.SaveAs("glowing-text-example.docx")
End Sub
End ClassImports IronWord
Imports IronWord.Models
Imports System.Drawing
Public Class TextGlowEffectExample
Public Sub ApplyGlowEffect()
' Create a new Word document
Dim doc As New WordDocument()
' Add a paragraph with text
Dim paragraph As New Paragraph()
Dim textRun As New Run(New TextContent("This text has a bright glow!"))
paragraph.AddChild(textRun)
' Initialize a new Glow object
Dim glow As New Glow()
' Set the properties for the glow effect
glow.GlowRadius = 15 ' Radius of the glow effect in points
glow.GlowColor = Color.FromArgb(200, 0, 255, 255) ' Semi-transparent cyan
' Create a TextEffect object and assign the glow effect to it
Dim textEffect As New TextEffect()
textEffect.GlowEffect = glow
' Apply the TextEffect to the text
textRun.Style = New TextStyle()
textRun.Style.TextEffect = textEffect
' Add the paragraph to the document
doc.AddParagraph(paragraph)
' Save the document
doc.SaveAs("glowing-text-example.docx")
End Sub
End Class
发光效果有哪些属性?
了解发光效果的各项属性,有助于美化文档,同时避免弱化正文。配置适用的许可,可在生产环境中不受限制地使用这些功能。
哪些属性控制发光效果的外观?
GlowRadius:设置发光半径,单位为磅(1 磅等于 1/72 英寸)。常用值为 5 至 30 磅。半径越大,光晕扩散得越远。5 至 10 磅可轻微突出文本,20 至 30 磅则会产生明显的光晕。GlowColor:设置发光颜色。接受IronWord.Models.Color值,也支持通过隐式转换传入System.Drawing.Color值,包括用来控制透明度的 ARGB 值。青色、黄色和洋红色等亮色能产生鲜明的效果,深色则适合轻微突出文本。
如何设置带 Alpha 透明度的颜色值?
通过 Alpha 透明度,可以让发光效果自然融入背景。Alpha 值的范围为 0(完全透明)至 255(完全不透明)。下面的示例展示了不同 Alpha 值的效果:
using IronWord;
using IronWord.Models;
using System.Drawing;
public class AlphaTransparencyExample
{
public void DemonstrateAlphaValues()
{
WordDocument doc = new WordDocument();
// Create multiple text samples with different alpha values
int[] alphaValues = { 50, 100, 150, 200, 255 };
foreach (int alpha in alphaValues)
{
Paragraph para = new Paragraph();
Run text = new Run(new TextContent($"Alpha: {alpha} - Glow Effect Sample"));
// Create glow with specific alpha transparency
Glow glow = new Glow
{
GlowRadius = 12,
GlowColor = Color.FromArgb(alpha, 255, 215, 0) // Gold with varying transparency
};
// Apply the glow effect
TextEffect effect = new TextEffect { GlowEffect = glow };
text.Style = new TextStyle
{
TextEffect = effect,
FontSize = 24,
TextFont = new Font { FontFamily = "Arial" }
};
para.AddChild(text);
doc.AddParagraph(para);
}
doc.SaveAs("alpha-transparency-demo.docx");
}
}using IronWord;
using IronWord.Models;
using System.Drawing;
public class AlphaTransparencyExample
{
public void DemonstrateAlphaValues()
{
WordDocument doc = new WordDocument();
// Create multiple text samples with different alpha values
int[] alphaValues = { 50, 100, 150, 200, 255 };
foreach (int alpha in alphaValues)
{
Paragraph para = new Paragraph();
Run text = new Run(new TextContent($"Alpha: {alpha} - Glow Effect Sample"));
// Create glow with specific alpha transparency
Glow glow = new Glow
{
GlowRadius = 12,
GlowColor = Color.FromArgb(alpha, 255, 215, 0) // Gold with varying transparency
};
// Apply the glow effect
TextEffect effect = new TextEffect { GlowEffect = glow };
text.Style = new TextStyle
{
TextEffect = effect,
FontSize = 24,
TextFont = new Font { FontFamily = "Arial" }
};
para.AddChild(text);
doc.AddParagraph(para);
}
doc.SaveAs("alpha-transparency-demo.docx");
}
}Imports IronWord
Imports IronWord.Models
Imports System.Drawing
Public Class AlphaTransparencyExample
Public Sub DemonstrateAlphaValues()
Dim doc As New WordDocument()
' Create multiple text samples with different alpha values
Dim alphaValues As Integer() = {50, 100, 150, 200, 255}
For Each alpha As Integer In alphaValues
Dim para As New Paragraph()
Dim text As New Run(New TextContent($"Alpha: {alpha} - Glow Effect Sample"))
' Create glow with specific alpha transparency
Dim glow As New Glow With {
.GlowRadius = 12,
.GlowColor = Color.FromArgb(alpha, 255, 215, 0) ' Gold with varying transparency
}
' Apply the glow effect
Dim effect As New TextEffect With {
.GlowEffect = glow
}
text.Style = New TextStyle With {
.TextEffect = effect,
.FontSize = 24,
.TextFont = New Font With {.FontFamily = "Arial"}
}
para.AddChild(text)
doc.AddParagraph(para)
Next
doc.SaveAs("alpha-transparency-demo.docx")
End Sub
End ClassImports IronWord
Imports IronWord.Models
Imports System.Drawing
Public Class AlphaTransparencyExample
Public Sub DemonstrateAlphaValues()
Dim doc As New WordDocument()
' Create multiple text samples with different alpha values
Dim alphaValues As Integer() = {50, 100, 150, 200, 255}
For Each alpha As Integer In alphaValues
Dim para As New Paragraph()
Dim text As New Run(New TextContent($"Alpha: {alpha} - Glow Effect Sample"))
' Create glow with specific alpha transparency
Dim glow As New Glow With {
.GlowRadius = 12,
.GlowColor = Color.FromArgb(alpha, 255, 215, 0) ' Gold with varying transparency
}
' Apply the glow effect
Dim effect As New TextEffect With {
.GlowEffect = glow
}
text.Style = New TextStyle With {
.TextEffect = effect,
.FontSize = 24,
.TextFont = New Font With {.FontFamily = "Arial"}
}
para.AddChild(text)
doc.AddParagraph(para)
Next
doc.SaveAs("alpha-transparency-demo.docx")
End Sub
End ClassAlpha 值指南:
- 50-100:效果很淡,近乎不可见,适合呈现类似水印的效果
- 100-150:发光柔和,适合需要保持专业外观的商务文档
- 150-200:发光强度适中,适合页眉和标题
- 200-255:发光强烈,适合需要突出文字的宣传材料
发光效果有哪些示例?
发光效果支持 ARGB 颜色值,其中 Alpha 值控制不透明度。以下示例展示了发光效果在不同文档场景中的用法。部署到生产环境前,请配置相应的许可证密钥。
何时应该使用不同的半径值?
不同的半径适用于不同场景。较小的半径(5 至 10 磅)会产生集中的光晕,适合略微突出术语或链接。中等半径(15 至 20 磅)适用于章节标题和文档标题,有助于区分层级。较大的半径(25 磅及以上)适用于需要醒目效果的封面或促销提示。
以下示例演示如何应用不同的发光半径:
public class RadiusExamples
{
public void CreateRadiusComparison()
{
WordDocument doc = new WordDocument();
// Example 1: Subtle emphasis for inline text
Paragraph p1 = new Paragraph();
Run subtleText = new Run(new TextContent("Important: This deadline cannot be extended."));
subtleText.Style = new TextStyle
{
TextEffect = new TextEffect
{
GlowEffect = new Glow
{
GlowRadius = 6,
GlowColor = Color.FromArgb(180, 255, 0, 0) // Soft red glow
}
}
};
p1.AddChild(subtleText);
// Example 2: Section header with medium glow
Paragraph p2 = new Paragraph();
Run headerText = new Run(new TextContent("Chapter 1: Getting Started"));
headerText.Style = new TextStyle
{
FontSize = 28,
TextFont = new Font { FontFamily = "Calibri" },
TextEffect = new TextEffect
{
GlowEffect = new Glow
{
GlowRadius = 18,
GlowColor = Color.FromArgb(150, 0, 120, 215) // Corporate blue
}
}
};
p2.AddChild(headerText);
// Example 3: Promotional text with large glow
Paragraph p3 = new Paragraph();
Run promoText = new Run(new TextContent("SPECIAL OFFER - LIMITED TIME!"));
promoText.Style = new TextStyle
{
FontSize = 36,
IsBold = true,
TextEffect = new TextEffect
{
GlowEffect = new Glow
{
GlowRadius = 30,
GlowColor = Color.FromArgb(220, 255, 255, 0) // Bright yellow
}
}
};
p3.AddChild(promoText);
doc.AddParagraph(p1);
doc.AddParagraph(p2);
doc.AddParagraph(p3);
doc.SaveAs("radius-examples.docx");
}
}public class RadiusExamples
{
public void CreateRadiusComparison()
{
WordDocument doc = new WordDocument();
// Example 1: Subtle emphasis for inline text
Paragraph p1 = new Paragraph();
Run subtleText = new Run(new TextContent("Important: This deadline cannot be extended."));
subtleText.Style = new TextStyle
{
TextEffect = new TextEffect
{
GlowEffect = new Glow
{
GlowRadius = 6,
GlowColor = Color.FromArgb(180, 255, 0, 0) // Soft red glow
}
}
};
p1.AddChild(subtleText);
// Example 2: Section header with medium glow
Paragraph p2 = new Paragraph();
Run headerText = new Run(new TextContent("Chapter 1: Getting Started"));
headerText.Style = new TextStyle
{
FontSize = 28,
TextFont = new Font { FontFamily = "Calibri" },
TextEffect = new TextEffect
{
GlowEffect = new Glow
{
GlowRadius = 18,
GlowColor = Color.FromArgb(150, 0, 120, 215) // Corporate blue
}
}
};
p2.AddChild(headerText);
// Example 3: Promotional text with large glow
Paragraph p3 = new Paragraph();
Run promoText = new Run(new TextContent("SPECIAL OFFER - LIMITED TIME!"));
promoText.Style = new TextStyle
{
FontSize = 36,
IsBold = true,
TextEffect = new TextEffect
{
GlowEffect = new Glow
{
GlowRadius = 30,
GlowColor = Color.FromArgb(220, 255, 255, 0) // Bright yellow
}
}
};
p3.AddChild(promoText);
doc.AddParagraph(p1);
doc.AddParagraph(p2);
doc.AddParagraph(p3);
doc.SaveAs("radius-examples.docx");
}
}Public Class RadiusExamples
Public Sub CreateRadiusComparison()
Dim doc As New WordDocument()
' Example 1: Subtle emphasis for inline text
Dim p1 As New Paragraph()
Dim subtleText As New Run(New TextContent("Important: This deadline cannot be extended."))
subtleText.Style = New TextStyle With {
.TextEffect = New TextEffect With {
.GlowEffect = New Glow With {
.GlowRadius = 6,
.GlowColor = Color.FromArgb(180, 255, 0, 0) ' Soft red glow
}
}
}
p1.AddChild(subtleText)
' Example 2: Section header with medium glow
Dim p2 As New Paragraph()
Dim headerText As New Run(New TextContent("Chapter 1: Getting Started"))
headerText.Style = New TextStyle With {
.FontSize = 28,
.TextFont = New Font With {.FontFamily = "Calibri"},
.TextEffect = New TextEffect With {
.GlowEffect = New Glow With {
.GlowRadius = 18,
.GlowColor = Color.FromArgb(150, 0, 120, 215) ' Corporate blue
}
}
}
p2.AddChild(headerText)
' Example 3: Promotional text with large glow
Dim p3 As New Paragraph()
Dim promoText As New Run(New TextContent("SPECIAL OFFER - LIMITED TIME!"))
promoText.Style = New TextStyle With {
.FontSize = 36,
.IsBold = True,
.TextEffect = New TextEffect With {
.GlowEffect = New Glow With {
.GlowRadius = 30,
.GlowColor = Color.FromArgb(220, 255, 255, 0) ' Bright yellow
}
}
}
p3.AddChild(promoText)
doc.AddParagraph(p1)
doc.AddParagraph(p2)
doc.AddParagraph(p3)
doc.SaveAs("radius-examples.docx")
End Sub
End ClassPublic Class RadiusExamples
Public Sub CreateRadiusComparison()
Dim doc As New WordDocument()
' Example 1: Subtle emphasis for inline text
Dim p1 As New Paragraph()
Dim subtleText As New Run(New TextContent("Important: This deadline cannot be extended."))
subtleText.Style = New TextStyle With {
.TextEffect = New TextEffect With {
.GlowEffect = New Glow With {
.GlowRadius = 6,
.GlowColor = Color.FromArgb(180, 255, 0, 0) ' Soft red glow
}
}
}
p1.AddChild(subtleText)
' Example 2: Section header with medium glow
Dim p2 As New Paragraph()
Dim headerText As New Run(New TextContent("Chapter 1: Getting Started"))
headerText.Style = New TextStyle With {
.FontSize = 28,
.TextFont = New Font With {.FontFamily = "Calibri"},
.TextEffect = New TextEffect With {
.GlowEffect = New Glow With {
.GlowRadius = 18,
.GlowColor = Color.FromArgb(150, 0, 120, 215) ' Corporate blue
}
}
}
p2.AddChild(headerText)
' Example 3: Promotional text with large glow
Dim p3 As New Paragraph()
Dim promoText As New Run(New TextContent("SPECIAL OFFER - LIMITED TIME!"))
promoText.Style = New TextStyle With {
.FontSize = 36,
.IsBold = True,
.TextEffect = New TextEffect With {
.GlowEffect = New Glow With {
.GlowRadius = 30,
.GlowColor = Color.FromArgb(220, 255, 255, 0) ' Bright yellow
}
}
}
p3.AddChild(promoText)
doc.AddParagraph(p1)
doc.AddParagraph(p2)
doc.AddParagraph(p3)
doc.SaveAs("radius-examples.docx")
End Sub
End Class发光效果有哪些常见的配色方案?
配色取决于文档用途和品牌风格。专业文档适合使用柔和的蓝色、灰色或 Alpha 值较低的品牌色。营销材料可使用鲜艳的互补色或高对比度配色。搭配使用多个 Iron Software 产品时,可查看产品更新以确认兼容性。
常用颜色组合:
- 商务蓝:藏蓝色文本配浅蓝色光晕(RGB: 100, 150, 255)
- 暖色强调:深棕色文本配金色光晕(RGB: 255, 200, 50)
- 高对比度:黑色文本配白色或银色光晕(RGB: 220, 220, 220)
- 品牌配色:使用品牌色设置文本,再搭配互补色光晕
- 季节主题: 节假日使用绿色/红色,万圣节使用橙色/黑色

发光效果应提升可读性,避免影响阅读。请测试不同背景下的配色,并遵循无障碍指南。需要延长支持期限的企业应用可通过许可扩展继续获得更新和功能。扩展应用规模时,升级选项可满足团队增长和需求扩大的需要。
常见问题
如何在C# Word文档中添加文本发光效果?
使用IronWord创建一个Glow对象,设置其GlowRadius和GlowColor,并应用到TextStyle的TextEffect属性中,然后在文档中将其分配给文本的样式。
IronWord中发光效果的属性是什么?
发光效果的属性包括GlowRadius,定义发光的大小(以点为单位),以及GlowColor,使用IronWord.Models.Color或System.Drawing.Color值指定发光颜色。
为什么先创建Glow对象很重要?
先创建Glow对象允许您在应用之前配置其属性,遵循关注点分离原则,并提高代码可维护性。它还使重用和动态修改成为可能。
文档中的常见发光颜色组合是什么?
常见颜色组合包括专业蓝(深蓝色文本,浅蓝色发光),温暖强调(深棕色文本,金色发光),高对比度(黑色文本,白色/银色发光),适合各种文档背景和品牌需求。
何时应使用不同的发光半径值?
小半径(5-10点)增强术语或链接的重点,中等半径(15-20点)突出章节标题,大半径(25点以上)适用于封面或需要高影响力的促销材料。
如何对发光效果应用Alpha透明度?
通过在GlowColor中设置Alpha值使用Alpha透明度,范围从0(透明)到255(不透明)。这使发光效果能自然地与不同背景融合。
使用IronWord实现发光效果的一般步骤是什么?
首先,安装IronWord。然后,创建一个TextStyle并配置带有所需属性的GlowEffect。将此分配给段落中的Run,然后保存为Word文档。
发光效果如何增强文档?
发光效果增强文本的视觉重点,使其以辉光脱颖而出,提高C# Word文档的整体专业性和可读性。
在企业应用中使用发光效果应考虑哪些因素?
保证与Iron Software的最新更新兼容,遵循可访问性指南,并考虑许可扩展以继续提供支持和功能。
Alpha透明度会影响文档性能吗?
Alpha透明度提供更逼真的效果,并不会显著影响性能,但建议在不同背景上测试以确保在各类应用中可读。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。