如何在 C# 中新增漸層文字效果
若要在 C# 中為文字新增漸層效果,請使用 IronWord 的 TextStyle 類別,並在其 TextEffect 屬性中設定 GradientEffect。您可以使用內建漸層或自訂漸層停止點,讓文字的顏色平滑過渡。
漸層效果讓文字字元或背景在兩種以上的顏色之間平滑過渡,增加視覺層次,讓文字更醒目。漸層可以是線性的(顏色沿直線過渡),也可以是徑向的(顏色從中心點向外過渡)。在文件處理應用程式中,漸層文字常用於標頭、標題、宣傳資料或其他需要凸顯的內容。
快速入門:使用 IronWord 為文字新增漸層效果以下範例示範如何用 IronWord 為文字套用內建漸層效果。執行程式碼前,請確認已為 IronWord 設定授權金鑰。
-
1使用 IronWord 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
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");Imports IronWord Imports IronWord.Models Dim doc As New WordDocument() Dim textStyle As New TextStyle() textStyle.TextEffect = New TextEffect() With {.GradientEffect = Gradient.DefaultGray} Dim paragraph As New Paragraph() Dim textRun As New Run(New TextContent("Gradient Text")) textRun.Style = textStyle paragraph.AddChild(textRun) doc.AddParagraph(paragraph) doc.SaveAs("out.docx")Imports IronWord Imports IronWord.Models Dim doc As New WordDocument() Dim textStyle As New TextStyle() textStyle.TextEffect = New TextEffect() With {.GradientEffect = Gradient.DefaultGray} Dim paragraph As New Paragraph() Dim textRun As New Run(New TextContent("Gradient Text")) textRun.Style = textStyle paragraph.AddChild(textRun) doc.AddParagraph(paragraph) doc.SaveAs("out.docx") -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronWord,並使用 免費試用
如何為文字新增漸層效果(5 個步驟)
- 安裝 IronWord:
Install-Package IronWord - 建立
TextStyle,並使用內建預設Gradient.DefaultGray設定GradientEffect - 建立一個包含
TextContent的Run,並將TextStyle指派給該Run - 使用
AddChild將Run加入Paragraph - 儲存文件
我如何新增漸層效果?
建立漸層文字需要哪些步驟?
要套用漸層效果,請先建立 TextStyle,在其 TextEffect 屬性中設定 GradientEffect。接著建立 Paragraph 和包含 TextContent 的 Run。將 TextStyle 指派給 Run(而非 TextContent),再使用 AddChild 將 Run 加入 Paragraph。文件的階層結構為 Document → Paragraph → Run → TextContent。
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");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")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,可作為Gradient類別的靜態欄位,即時應用,無需自訂設定。 對於任何其他的顏色組合,使用StopPoints、LinearShadeAngle和LinearShadeScaled屬性建立一個自訂漸層,如下方的自訂漸層範例所示。 內建預設與標準文件範本配合使用,並在不同的文件格式中保持可讀性。
若要在正式環境中使用漸層效果,可先確認授權選項是否涵蓋應用程式的部署情境。
我可以為漸層效果自訂哪些屬性?
漸層停止點如何運作?
您可以調整漸層效果的各項屬性,以符合不同的設計需求。以下說明各項屬性的用途:
GradientStop
- Color: 取得或設定漸層停止點的配色。可使用 IronWord 的預先定義色彩常數或自訂 RGB 值。
- StopPoint: 取得或設定漸層停止點的位置。常用值為 0 到 100,代表該點在漸層路徑中的百分比位置。
漸層停止點是漸層中指定顏色的位置。多個停止點可讓不同顏色平滑過渡,點與點之間的距離則決定色彩變化的快慢。
Gradient
- StopPoints: 取得或設定漸層填滿所用的停止點清單。基本漸層至少需要兩個停止點。
- LinearShadeScaled: 取得或設定是否縮放線性漸層。值為 true 時,漸層會配合文字邊界調整。
- LinearShadeAngle: 取得或設定線性漸層的角度,控制漸層在文字上的方向。
若團隊計畫擴充文件處理能力,可透過不同的授權選項,將應用範圍擴大至多個專案和開發人員。
我如何建立自訂的漸層效果?
建立自訂漸層效果可讓文字造型獨特,符合您的品牌或設計需求。 以下範例示範如何建立具有特定角度和縮放屬性的雙色漸層。 對於與漸層效果相關的最新功能和改進,請查看產品更新日誌。
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");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")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°(垂直從下到上):建立向上的漸層效果
在多個文件或範本上實施漸層效果時,考慮授權選項以確保持續取得更新和支援。
漸層文字效果的最佳實務
在專業文件中為文字套用漸層效果時,請考慮這些準則:
- 可讀性優先:確保漸層顏色與文件背景之間有足夠的對比
- 顏色和諧:選擇能夠互補您的文件整體設計方案的顏色
- 商業文件中的細緻性:對於正式文件,使用柔和的漸層而非鮮艷的顏色過渡
- 效能考量:具有許多停止點的複雜漸層可能會影響文件呈現速度
- 跨平台相容性: 使用不同的 Word 檢視工具,並檢查匯出後的 PDF,確認漸層的外觀
漸層適合用於章名、節標題或提示文字。凸顯這些元素,有助於讀者快速找到所需的內容。
常見問題
什麼是C# Word文件中的漸層文字效果?
C# Word文件中的漸層文字效果是IronWord提供的功能,可讓您對文字字元應用平滑的顏色過渡。它可以是線性或放射狀的,為您的文件增添深度和視覺趣味。
如何使用IronWord創建漸層文字效果?
要使用IronWord創建漸層文本效果,可以使用TextStyle類並將其TextEffect屬性設置為GradientEffect。這可以通過應用內建的漸層或創建自訂漸層停止點以獲得更個性化的樣式來完成。
IronWord中有哪些內建的漸層選項?
IronWord提供一個內建的漸層預設名為Gradient.DefaultGray。如需額外的顏色組合,您可以使用StopPoints、LinearShadeAngle和LinearShadeScaled等屬性創建自訂漸層。
如何在IronWord中自定義文字的漸層效果?
您可以通過創建一個具有如StopPoints等屬性的Gradient物件來自訂IronWord中的漸層效果,以設置漸層停止點顏色,LinearShadeAngle設置方向,和LinearShadeScaled來縮放漸層以適應文本邊界。
什麼是漸層停止點,它們如何運作?
漸層停止點是在漸層中定義特定顏色的點。它們決定了漸層中的顏色過渡,停止點之間的間距會影響過渡的平滑或急促程度。
我可以在IronWord中使用自定義的RGB值作為漸層停止點嗎?
是的,您可以使用IronWord的預定顏色常數或指定自定義RGB值來定義漸層停止點的自定義顏色,以便更精確地控制顏色。
哪些角度值可以產生不同的線性漸層效果?
在IronWord中,LinearShadeAngle的值範圍從0°至360°。例如,0°創建從左到右的漸層,90°創建從上到下的漸層,而45°則產生對角線的漸層效果。
在應用漸層文字效果時,我應考慮什麼最佳實踐?
在應用漸層文字效果時,確保具有強烈對比度以便閱讀,選擇互補的顏色,對於正式文件使用細微的漸層,並考慮性能影響和跨平台相容性。
IronWord中有方式創建放射狀漸層嗎?
目前,IronWord主要支持線性漸層。放射狀漸層需從中心點向外過渡顏色,需要自定義地操作漸層屬性以達到類似效果。
IronWord中的漸層效果在不同的文件格式中能保持相容性嗎?
是的,IronWord中的內建漸層預設和自定義漸層被設計成能與標準文件範本一起使用,確保在各種Word格式和PDF輸出中可讀。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。