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

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

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

在 C# 中使用 IronWord 為文字添加陰影效果,方法是建立一個具有 ShadowEffect 屬性的 TextStyle 物件,接著套用預設陰影(如 OuterShadow1),或自訂模糊度、距離及顏色等屬性,以呈現 Professional 的文字立體感。

文字的"陰影效果"是一種視覺增強技術,用於為文字元素增添深度與層次感。 應用此效果時,會在原始文字後方疊加一份複製的文字,並略微偏移位置以營造出陰影的效果。 這段稱為"陰影"的輔助文字,可透過多種方式進行調整,以呈現不同的視覺效果。

陰影效果在製作專業文件、簡報和報告時特別有用,這些場合通常需要讓文字更為突出。 就像您在 PowerPoint 中建立空白簡報一樣,IronWord 讓您能夠透過程式設計,為 WORD 文件增添精緻的文字特效。 此函式庫既提供預設的陰影選項以利快速實作,也具備廣泛的自訂功能,可滿足獨特的品牌需求。

快速入門:一行代碼即可添加預設陰影效果

以下是使用 IronWord 為 WORD 文件文字添加陰影的效果——只需一行代碼定義樣式與陰影,然後儲存即可。 快速實作且設定簡便。在實作陰影效果前,請確保已正確設定授權金鑰,以避免正式文件中出現浮水印。

  1. using 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() { ShadowEffect = Shadow.OuterShadow1 };
    Paragraph paragraph = new Paragraph();
    Run textRun = new Run(new TextContent("Shadow Text"));
    textRun.Style = textStyle;
    paragraph.AddChild(textRun);
    doc.AddParagraph(paragraph);
    doc.SaveAs("shadow.docx");
  3. 部署至您的生產環境進行測試

    立即透過免費試用,在您的專案中開始使用 IronWord

    arrow pointer

如何為文字添加陰影效果?

若要套用陰影效果,請建立 TextStyle,並將其 TextEffect 屬性填入 ShadowEffect。 接著建立一個 Paragraph,其後接一個包含 TextContentRun。 將 TextStyle 指派給 Run(而非 TextContent),然後使用 AddChildRun 加入 Paragraph 中。 此文件遵循以下層級結構:文件 → 段落 → 執行 → 文字內容。

其實作流程遵循直觀的模式,能與現有的文件生成工作流程無縫整合。 無論您是建立自動化報告、產生憑證,還是製作品牌文件,陰影效果都能為您的文字元素增添 Professional 質感。 對於考慮授權方案的組織,IronWord 的陰影效果已包含在所有授權層級中,確保開發、測試和生產環境間的功能一致性。

有哪些預設陰影效果可供使用?

IronWord 提供數種內建的陰影預設值,例如 OuterShadow1OuterShadow20,可呈現不同的視覺風格。 這些預設設定可讓您無需手動配置,即可快速完成實作。 每個預設值均經過精心設計,以符合專業文件製作中的常見使用情境:

  • OuterShadow1-5:用於正文與標題的細微陰影
  • OuterShadow6-10:適用於標題與重點標示的中等強度陰影
  • OuterShadow11-15:適用於封面頁與章節分隔線的粗體陰影
  • OuterShadow16-20:為簡報與創意文件增添戲劇性效果

如欲掌握最新預設項的增補與改進,請定期查閱變更紀錄。 開發團隊會根據使用者回饋與產業趨勢,持續優化這些預設設定。

何時該使用預設陰影效果,何時該使用自訂陰影效果?

請使用預設樣式以確保標準文件格式與快速實作。 當您有特定的品牌要求,或需要預設值無法提供的獨特視覺效果時,請選擇自訂陰影。預設陰影在需要跨多份文件保持一致性的情境中表現出色,例如企業範本或標準化報告。

當需遵循品牌指南中對色彩值、定位或模糊效果有精確規範時,自訂陰影便顯得至關重要。 行銷團隊通常需要精確的陰影規格,以確保所有宣傳品在視覺形象上保持一致。 此外,自訂陰影功能可創造出創意效果,例如多層次陰影,或是與特定背景顏色相襯的陰影。

基本實作模式為何?

建立 WordDocument,使用 ShadowEffect 設定 TextStyle,將該樣式套用至您的文字,並儲存文件。 無論使用預設值或自訂設定,此模式均保持一致。

: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,
};

// 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("shadowEffect.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 {
    .ShadowEffect = Shadow.OuterShadow1
}

' 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("shadowEffect.docx")
$vbLabelText   $csharpLabel
顯示

如何自訂陰影效果的屬性?

除了指定預設的陰影值外,陰影效果的所有屬性皆可進行設定。 這提供了一種靈活的選項,可依任何方式自訂陰影效果。 請參閱以下屬性及其說明。

對於正在評估授權擴展升級的團隊而言,自訂陰影效果彰顯了 IronWord 致力於提供企業級文件處理功能的承諾。 豐富的自訂選項確保您對 IronWord 的投資能隨著日益增長的文件處理需求而擴展。

