如何在 C# 中配置 PDF 打印設置 | IronPrint

如何在 C# 中加入漸層文字效果

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

若要在 C# 中添加文字漸變效果,請使用 IronWord 的 TextStyle 類別及其 GradientEffect 屬性,該屬性允許您透過內建漸變或自訂漸變停點,在文字字元間套用平滑的色彩過渡。

文字漸變效果是指在文字字元或背景上套用平滑的顏色過渡,從而創造從一種顏色到另一種顏色或多種顏色的混合效果。 這種效果為文字增添了深度、視覺趣味和動態外觀,使其脫穎而出,增強了其美感。 漸變效果可以是線性的(顏色沿直線過渡)或徑向的(顏色從中心點向外過渡)。 在文件處理應用程式中,漸層文字效果通常用於標頭、標題、宣傳資料以及任何需要視覺強調的內容。

快速入門:使用 IronWord 為文字添加漸層效果

以下是一個簡單的範例,說明如何使用 IronWord 在文字上套用內建的漸層效果。 在執行此程式碼之前,請確保您已設定 IronWord 的授權金鑰

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/IronWord

    PM > Install-Package IronWord
  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");
  3. 部署到您的生產環境進行測試

    今天就在您的專案中開始使用免費試用IronWord

    arrow pointer

如何新增漸層效果?

建立漸層文字需要哪些步驟?

若要套用漸層效果,請遵循 IronWord 的多步驟模式:建立一個 TextStyle,並將其 TextEffect 屬性填入 GradientEffect。 接著建立一個 Paragraph,其後接一個 Run,並包含 TextContent。 將 TextStyle 指派給 Run(而非 TextContent),然後使用 AddChildRun 加入 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")
$vbLabelText   $csharpLabel
Microsoft Word 顯示

有哪些內建的漸層選項可用?

IronWord 提供數種預設的漸層預設值,可透過 Gradient 類別的靜態屬性存取,包括 DefaultGray 及其他色彩組合,無需自訂設定即可立即套用。 這些預設提供類似 Microsoft Word 文字格式化對話方塊中的快速樣式選項。 內建的漸層可與標準的文件範本搭配使用,並維持不同文件格式的可讀性。

在生產環境中使用漸層效果時,請考慮檢閱授權選項,以確保您的應用程式有適當的涵蓋範圍來滿足您的部署情境。

漸層效果可以自訂哪些屬性?

漸層停點如何運作?

漸層效果可針對各種設計需求提供可調整的屬性。 以下列表詳細介紹了每個房產:

漸變停止

  • 顏色:取得或設定漸層止點的方案顏色。可以使用 IronWord 的預定義顏色常數或自訂 RGB 值來指定顏色。
  • StopPoint:取得或設定漸層停止點的位置。值的範圍通常是 0 到 100,代表沿著漸層路徑的百分比位置。

漸變停止點是指漸層中定義特定顏色的點。 多個停格可以創造顏色間的平滑過渡,而停格間的間距則決定顏色變化的漸進或突兀程度。

漸層

  • StopPoints :取得或設定定義漸層填滿的漸層停止點清單。 基本漸層至少需要兩個句點。
  • LinearShadeScaled :取得或設定一個值,該值指示線性陰影是否縮放。 當為真時,漸層會調整以符合文字邊界。
  • LinearShadeAngle :取得或設定線性遮光簾的角度。 此屬性可控制文字的漸層流動方向。

對於計劃擴充文件處理能力的 Team License,升級選項提供了靈活的途徑,可讓您在多個專案和開發人員之間擴大實作規模。

如何建立自訂漸層效果?

建立自訂漸層效果可讓文字造型獨特,符合您的品牌或設計需求。 以下範例示範如何建立具有特定角度和縮放屬性的雙色漸層。 有關漸層效果的最新功能和改進,請查看 產品變更表

: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")
$vbLabelText   $csharpLabel
Microsoft Word 文件顯示

哪些角度值會產生不同的效果?

LinearShadeAngle 接受 0 到 360 度的值,其中 0° 會建立從左至右的水平漸層,90° 會建立從上至下的垂直漸層,而 45° 則會建立對角線的漸層效果,如上圖範例所示。 常見的角度配置包括

  • 0°(水平從左至右):建立側向漸層,適用於現代標題
  • 90°(垂直自上而下):產生由上而下的淡入淡出效果
  • 45°(對角線):產生角到角的轉換
  • 180°(水平從右至左):扭轉標準的水平漸層方向
  • 270°(垂直自下而上):創造向上的漸層效果

在多個文件或模板中實作漸層效果時,請考慮授權擴充套件,以確保能持續取得更新與支援。

漸層文字效果的最佳實作

在專業文件中為文字套用漸層效果時,請考慮這些準則:

1.可讀性第一:確保漸層顏色與文件背景之間有足夠的對比度 2.色彩協調:選擇與您的文件整體設計方案相輔相成的顏色 3.商業文件中的細節:對於正式的文件,請使用柔和的漸層,而非大膽的顏色轉換 4.效能考量:具有許多停點的複雜漸層可能會影響文件的呈現速度 5.跨平台相容性:在不同的 Word 檢視器和 PDF 匯出中測試漸層外觀

漸層效果特別適用於章節標題、小節標題和喚出文字等文件元素,在這些地方,視覺強調可以增強讀者的導覽體驗。

常見問題解答

如何在 C# 中加入漸層文字效果?

若要在 C# 中加入漸層文字效果,請使用 IronWord 的 TextStyle 類與 GradientEffect 屬性。建立一個 TextStyle 物件,將 GradientEffect 屬性填入一個 Gradient 物件,然後將這個樣式指定給文字。IronWord 允許您使用內建的漸層或自訂的漸層停止點,在文字字元間套用平滑的顏色轉換。

有哪些內建的漸層選項?

IronWord 提供多種預設漸層,可透過 Gradient 類的靜態屬性存取,包括 DefaultGray 和其他顏色組合。這些預設值無須自訂設定即可立即套用,提供類似 Microsoft Word 文字格式化對話框中的快速樣式選項。

除了內建的選項之外,我還可以建立自訂的漸層效果嗎?

是的,IronWord 允許您使用自訂的漸層停止點來建立自訂的漸層效果。雖然內建的漸層(如 DefaultGray)可提供快速的解決方案,但您可以設定 GradientEffect 屬性,以建立自己的顏色轉換和自訂文字輪廓外觀。

有哪些類型的漸層效果可以套用在文字上?

IronWord 支援漸層效果,可在文字字元間建立平滑的顏色轉換。這些效果可包括線性漸層(顏色在直線上轉換)或徑向漸層(顏色從中心點向外轉換),為標題、標題和宣傳資料增添深度和視覺趣味。

如何在現有文字上套用漸層效果?

使用 IronWord,您可以將漸層效果套用到新建立和現有的文字上。只需建立一個具有所需 GradientEffect 屬性的 TextStyle 物件,並將其指定給文字的 Style 屬性。當您儲存文件時,就會套用漸層效果。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 37,916 | 版本: 2026.4 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package IronWord
執行範例 觀看您的資料變成 Word doc。