如何在 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點以上)適合封面或需要高影響力的促銷。
如何為輝光效果應用透明度?
通過設置GlowColor的alpha值來使用透明度,其範圍從0(透明)到255(不透明)。這使輝光效果能夠自然地與不同背景融合。
使用IronWord實現輝光效果的一般步驟是什麼?
首先,安裝IronWord。然後,創建一個TextStyle並配置一個具有所需屬性的GlowEffect。將此分配給段落中的Run,然後將其保存為Word文件。
輝光效果如何增強文件?
輝光效果增強了文本的視覺強調,讓其以發光光環突出,提高C# Word文件的專業性和可讀性。
在企業應用中使用輝光效果應考慮哪些因素?
確保與Iron Software的最新更新兼容,遵循無障礙指南,並考慮延長授權以獲得持續支援和功能可用性。
透明度是否會影響文件效能?
透明度提供了更逼真的效果,不會顯著影響效能,但建議在不同背景上進行測試以確保跨應用的可讀性。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。