如何在 C# 中為文字新增陰影效果
若要在 C# 中使用 IronWord 為文字新增陰影,請建立 TextStyle 物件來設定 ShadowEffect。您可以套用 OuterShadow1 等預設陰影,也可以自訂模糊程度、距離和顏色,為文字增加立體感。
套用陰影效果時,會在原文後方加上一份略微偏移的文字副本,讓文字更有立體感,也更容易與周圍內容區分。調整這份副本的外觀,即可呈現不同的陰影效果。
陰影效果適合用在需要凸顯文字的專業文件、簡報和報告。就像可以透過程式碼在 PowerPoint 中建立空白簡報,您也可以用 IronWord 為 Word 文件套用進階文字效果。這套程式庫提供可快速套用的預設陰影,也提供多種自訂選項以符合品牌需求。
快速入門:一行程式碼即可新增預設陰影效果使用 IronWord 為 Word 文件的文字新增陰影時,只需一行程式碼定義樣式和陰影,再儲存文件,所需設定很少。套用陰影效果前,請確認已正確設定授權金鑰,以免正式環境產生的文件出現浮水印。
-
1使用 IronWord 安裝 NuGet Package Manager
-
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");Imports IronWord Imports IronWord.Models Dim doc As New WordDocument() Dim textStyle As New TextStyle() textStyle.TextEffect = New TextEffect() With {.ShadowEffect = Shadow.OuterShadow1} Dim paragraph As New Paragraph() Dim textRun As New Run(New TextContent("Shadow Text")) textRun.Style = textStyle paragraph.AddChild(textRun) doc.AddParagraph(paragraph) doc.SaveAs("shadow.docx")Imports IronWord Imports IronWord.Models Dim doc As New WordDocument() Dim textStyle As New TextStyle() textStyle.TextEffect = New TextEffect() With {.ShadowEffect = Shadow.OuterShadow1} Dim paragraph As New Paragraph() Dim textRun As New Run(New TextContent("Shadow Text")) textRun.Style = textStyle paragraph.AddChild(textRun) doc.AddParagraph(paragraph) doc.SaveAs("shadow.docx") -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronWord,並使用 免費試用
如何為文字新增陰影效果(5 個步驟)
- 安裝 IronWord:
Install-Package IronWord - 建立
TextStyle,並使用Shadow.OuterShadow1等預設值來設定ShadowEffect - 建立一個包含
TextContent的Run,並將TextStyle指派給該Run - 使用
AddChild將Run加入Paragraph - 儲存文件
如何為文字新增陰影效果?
要套用陰影效果,請建立 TextStyle,在其 TextEffect 屬性中設定 ShadowEffect。接著建立 Paragraph 和包含 TextContent 的 Run。將 TextStyle 指派給 Run(而非 TextContent),再使用 AddChild 將 Run 加入 Paragraph。文件的階層結構為 Document → Paragraph → Run → TextContent。
這套做法可融入現有的文件產生流程。無論是自動產生報告、製作證書或建立品牌文件,陰影都能讓文字呈現更專業的外觀。IronWord 的授權層級只有部署範圍、開發人員數量及支援期間不同,各層級提供相同的功能,因此在開發、測試和正式環境中都能以相同方式使用陰影效果。
有哪些可用的預設陰影效果?
IronWord 提供一種內建陰影預設 Shadow.OuterShadow1,不必手動設定就能快速套用。若需要更低調、醒目或強烈的效果,可建立自訂 Shadow 執行個體,並直接設定 Alignment、BlurRadius、DirectionAngle、DistanceFromText、SchemeColor 以及縮放和傾斜屬性。設定方式請參閱下方的自訂陰影說明。
為及時取得新功能和增強功能,請定期檢查更新日誌。
何時應使用預設與自訂陰影效果?
使用預設的陰影來進行標準的文件格式和快速實作。 當您需要預設無法提供的特定品牌需求或獨特視覺效果時,請選擇自訂陰影。預設陰影適用於對多個文件的一致性要求極高的情況,例如企業範本或標準報告。
如果品牌規範對色彩值、位置或模糊程度有明確要求,自訂陰影就很實用。行銷團隊通常需要精確的陰影規格,讓各種宣傳品維持一致的視覺識別。自訂陰影也可用來建立多層陰影,或讓陰影與特定背景色搭配。
套用陰影的基本流程為何?
建立 WordDocument,使用 ShadowEffect 設定 TextStyle,將樣式套用到文字,再儲存文件。預設和自訂陰影都使用相同流程。
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");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")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")
如何自訂陰影效果屬性?
除了套用預設陰影,您也可以設定陰影效果的各項屬性,自行調整效果。各屬性的用途如下。
對於正在評估授權擴充或升級的團隊,自訂陰影是 IronWord 企業級文件處理功能的一例。多種自訂選項可因應日益增加的文件處理需求。
哪些屬性控制陰影定位?
- Alignment:取得或設定陰影的對齊方式。
- DirectionAngle:取得或設定陰影效果的方向角度。 方向角以度為單位。
- DistanceFromText:取得或設定陰影效果與文字或對象之間的距離。 距離以點(1/72 英吋)為單位。
這些定位屬性可搭配使用,讓陰影更逼真。Alignment 決定陰影相對於文字的錨點,DirectionAngle 模擬光源方向,DistanceFromText 控制文字看起來離頁面有多高。適當搭配這些屬性,可讓整份文件的陰影看起來由相同方向的光源投射。
哪些屬性會影響陰影的外觀?
- BlurRadius:取得或設定陰影效果的模糊半徑。 模糊半徑以點(1/72 英吋)為單位指定。
- SchemeColor: 取得或設定陰影效果的配色。
這些屬性會直接影響陰影的外觀。BlurRadius 的值越低,陰影邊緣越清晰,適合技術文件,數值越高,陰影越柔和,適合創意設計。SchemeColor 可讓陰影配合文件的配色,維持視覺一致性。
如何控制陰影的縮放和傾斜?
- HorizontalScalingFactor:取得或設定陰影效果的水平縮放比例。
- HorizontalSkewAngle:取得或設定陰影效果的水平傾斜角度。 傾斜角度以度為單位。
- VerticalScalingFactor:取得或設定陰影效果的垂直縮放比例。
- VerticalSkewAngle:取得或設定陰影效果的垂直傾斜角度。 傾斜角度以度為單位。
縮放與傾斜屬性可讓透視效果增加文字的立體感。 HorizontalScalingFactor和VerticalScalingFactor拉伸或壓縮陰影,在不同的視角下創造效果。 傾斜角度可讓您建立類似斜體的陰影效果,或模擬投射在有角度表面上的陰影,為您的文件增添精密的視覺深度。
常見的屬性值範圍是什麼?
BlurRadius 通常為 0 到 10 點,DirectionAngle 為 0 到 360 度,縮放比例以百分比表示(100 代表原始大小)。若想要較低調的陰影,DistanceFromText 設為 1 到 5 點通常效果最佳。
瞭解這些範圍有助於快速達到專業效果。 對於商業文件,保守的值(BlurRadius: 2-4, DistanceFromText: 1-2)在增加視覺趣味的同時保持可讀性。而創意應用可能會推動這些界限,創造出戲劇性的效果(BlurRadius: 8-10, DistanceFromText: 4-6)以增強視覺衝擊。 請記住,印表機功能和螢幕解析度會影響陰影的顯示方式,因此請在預期的輸出方式中測試您的文件。
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");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")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")
常見問題
如何使用IronWord為文字添加陰影效果?
要使用IronWord添加陰影效果,請建立一個具有ShadowEffect屬性的TextStyle物件。您可以應用預設的陰影如OuterShadow1,或者自訂模糊、距離和顏色等屬性來增強您的Word文件。
在C# Word文件中實現陰影效果的步驟是什麼?
首先,安裝IronWord,然後創建TextStyle並配置ShadowEffect。創建一個具有TextContent的Run,將TextStyle應用到Run,將Run加入到Paragraph中,最後保存文件。
IronWord提供哪些預設陰影效果選項?
IronWord包含一個預設陰影效果,Shadow.OuterShadow1,可以在不需複雜配置的情況下快速實施。對於其他風格,您可以創建自訂陰影配置。
什麼時候應該在IronWord中使用預設陰影效果?
在需要多個文件中保持一致性時,例如公司模板,可以使用預設陰影效果進行標準文件格式化和快速實施。
如何在IronWord中自訂陰影效果?
通過設置Alignment、BlurRadius、DirectionAngle、DistanceFromText、SchemeColor以及縮放和傾斜因子等屬性,可以靈活自訂陰影效果以實現量身定制的視覺效果。
陰影效果能否用於IronWord的品牌塑造?
是的,在IronWord中,通過調整準確的顏色值、定位或模糊效果,自訂陰影效果可以滿足特定的品牌要求,確保品牌規範的遵循。
哪些屬性控制陰影效果的定位?
陰影效果的定位由Alignment、DirectionAngle和DistanceFromText等屬性控制,這些屬性有助於模擬現實的光源。
`BlurRadius`屬性在陰影效果中的重要性是什麼?
BlurRadius屬性決定了陰影邊緣的清晰度,較低的值產生清晰的陰影,而較高的值則提供擴散、柔和的陰影效果。
在IronWord中,陰影縮放和傾斜屬性如何影響文字?
縮放和傾斜屬性,例如HorizontalScalingFactor、VerticalScalingFactor和傾斜角度,允許透視效果,為文字增加維度感和精緻感。
陰影效果屬性的常見值範圍是什麼?
通常,BlurRadius範圍為0-10點,DirectionAngle範圍為0-360度,距離和縮放因子各有不同,確保在不同文件類型中達到專業效果。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。