如何在 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 文檔文字加上陰影 - 只需一行定義樣式和陰影,Plus 即可儲存。 以最少的設定快速實作。在實施陰影效果之前,請確保您已正確設定 授權金鑰,以避免在生產文件中出現水印。

  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() { 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

如何為文字新增陰影效果?

To apply a shadow effect, create a TextStyle and populate its TextEffect property with a ShadowEffect. Then create a Paragraph, followed by a Run containing TextContent. Assign the TextStyle to the Run (not the TextContent), then use AddChild to add the Run to the Paragraph. 此文件遵循以下層級結構:文件 → 段落 → 執行 → 文字內容。

實作流程遵循簡單直接的模式,可與現有的文件生成工作流程無縫整合。 無論您是建立自動化報告、產生證書或建立品牌文件,陰影效果都能為您的文字元素增添專業的光澤。 對於考慮授權選項的組織而言,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
顯示

如何自訂陰影效果屬性?

除了設定預先定義的陰影值外,還可以配置陰影效果的所有屬性。 這提供了一個靈活的選項,以任何可能的方式自訂陰影效果。 請參閱下列屬性及其說明。

對於評估 License 延伸功能升級的團隊而言,自訂陰影效果展現了 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 點之間效果最佳,可呈現細微的視覺效果。

瞭解這些範圍有助於快速達到專業效果。 針對商業文件,採用保守的數值 (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 的陰影效果特別有用。它們可增加文字元素的深度和視覺上的區別,因此非常適合自動化報告、證書和品牌文件。

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。