本文将介绍如何使用Spire.Doc来设置文字效果,包括文本边框,文本填充,文字阴影等。
C#
//实例化一个Document对象
Document doc = new Document();
//向文档中添加一个Section对象
Section sec = doc.AddSection();
//在这个section上添加一个段落并给文字加边框
Paragraph p1 = sec.AddParagraph();
TextRange tr1 = p1.AppendText("加粉色边框的文字");
tr1.CharacterFormat.Border.BorderType = BorderStyle.DashDotStroker;
tr1.CharacterFormat.Border.Color = Color.Pink;
p1.AppendBreak(BreakType.LineBreak);
//添加一个新段落并设置文字填充效果
Paragraph p2 = sec.AddParagraph();
TextRange tr2 = p2.AppendText("设置填充效果的文字");
//设置文字前景色
tr2.CharacterFormat.TextColor = Color.Orange;
//设置文字背景色
tr2.CharacterFormat.TextBackgroundColor = Color.LightGray;
//设置文字缩放比例
tr2.CharacterFormat.TextScale = 150;
p2.AppendBreak(BreakType.LineBreak);
//添加一个新段落并设置阴影
Paragraph p3 = sec.AddParagraph();
TextRange tr3 = p3.AppendText("设置阴影效果的文字");
tr3.CharacterFormat.TextColor = Color.LightSeaGreen;
tr3.CharacterFormat.IsShadow = true;
p3.AppendBreak(BreakType.LineBreak);
//添加一个新段落并设置简单的文字样式
Paragraph p4 = sec.AddParagraph();
TextRange tr4 = p4.AppendText("设置删除线效果的文字");
tr4.CharacterFormat.IsStrikeout = true;
p4.AppendBreak(BreakType.LineBreak);
TextRange tr5 = p4.AppendText("设置文字大写: hello, e-iceblue.");
tr5.CharacterFormat.IsSmallCaps = true;
//使用ClearFormatting()来删除某个TextRange的文字效果
//tr5.CharacterFormat.ClearFormatting();
//保存文档
doc.SaveToFile("文字效果.docx");
VB.NET
'实例化一个Document对象
Dim doc As Document = New Document
'向文档中添加一个Section对象
Dim sec As Section = doc.AddSection
'在这个section上添加一个段落并给文字加边框
Dim p1 As Paragraph = sec.AddParagraph
Dim tr1 As TextRange = p1.AppendText("加粉色边框的文字")
tr1.CharacterFormat.Border.BorderType = BorderStyle.DashDotStroker
tr1.CharacterFormat.Border.Color = Color.Pink
p1.AppendBreak(BreakType.LineBreak)
'添加一个新段落并设置文字填充效果
Dim p2 As Paragraph = sec.AddParagraph
Dim tr2 As TextRange = p2.AppendText("设置填充效果的文字")
'设置文字前景色
tr2.CharacterFormat.TextColor = Color.Orange
'设置文字背景色
tr2.CharacterFormat.TextBackgroundColor = Color.LightGray
'设置文字缩放比例
tr2.CharacterFormat.TextScale = 150
p2.AppendBreak(BreakType.LineBreak)
'添加一个新段落并设置阴影
Dim p3 As Paragraph = sec.AddParagraph
Dim tr3 As TextRange = p3.AppendText("设置阴影效果的文字")
tr3.CharacterFormat.TextColor = Color.LightSeaGreen
tr3.CharacterFormat.IsShadow = true
p3.AppendBreak(BreakType.LineBreak)
'添加一个新段落并设置简单的文字样式
Dim p4 As Paragraph = sec.AddParagraph
Dim tr4 As TextRange = p4.AppendText("设置删除线效果的文字")
tr4.CharacterFormat.IsStrikeout = true
p4.AppendBreak(BreakType.LineBreak)
Dim tr5 As TextRange = p4.AppendText("设置文字大写: hello, e-iceblue.")
tr5.CharacterFormat.IsSmallCaps = true
'使用ClearFormatting()来删除某个TextRange的文字效果
'tr5.CharacterFormat.ClearFormatting();
'保存文档
doc.SaveToFile("文字效果.docx")
效果图如下: