如何在 C# | IronWord 中為文字加入陰影效果

如何在 C# 中為文字添加陰影效果

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

文字陰影效果是一種視覺增強技術,用於為文字元素創造深度和區分度。 應用後,它會在原始文字後面引入一個文字的副本,略微偏移,從而產生陰影效果。 這種被稱為陰影的輔助文字可以透過多種方式進行調整,以達到不同的視覺效果。

快速入門:一行程式碼新增預設陰影效果

以下是如何使用 IronWord 為 Word 文件文字添加陰影效果——只需一行程式碼即可定義樣式和陰影,然後儲存。 非常適合希望以最少的設定快速獲得結果的開發人員。

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronWord

    PM > Install-Package IronWord

  2. 複製並運行這段程式碼。

    new IronWord.WordDocument().AddText("Shadow!").Style = new IronWord.Models.TextStyle { TextEffect = new IronWord.Models.TextEffect { ShadowEffect = IronWord.Models.Shadow.OuterShadow1 } };
  3. 部署到您的生產環境進行測試

    立即開始在您的專案中使用 IronWord,免費試用!
    arrow pointer

添加陰影效果

若要指定文字的陰影效果,請建立TextStyle對象,並使用Shadow物件填入 ShadowEffect 屬性。 最後,透過將TextStyle物件指派給 TextEffect 屬性來新增具有該樣式的新文字。

:path=/static-assets/word/content-code-examples/how-to/text-effect-shadow-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()
{
    ShadowEffect = Shadow.OuterShadow1,
};

// Add text with style
doc.AddText("Hello World").Style = textStyle;

// Export new Word document
doc.SaveAs("shadowEffect.docx");
Imports IronWord
Imports IronWord.Models

' Create new Word document
Private doc As New WordDocument()

' Create and configure text style
Private textStyle As New TextStyle()
textStyle.TextEffect = New TextEffect() With {.ShadowEffect = Shadow.OuterShadow1}

' Add text with style
doc.AddText("Hello World").Style = textStyle

' Export new Word document
doc.SaveAs("shadowEffect.docx")
$vbLabelText   $csharpLabel
添加陰影效果

陰影效果屬性

除了設定預先定義的陰影值外,還可以配置陰影效果的所有屬性。 這提供了一個非常靈活的選項,可以以任何可能的方式自訂陰影效果。 請查看以下房產及其描述:

-對齊方式:取得或設定陰影的對齊方式。

  • BlurRadius :取得或設定陰影效果的模糊半徑。 模糊半徑以點(1/72 英吋)為單位指定。
  • DirectionAngle :取得或設定陰影效果的方向角度。 方向角以度為單位。
  • DistanceFromText :取得或設定陰影效果與文字或物件之間的距離。 距離以磅(1/72 英吋)為單位。
  • HorizontalScalingFactor :取得或設定陰影效果的水平縮放因子。
  • HorizontalSkewAngle :取得或設定陰影效果的水平傾斜角度。 傾斜角度以度為單位。
  • SchemeColor :取得或設定陰影效果的配色方案顏色。
  • VerticalScalingFactor :取得或設定陰影效果的垂直縮放因子。
  • VerticalSkewAngle :取得或設定陰影效果的垂直傾斜角度。 傾斜角度以度為單位。
:path=/static-assets/word/content-code-examples/how-to/text-effect-customized-shadow-effect.cs
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;

// Create new Word document
WordDocument doc = new WordDocument();

// Create and configure text style
TextStyle textStyle = new TextStyle();
textStyle.TextEffect = new TextEffect()
{
    ShadowEffect = new Shadow()
    {
        Alignment = RectangleAlignmentValues.BottomLeft,
        BlurRadius = 5,
        DirectionAngle = 45,
        DistanceFromText = 3,
        HorizontalScalingFactor = 100,
        VerticalScalingFactor = 100,
        HorizontalSkewAngle = 0,
        SchemeColor = IronWord.Models.Color.Aqua,
        VerticalSkewAngle = 0,
    },
};

// Add text with style
doc.AddText("Customized shadow").Style = textStyle;

// Export new Word document
doc.SaveAs("customizedShadowEffect.docx");
Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums

' Create new Word document
Private doc As New WordDocument()

' Create and configure text style
Private textStyle As New TextStyle()
textStyle.TextEffect = New TextEffect() With {
	.ShadowEffect = New Shadow() With {
		.Alignment = RectangleAlignmentValues.BottomLeft,
		.BlurRadius = 5,
		.DirectionAngle = 45,
		.DistanceFromText = 3,
		.HorizontalScalingFactor = 100,
		.VerticalScalingFactor = 100,
		.HorizontalSkewAngle = 0,
		.SchemeColor = IronWord.Models.Color.Aqua,
		.VerticalSkewAngle = 0
	}
}

' Add text with style
doc.AddText("Customized shadow").Style = textStyle

' Export new Word document
doc.SaveAs("customizedShadowEffect.docx")
$vbLabelText   $csharpLabel
自訂陰影效果

常見問題解答

如何在 C# 中為文字加入陰影效果?

您可以使用 IronWord 函式庫在 C# 中為文字加入陰影效果。首先,下載該函式庫,然後使用 Shadow 類應用陰影效果。根據需要配置屬性,然後輸出已套用效果的文件。

IronWord 中的陰影效果有哪些可自訂的屬性?

IronWord 允許您自訂陰影效果的幾個屬性,包括 Alignment, BlurRadius, DirectionAngle, DistanceFromTextHorizontalScalingFactor, HorizontalSkewAngle, SchemeColor, VerticalScalingFactor, 和 VerticalSkewAngle.

如何使用 IronWord 套用預設的陰影效果?

若要使用 IronWord 套用預設的陰影效果,請使用 Shadow 類的靜態命名實例,並配置其屬性以符合您的需求。

我可以為 Word 文件中的現有文字套用陰影效果嗎?

是的,您可以透過建立 TextStyle 物件,並將 ShadowEffect 屬性填充為 Shadow 物件,使用 IronWord 將陰影效果套用至 Word 文件中新建立和現有的文字。

BlurRadius 屬性在陰影效果中扮演什麼角色?

陰影效果中的 BlurRadius 屬性指定模糊半徑,單位為點(1/72 英吋),它決定陰影顯示的柔和或銳利程度。

如何控制 Word 文件中陰影效果的方向?

若要在 IronWord 中控制陰影效果的方向,請設定 DirectionAngle 屬性,此屬性的指定單位為度,可依需要調整陰影的方向。

是否可以將陰影與文字以不同的方式對齊?

是的,IronWord 的 Alignment 屬性允許您設定陰影相對於文字的對齊方式,提供陰影定位方式的彈性。

如何設定陰影效果的顏色以符合我的文件主題?

使用 IronWord 中的 SchemeColor 屬性來設定陰影效果的配色方案,讓陰影與您文件的整體設計主題相符。

DistanceFromText 屬性在陰影效果中控制什麼?

DistanceFromText 屬性控制陰影與文字或物件的距離,以點為單位 (1/72 英吋),影響陰影與文字的距離。

Curtis Chau
技術作家

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

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

準備好開始了嗎?
Nuget 下載 27,129 | Version: 2025.11 剛發表