如何在 C# 中为文本添加倒影效果
使用 IronWord 的 API,可以在 C# 中为文本添加镜面倒影。只需一行代码即可模拟文字在表面上的倒影,增强视觉层次感。
快速入门:在 C# 中为文本添加倒影效果使用 IronWord,只需一行代码即可为任意文本应用预设倒影效果,无需复杂配置或样板代码。
-
1使用 IronWord 安装 NuGet Package Manager
-
2复制并运行这段代码。
using IronWord; using IronWord.Models; WordDocument doc = new WordDocument(); TextStyle textStyle = new TextStyle(); textStyle.TextEffect = new TextEffect() { ReflectionEffect = new Reflection() }; Paragraph paragraph = new Paragraph(); Run textRun = new Run(new TextContent("Reflection Text")); textRun.Style = textStyle; paragraph.AddChild(textRun); doc.AddParagraph(paragraph); doc.SaveAs("reflection.docx");Imports IronWord Imports IronWord.Models Dim doc As New WordDocument() Dim textStyle As New TextStyle() textStyle.TextEffect = New TextEffect() With {.ReflectionEffect = New Reflection()} Dim paragraph As New Paragraph() Dim textRun As New Run(New TextContent("Reflection Text")) textRun.Style = textStyle paragraph.AddChild(textRun) doc.AddParagraph(paragraph) doc.SaveAs("reflection.docx")Imports IronWord Imports IronWord.Models Dim doc As New WordDocument() Dim textStyle As New TextStyle() textStyle.TextEffect = New TextEffect() With {.ReflectionEffect = New Reflection()} Dim paragraph As New Paragraph() Dim textRun As New Run(New TextContent("Reflection Text")) textRun.Style = textStyle paragraph.AddChild(textRun) doc.AddParagraph(paragraph) doc.SaveAs("reflection.docx") -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronWord
如何为文本添加倒影效果(5 个步骤)
- 安装IronWord:
Install-Package IronWord - 创建
TextStyle并使用new Reflection()配置ReflectionEffect - 创建一个包含
TextContent的Run,并将TextStyle分配给该Run - 使用
AddChild将Run添加到Paragraph - 保存文档
如何添加倒影效果?
要应用倒影效果,先创建一个 TextStyle,并在其 TextEffect 属性中设置 ReflectionEffect。然后创建一个 Paragraph,再创建一个包含 TextContent 的 Run。将 TextStyle 赋给 Run(而非 TextContent),再使用 AddChild 将 Run 添加到 Paragraph。文档层次结构为 Document → Paragraph → Run → TextContent。
倒影效果可为重要文本增添层次感和视觉变化,尤其适合各级标题以及专业文档中需要突出的关键信息。该效果模拟文字在光滑表面上的倒影,使呈现更现代,也更容易吸引读者的注意。
为什么创建一个TextStyle很重要?
在 IronWord 中,所有文本格式都可通过 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()
{
ReflectionEffect = new Reflection(),
};
// 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("reflectionEffect.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()
{
ReflectionEffect = new Reflection(),
};
// 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("reflectionEffect.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 {
.ReflectionEffect = New Reflection()
}
' 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("reflectionEffect.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 {
.ReflectionEffect = New Reflection()
}
' 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("reflectionEffect.docx")默认倒影效果是什么样的?
默认效果会在文本下方生成淡淡的镜像,不透明度自动逐渐降低。该预设适用于大多数商务文档和演示文稿,无需额外配置。倒影与文本之间采用默认间距,模糊度和透明度设置适合多种字号和字体样式。