哪些屬性控制陰影的位置?

  • 對齊方式:取得或設定陰影的對齊方式。
  • DirectionAngle:取得或設定陰影效果的方向角度。 方向角度以度為單位指定。
  • DistanceFromText:取得或設定陰影效果與文字或物件之間的距離。 距離單位以點(1/72 英吋)為單位。

這些定位屬性相互配合,可創造出逼真的陰影效果。 Alignment 屬性決定陰影相對於文字的錨點位置,而 DirectionAngle 則模擬光源方向。 DistanceFromText 控制文字在頁面表面上的視覺浮凸效果。 有效結合這些屬性,可在文件中營造出彷彿源自一致光源的陰影效果。

哪些屬性會影響陰影的外觀?

  • BlurRadius:取得或設定陰影效果的模糊半徑。 模糊半徑以點(1/72 英吋)為單位指定。
  • SchemeColor:取得或設定陰影效果的配色方案顏色。

外觀屬性會直接影響陰影的視覺品質。 BlurRadius 可產生柔和或硬邊的陰影——較低的數值會產生適合技術文件的清晰陰影,而較高的數值則會產生適合創意設計的柔和陰影。 SchemeColor 讓您能將陰影與文件的色彩方案相匹配,確保內容整體視覺的一致性。

如何控制陰影縮放與傾斜?

  • HorizontalScalingFactor:取得或設定陰影效果的水平縮放係數。
  • HorizontalSkewAngle:取得或設定陰影效果的水平傾斜角度。 傾斜角度以度為單位指定。
  • VerticalScalingFactor:取得或設定陰影效果的垂直縮放係數。
  • VerticalSkewAngle:取得或設定陰影效果的垂直傾斜角度。 傾斜角度以度為單位指定。

縮放與傾斜屬性可實現透視效果,為您的文字增添立體感。 HorizontalScalingFactorVerticalScalingFactor 可拉伸或壓縮陰影,創造出模擬不同視角的效果。 透過傾斜角度,您可以創造類似斜體的陰影效果,或模擬投射在傾斜表面上的陰影,為您的文件增添精緻的視覺深度。

常見的屬性值範圍有哪些?

BlurRadius 通常範圍為 0 至 10 分,DirectionAngle 範圍為 0 至 360 度,而縮放係數則使用百分比值(100 = 正常大小)。 DistanceFromText 通常設定在 1 至 5 點之間效果最佳,可呈現細微的視覺效果。

理解這些範疇有助於快速達成Professional的翻譯成果。 針對商務文件,採用保守的數值 (BlurRadius: 2-4, DistanceFromText: 1-2) 既能維持可讀性,又能增添視覺吸引力。 創意應用可突破這些限制,運用戲劇性效果 (BlurRadius: 8-10, DistanceFromText: 4-6) 以產生強烈的視覺衝擊。 請注意,印表機功能與螢幕解析度會影響陰影的呈現效果,因此請針對預期的輸出方式測試您的文件。

: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,
    },
};

// Create paragraph
Paragraph paragraph = new Paragraph();

// Create run with text and style
Run textRun = new Run(new TextContent("Customized shadow"));
textRun.Style = textStyle;

// Add run to paragraph
paragraph.AddChild(textRun);

// Add paragraph to document
doc.AddParagraph(paragraph);

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

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

' Create and configure text style
Dim 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
    }
}

' Create paragraph
Dim paragraph As New Paragraph()

' Create run with text and style
Dim textRun As New Run(New TextContent("Customized shadow"))
textRun.Style = textStyle

' Add run to paragraph
paragraph.AddChild(textRun)

' Add paragraph to document
doc.AddParagraph(paragraph)

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

常見問題

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

若要使用 IronWord 添加陰影效果,請建立一個 TextStyle 物件,並將 ShadowEffect 屬性設定為 Shadow 物件。您可以使用預設陰影(如 OuterShadow1),或自訂模糊度、距離和顏色等屬性。接著在將文字新增至文件時,將此樣式套用至文字。

我能否快速套用預設的陰影效果,而無需進行自訂?

是的,IronWord 提供預設的陰影選項,以便快速實作。您只需一行程式碼即可套用陰影效果:new IronWord.WordDocument().AddText("Shadow!").Style = new IronWord.Models.TextStyle { TextEffect = new IronWord.Models.TextEffect { ShadowEffect = IronWord.Models.Shadow.OuterShadow1 } };

有哪些陰影自訂選項可用?

IronWord 允許您自訂各種陰影屬性,包括模糊程度、與文字的距離、陰影顏色以及偏移位置。這使您能夠超越預設選項,創建符合品牌需求的獨特陰影效果。

使用陰影效果需要特殊授權嗎?

所有 IronWord 授權層級均包含陰影效果。然而,在實作陰影等文字效果時,您需正確設定授權金鑰,以避免在正式文件中出現浮水印。

在 WORD 文件中,陰影效果有何用途?

在 IronWord 中,陰影效果對於製作需要突顯文字的 Professional 文件、簡報和報告特別有用。這些效果能為文字元素增添深度與視覺層次,使其成為自動化報告、證書及品牌文件的理想選擇。

Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備開始了嗎?
Nuget 下載 44,829 | 版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎? PM > Install-Package IronWord
執行範例 觀看您的資料轉為 WORD 文件。