如何在 C# 中添加漸變文字效果
若要在 C# 中添加文字漸變效果,請使用 IronWord 的 TextStyle 類別及其 GradientEffect 屬性,該屬性允許您透過內建漸變或自訂漸變停點,在文字字元間套用平滑的色彩過渡。
文字的漸層效果是指在文字的字元或背景上應用平滑的色彩過渡,使顏色從一種色調漸變至另一種色調,或呈現多色漸變的效果。 此效果為文字增添了深度、視覺吸引力與動態感,使其脫穎而出並提升美感。 漸層效果可分為線性(顏色沿直線過渡)或放射狀(顏色從中心點向外過渡)。 在文件處理應用程式中,漸變文字效果常被用於頁首、標題、宣傳資料,以及任何需要視覺強調的內容。
快速入門:使用 IronWord 為文字添加漸變效果
以下是一個簡單範例,展示如何使用 IronWord 為文字套用內建的漸層效果。 在執行此程式碼之前,請確保您已為 IronWord 設定好授權金鑰。
-
using NuGet 套件管理員安裝 https://www.nuget.org/packages/IronWord
PM > Install-Package IronWord -
請複製並執行此程式碼片段。
using IronWord; using IronWord.Models; WordDocument doc = new WordDocument(); TextStyle textStyle = new TextStyle(); textStyle.TextEffect = new TextEffect() { GradientEffect = Gradient.DefaultGray }; Paragraph paragraph = new Paragraph(); Run textRun = new Run(new TextContent("Gradient Text")); textRun.Style = textStyle; paragraph.AddChild(textRun); doc.AddParagraph(paragraph); doc.SaveAs("out.docx"); -
部署至您的生產環境進行測試
立即透過免費試用,在您的專案中開始使用 IronWord
如何為文字添加漸變效果(5 個步驟)
- 安裝 IronWord:
Install-Package IronWord - 建立
TextStyle並使用Gradient.DefaultGray等預設值來設定GradientEffect - 建立一個包含
TextContent的Run,並將TextStyle指派給該Run - 使用
AddChild將Run加入段落 - 儲存文件
如何添加漸變效果?
製作漸變文字需要哪些步驟?
若要套用漸層效果,請遵循 IronWord 的多步驟模式:建立一個 TextStyle,並將其 TextEffect 屬性填入 GradientEffect。 接著建立一個 Paragraph,其後接一個包含 TextContent 的 Run。 將 TextStyle 指派給 Run(而非 TextContent),然後使用 AddChild 將 Run 加入 Paragraph 中。 此文件遵循以下層級結構:文件 → 段落 → 執行 → 文字內容。
: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,
};
// Create paragraph
Paragraph paragraph = new Paragraph();
// Create run with text and style
Run textRun = new Run(new TextContent("Hello World"));
textRun.Style = textStyle;
// Add run to paragraph
paragraph.AddChild(textRun);
// Add paragraph to document
doc.AddParagraph(paragraph);
// Export new Word document
doc.SaveAs("gradientEffect.docx");
Imports IronWord
Imports IronWord.Models
' Create new Word document
Dim doc As New WordDocument()
' Create and configure text style
Dim textStyle As New TextStyle()
textStyle.TextEffect = New TextEffect() With {
.GradientEffect = Gradient.DefaultGray
}
' Create paragraph
Dim paragraph As New Paragraph()
' Create run with text and style
Dim textRun As New Run(New TextContent("Hello World"))
textRun.Style = textStyle
' Add run to paragraph
paragraph.AddChild(textRun)
' Add paragraph to document
doc.AddParagraph(paragraph)
' Export new Word document
doc.SaveAs("gradientEffect.docx")
有哪些內建的漸層選項可用?
IronWord 提供數種預設的漸層預設值,可透過 Gradient 類別的靜態屬性存取,包括 DefaultGray 及其他色彩組合,無需自訂設定即可立即套用。 這些預設值提供類似於 WORD 文字格式設定對話方塊中的快速樣式選項。 內建的漸層效果可與標準文件範本配合使用,並在不同文件格式下維持可讀性。
在生產環境中使用 Gradient Effects 時,請務必檢視授權選項,以確保您的應用程式具備符合部署情境所需的適當授權範圍。
我可以自訂哪些屬性來設定漸層效果?
漸變色停點如何運作?
漸層效果提供可調整的屬性,以滿足各種設計需求。 請參閱以下清單以了解各屬性的詳細說明:
GradientStop
- Color:取得或設定漸層停點的配色方案。顏色可透過 IronWord 的預設色彩常數或自訂 RGB 值來指定。
- StopPoint:取得或設定漸變停點的位置。數值通常介於 0 至 100 之間,代表沿漸變路徑的百分比位置。
漸變停點是指漸變曲線中定義了特定顏色的點。 多個色階能創造出平滑的色彩過渡,而色階之間的間距則決定了色彩變化呈現出漸變或突變的效果。
Gradient
- StopPoints:取得或設定定義漸層填充的漸層停點清單。 基本漸變效果至少需要兩個色階。
- LinearShadeScaled:取得或設定一個值,用以指示線性陰影是否經過縮放。 當設定為 true 時,漸層會自動調整以適應文字邊界。
- 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,
}
};
// Create paragraph
Paragraph paragraph = new Paragraph();
// Create run with text and style
Run textRun = new Run(new TextContent("Hello World"));
textRun.Style = textStyle;
// Add run to paragraph
paragraph.AddChild(textRun);
// Add paragraph to document
doc.AddParagraph(paragraph);
// Export new Word document
doc.SaveAs("customizedGradientEffect.docx");
Imports IronWord
Imports IronWord.Models
Imports System.Collections.Generic
' Create new Word document
Dim doc As New WordDocument()
' Create gradient stops
Dim firstGradientStop As New GradientStop() With {
.Color = IronWord.Models.Color.Aqua,
.StopPoint = 1
}
Dim secondGradientStop As New GradientStop() With {
.Color = IronWord.Models.Color.OrangeRed,
.StopPoint = 10
}
' Create and configure text style
Dim 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
}
}
' Create paragraph
Dim paragraph As New Paragraph()
' Create run with text and style
Dim textRun As New Run(New TextContent("Hello World"))
textRun.Style = textStyle
' Add run to paragraph
paragraph.AddChild(textRun)
' Add paragraph to document
doc.AddParagraph(paragraph)
' Export new Word document
doc.SaveAs("customizedGradientEffect.docx")
哪些角度值會產生不同的效果?
LinearShadeAngle 接受 0 至 360 度的數值,其中 0° 會產生從左至右的水平漸變,90° 會產生從上至下的垂直漸變,而 45° 則會產生如上例所示的對角線漸變效果。 常見的視角設定包括:
- 0°(水平由左至右):產生左右方向的漸變效果,非常適合用於現代標題
- 90°(垂直自上而下):產生自上而下的淡入淡出效果
- 45°(對角線):產生從一角到另一角的漸變效果
- 180°(水平由右至左):反轉標準的水平漸變方向
- 270°(垂直自下而上):產生向上漸變效果
在多個文件或範本中實施漸變效果時,請考慮授權擴充套件,以確保能持續獲得更新與技術支援。
漸變文字效果的最佳實踐
在Professional文件中為文字套用漸層效果時,請遵循以下準則:
- 可讀性優先:確保漸層顏色與文件背景之間有足夠的對比度
- 色彩協調:選擇與文件整體設計風格相襯的顏色
- 商務文件的細微之處:正式文件應採用低調的漸層效果,而非鮮明的色彩過渡
- 效能考量:包含多個色階的複雜漸層可能會影響文件渲染速度
- 跨平台相容性:測試漸層效果在不同 WORD 檢視器及 PDF 匯出檔案中的顯示效果
漸變效果特別適用於章節標題、段落標題和說明文字等文件元素,透過視覺強調能提升讀者的導覽體驗。
常見問題
如何在 C# 中添加漸變文字效果?
若要在 C# 中添加文字漸變效果,請使用 IronWord 的 TextStyle 類別及其 GradientEffect 屬性。建立一個 TextStyle 物件,將 GradientEffect 屬性設定為 Gradient 物件,並將此樣式套用至您的文字。IronWord 允許您透過內建漸變或自訂漸變停點,在文字字元間應用平滑的色彩過渡。
有哪些內建的漸層選項可用?
IronWord 提供數種預設的漸層預設值,可透過 Gradient 類別的靜態屬性存取,包括 DefaultGray 及其他色彩組合。這些預設值無需自訂設定即可立即套用,提供類似 Microsoft WORD 文字格式設定對話方塊中的快速樣式選項。
除了內建選項之外,我能否建立自訂的漸層效果?
是的,IronWord 允許您透過自訂漸變停點來建立自訂漸變效果。雖然內建的漸變(如 DefaultGray)能提供快速解決方案,但您亦可設定 GradientEffect 屬性,以建立專屬的色彩過渡效果,並自訂文字輪廓的外觀。
文字可以套用哪些類型的漸層效果?
IronWord 支援漸層效果,可在文字字元間創造平滑的色彩過渡。這些效果包含線性漸層(色彩沿直線過渡)或放射狀漸層(色彩從中心點向外過渡),能為標題、副標題及宣傳素材增添立體感與視覺吸引力。
如何對現有文字套用漸層效果?
透過 IronWord,您可以將漸層效果套用至新建立或現有的文字。只需建立一個具有所需 GradientEffect 屬性的 TextStyle 物件,並將其指派給文字的 Style 屬性。當您儲存文件時,漸層效果即會套用。