倒影效果有哪些可配置的属性?
倒影效果提供多项可调属性,可根据文档风格和设计需求创建不同的视觉效果。这些属性分别控制倒影的位置、角度、透明度和颜色等。以下列表详细说明各项属性。
哪些属性控制倒影外观?
SchemeColor:获取或设置倒影的配色方案颜色。可以为倒影着色,以模拟水面或金属表面的反光效果。HorizontalSkewAngle:获取或设置水平倾斜角度,以度为单位。可使倒影看起来向远处延伸,营造透视效果。HorizontalScalingFactor:获取或设置水平缩放因子。小于 100 时压缩倒影,大于 100 时拉伸倒影。DistanceFromText:获取或设置倒影与文本之间的距离,以点(1/72 英寸)为单位。较小的值使倒影贴近文本,较大的值可模拟较远表面上的倒影。DirectionAngle:获取或设置方向角,以度为单位。此值决定倒影呈现出的光源方向。FadeDirectionAngle:获取或设置淡出方向,以度为单位。可让地面上的倒影沿垂直方向淡出,或让水面上的倒影沿斜向淡出。EndPosition:获取或设置结束位置。 确定倒影完全消失的位置。StartPosition:获取或设置起始位置。通常设为 0,使倒影紧接在文本下方。EndingOpacity:获取或设置结束不透明度。值越低,倒影末端越接近透明,效果越柔和。VerticalScalingFactor:获取或设置垂直缩放因子。负值会翻转文本,绝对值决定倒影高度。StartingOpacity:获取或设置起始不透明度。值越高,倒影起始处越明显。Alignment:获取或设置对齐方式。 从各种选项中选择相对于文本的倒影位置。BlurRadius:获取或设置模糊半径,以点(1/72 英寸)为单位。值越大,倒影越柔和、边缘越模糊。VerticalSkewAngle:获取或设置垂直倾斜角度,以度为单位。可用于创建倾斜的倒影。
如何创建自定义倒影效果?
通过自定义倒影效果,可以让文档符合组织的品牌风格,或营造独特的视觉效果。以下示例使用特定的位置和不透明度设置创建金色倒影,适合证书、奖状或精美文档的标题。
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()
{
ReflectionEffect = new Reflection()
{
Alignment = RectangleAlignmentValues.BottomLeft,
BlurRadius = 5,
DirectionAngle = 90,
DistanceFromText = 5,
EndingOpacity = 100,
EndPosition = 10,
FadeDirectionAngle = 90,
HorizontalScalingFactor = 100,
HorizontalSkewAngle = 0,
SchemeColor = IronWord.Models.Color.Gold,
StartingOpacity = 0,
StartPosition = 0,
VerticalScalingFactor = -100,
VerticalSkewAngle = 0,
},
};
// Create paragraph
Paragraph paragraph = new Paragraph();
// Create run with text and style
Run textRun = new Run(new TextContent("Customized reflection"));
textRun.Style = textStyle;
// Add run to paragraph
paragraph.AddChild(textRun);
// Add paragraph to document
doc.AddParagraph(paragraph);
// Export new Word document
doc.SaveAs("customizedReflectionEffect.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()
{
ReflectionEffect = new Reflection()
{
Alignment = RectangleAlignmentValues.BottomLeft,
BlurRadius = 5,
DirectionAngle = 90,
DistanceFromText = 5,
EndingOpacity = 100,
EndPosition = 10,
FadeDirectionAngle = 90,
HorizontalScalingFactor = 100,
HorizontalSkewAngle = 0,
SchemeColor = IronWord.Models.Color.Gold,
StartingOpacity = 0,
StartPosition = 0,
VerticalScalingFactor = -100,
VerticalSkewAngle = 0,
},
};
// Create paragraph
Paragraph paragraph = new Paragraph();
// Create run with text and style
Run textRun = new Run(new TextContent("Customized reflection"));
textRun.Style = textStyle;
// Add run to paragraph
paragraph.AddChild(textRun);
// Add paragraph to document
doc.AddParagraph(paragraph);
// Export new Word document
doc.SaveAs("customizedReflectionEffect.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 {
.ReflectionEffect = New Reflection() With {
.Alignment = RectangleAlignmentValues.BottomLeft,
.BlurRadius = 5,
.DirectionAngle = 90,
.DistanceFromText = 5,
.EndingOpacity = 100,
.EndPosition = 10,
.FadeDirectionAngle = 90,
.HorizontalScalingFactor = 100,
.HorizontalSkewAngle = 0,
.SchemeColor = IronWord.Models.Color.Gold,
.StartingOpacity = 0,
.StartPosition = 0,
.VerticalScalingFactor = -100,
.VerticalSkewAngle = 0
}
}
' Create paragraph
Dim paragraph As New Paragraph()
' Create run with text and style
Dim textRun As New Run(New TextContent("Customized reflection"))
textRun.Style = textStyle
' Add run to paragraph
paragraph.AddChild(textRun)
' Add paragraph to document
doc.AddParagraph(paragraph)
' Export new Word document
doc.SaveAs("customizedReflectionEffect.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 {
.ReflectionEffect = New Reflection() With {
.Alignment = RectangleAlignmentValues.BottomLeft,
.BlurRadius = 5,
.DirectionAngle = 90,
.DistanceFromText = 5,
.EndingOpacity = 100,
.EndPosition = 10,
.FadeDirectionAngle = 90,
.HorizontalScalingFactor = 100,
.HorizontalSkewAngle = 0,
.SchemeColor = IronWord.Models.Color.Gold,
.StartingOpacity = 0,
.StartPosition = 0,
.VerticalScalingFactor = -100,
.VerticalSkewAngle = 0
}
}
' Create paragraph
Dim paragraph As New Paragraph()
' Create run with text and style
Dim textRun As New Run(New TextContent("Customized reflection"))
textRun.Style = textStyle
' Add run to paragraph
paragraph.AddChild(textRun)
' Add paragraph to document
doc.AddParagraph(paragraph)
' Export new Word document
doc.SaveAs("customizedReflectionEffect.docx")使用自定义属性可以达到什么效果?
上面的示例通过金色倒影提升文档的质感。将不透明度从 0% 渐变到 100% 可形成反向淡出效果,使倒影离文本越远越明显。此方法适合制作醒目的标题或突出重要公告。

倒影效果的最佳实践
为专业文档添加倒影效果时,请参考以下做法。
优先使用柔和的效果:在商务文档中使用较低的结束不透明度(20% 至 40%),让倒影衬托正文,避免分散注意力。营销材料或演示文稿需要突出视觉效果时,可以使用更明显的倒影。
与文档风格协调:根据文档的整体设计调整倒影属性。正式文档适合使用模糊度较低的垂直倒影,创意材料则可通过倾斜或着色营造艺术效果。
性能考虑:模糊半径较大的复杂倒影效果可能增大文件体积并延长处理时间。如果文档包含大量带倒影的元素,请测试性能并调整属性。
关注无障碍:倒影等装饰效果应有助于清晰传达信息。确保主要文本易于阅读,尤其是在文档需要符合无障碍标准时。
常见问题
如何在C# Word文档中为文本添加倒影效果?
您可以通过使用IronWord创建一个TextStyle并设置其TextEffect属性为ReflectionEffect,在C# Word文档中应用倒影效果。然后,将此样式添加到包含TextContent的Run中,并将其包含在文档中。
倒影效果在Word文档中有哪些优势?
倒影效果通过增加深度和现代风格提升Word文档的视觉吸引力。它们在通过模拟光滑表面上的文本外观来突出标题、题目或重要信息时特别有用。
在IronWord中处理文本效果时,TextStyle对象为什么重要?
TextStyle对象允许您在多个文本元素中一致地管理和应用文本格式。这个中央配置点确保了统一性,并使得在IronWord中样式的全局更新更为高效。
IronWord中的默认倒影效果是什么样的?
IronWord中默认的倒影效果在其下方创建文本的微妙镜像,并具有自动渐隐效果,能够适应各种字体大小和样式,非常适合商务文档和演示。
在IronWord中可以为倒影效果配置哪些属性?
您可以在IronWord中为倒影效果配置多个属性,包括颜色、缩放、距离、透明度和倾斜角度。每个属性都允许您根据文档的设计需要定制倒影。
如何在IronWord中创建自定义倒影效果?
要在IronWord中创建自定义倒影效果,请在ReflectionEffect配置中修改属性,如SchemeColor、HorizontalScalingFactor和VerticalSkewAngle。这使您能够实现独特的视觉风格,如金色或倾斜的倒影。
在专业文档中使用倒影效果的一些最佳实践是什么?
对于专业文档,请使用低透明度和最小模糊的微妙倒影效果。确保样式与文档的整体设计保持一致,并在使用复杂效果时考虑性能,以避免增加文件大小和处理时间。
在IronWord中自定义倒影效果会影响到文档的性能吗?
会的,复杂度非常高的倒影效果,尤其是带有高模糊半径设置的效果,可能会增加文件大小和处理时间。建议测试性能并调整这些属性,尤其是在文档中包含大量倒影元素时。
如何确保Word文档中的倒影效果符合可访问性标准?
倒影效果应用于增强而不是替换可读文本。确保主要内容保持清晰和可读,并始终检查可访问性标准,特别是在创建面向公众或需要符合规定的文档时。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。