Word控件Spire.Doc 【页面背景】教程(3) ;如何在 C# 中设置单词段落底纹

发布于:2022-11-28 ⋅ 阅读:(670) ⋅ 点赞:(0)

Word 页面边框是页面背景的一部分,用于美化文档外观。本指南中的解决方案介绍了如何在 C# 和 VB.NET 中插入和格式化 Word 页面边框。

Spire.Doc for.NET 最新下载icon-default.png?t=M85Bhttps://www.evget.com/product/3368/download

通过对word文档中的单词或段落进行阴影处理,我们可以强调信息并轻松吸引他人的注意力。本文将向您展示如何在Spire.Doc的帮助下在 C# 中设置段落底纹。

首先,下载 Spire.Doc并安装在您的系统上。Spire.Doc 安装干净、专业,并包含在 MSI 安装程序中。

然后,通过以下路径在下载的 Bin 文件夹中添加 Spire.Doc.dll 作为参考:“..\Spire.Doc\Bin\NET4.0\ Spire.Doc.dll”。

现在介绍如何设置文本或段落的背景颜色的步骤。

第 1 步:创建一个新的 word 文档并从文件中加载。

[C#]

Document document = new Document();
document.LoadFromFile(@"..\..\Sample.docx");

第 2 步:获取一个段落。

[C#]

Paragraph paragaph = document.Sections[0].Paragraphs[0];

第 3 步:为段落或选定的单词添加阴影。

[C#]

//Set background color for the paragraph
paragaph.Format.BackColor = Color.Yellow;

//Set background color for the selected text of paragraph
paragaph = document.Sections[0].Paragraphs[1];
TextSelection selection= paragaph.Find("Blues",true,false);
TextRange range = selection.GetAsOneRange();
range.CharacterFormat.TextBackgroundColor = Color.Yellow;

Paragraph paragaph = document.Sections[0].Paragraphs[0];

第 4 步:将文档保存到文件中。

[C#]

document.SaveToFile("sample.docx",FileFormat.Docx);

效果截图

完整代码

[C#]

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace SetWordParagh
{
class Program
{
static void Main(string[] args)
{
Document document = new Document();
document.LoadFromFile(@"..\..\Sample.docx");
Paragraph paragaph = document.Sections[0].Paragraphs[0];

//Set background color for the paragraph
paragaph.Format.BackColor = Color.Yellow;

//Set background color for the selected text of paragraph
paragaph = document.Sections[0].Paragraphs[1];
TextSelection selection= paragaph.Find("Blues",true,false);
TextRange range = selection.GetAsOneRange();
range.CharacterFormat.TextBackgroundColor = Color.Yellow;

document.SaveToFile("sample.docx",FileFormat.Docx);
}
}
}

 

本文含有隐藏内容,请 开通VIP 后查看