Word控件Spire.Doc 【字体】教程(1):在 Word 中更改字体颜色

Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。 

E-iceblue 功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式

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

如果要强调Word文档中的特定段落或文本,可以更改其字体颜色。本文将演示如何在 C# 中更改 Word 中的字体颜色,并使用 Spire.Doc for .NET 库 VB.NET。

  • 更改段落的字体颜色
  • 更改特定文本的字体颜色

安装 Spire.Doc for .NET

首先,您需要将 Spire.Doc for .NET 包中包含的 DLL 文件添加为 .NET 项目中的引用。DLL 文件可以通过 NuGet 安装。

PM> Install-Package Spire.Doc

在 C# 和 VB.NET 中更改段落的字体颜色

以下是更改 Word 文档中段落的字体颜色的步骤:

创建文档实例。

  • 使用 Document.LoadFromFile() 方法加载 Word 文档。
  • 使用 Document.Sections [sectionIndex] 属性获取所需的节。
  • 获取要使用 Section.Paragraphs[paragraphIndex] 属性更改字体颜色的所需段落。
  • 创建一个段落样式实例。
  • 使用 ParagraphStyle.Name 和 ParagraphStyle.CharacterFormat.TextColor 属性设置样式名称和字体颜色。
  • 使用 Document.Styles.Add() 方法将样式添加到文档中。
  • 使用 Paragraph.ApplyStyle() 方法将样式应用于段落。
  • 使用 Document.SaveToFile() 方法保存结果文档。

【C#】

using Spire.Doc;

using Spire.Doc.Documents;
using System.Drawing;

namespace ChangeFontColorForParagraph
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new Document();
//Load a Word document
document.LoadFromFile("Sample.docx");

//Get the first section
Section section = document.Sections[0];

//Change text color of the first Paragraph
Paragraph p1 = section.Paragraphs[0];
ParagraphStyle s1 = new ParagraphStyle(document);
s1.Name = "Color1";
s1.CharacterFormat.TextColor = Color.RosyBrown;
document.Styles.Add(s1);
p1.ApplyStyle(s1.Name);

//Change text color of the second Paragraph
Paragraph p2 = section.Paragraphs[1];
ParagraphStyle s2 = new ParagraphStyle(document);
s2.Name = "Color2";
s2.CharacterFormat.TextColor = Color.DarkBlue;
document.Styles.Add(s2);
p2.ApplyStyle(s2.Name);

//Save the result document
document.SaveToFile("ChangeParagraphTextColor.docx", FileFormat.Docx);
}
}
}

【VB.NET】

Imports Spire.Doc
Imports Spire.Doc.Documents
Imports System.Drawing

Namespace ChangeFontColorForParagraph
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a Document instance
Dim document As Document = New Document()
'Load a Word document
document.LoadFromFile("Sample.docx")

'Get the first section
Dim section As Section = document.Sections(0)

'Change text color of the first Paragraph
Dim p1 As Paragraph = section.Paragraphs(0)
Dim s1 As ParagraphStyle = New ParagraphStyle(document)
s1.Name = "Color1"
s1.CharacterFormat.TextColor = Color.RosyBrown
document.Styles.Add(s1)
p1.ApplyStyle(s1.Name)

'Change text color of the second Paragraph
Dim p2 As Paragraph = section.Paragraphs(1)
Dim s2 As ParagraphStyle = New ParagraphStyle(document)
s2.Name = "Color2"
s2.CharacterFormat.TextColor = Color.DarkBlue
document.Styles.Add(s2)
p2.ApplyStyle(s2.Name)

'Save the result document
document.SaveToFile("ChangeParagraphTextColor.docx", FileFormat.Docx)
End Sub
End Class
End Namespace

在 C# 和 VB.NET 中更改特定文本的字体颜色

以下是更改 Word 文档中特定文本的字体颜色的步骤:

创建文档实例。

  • 使用 Document.LoadFromFile() 方法加载 Word 文档。
  • 找到要使用 Document.FindAllString() 方法更改字体颜色的文本。
  • 循环遍历搜索文本的所有匹配项,并使用 TextSelection.GetAsOneRange() 更改每次出现的字体颜色。CharacterFormat.TextColor 属性。
  • 使用 Document.SaveToFile() 方法保存结果文档。

【C#】

using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;

namespace ChangeFontColorForText
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new Document();
//Load a Word document
document.LoadFromFile("Sample.docx");

//Find the text that you want to change font color for
TextSelection[] text = document.FindAllString("Spire.Doc for .NET", false, true);
//Change the font color for the searched text
foreach (TextSelection seletion in text)
{
seletion.GetAsOneRange().CharacterFormat.TextColor = Color.Red;
}

//Save the result document
document.SaveToFile("ChangeCertainTextColor.docx", FileFormat.Docx);
}
}
}

【VB.NET】

Imports Spire.Doc
Imports Spire.Doc.Documents
Imports System.Drawing

Namespace ChangeFontColorForText
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a Document instance
Dim document As Document = New Document()
'Load a Word document
document.LoadFromFile("Sample.docx")

'Find the text that you want to change font color for
Dim text As TextSelection() = document.FindAllString("Spire.Doc for .NET", False, True)
'Change the font color for the searched text
For Each seletion As TextSelection In text
seletion.GetAsOneRange().CharacterFormat.TextColor = Color.Red
Next

'Save the result document
document.SaveToFile("ChangeCertainTextColor.docx", FileFormat.Docx)
End Sub
End Class
End Namespace

以上便是在 Word 中更改字体颜色的教程,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。

猜你喜欢

转载自blog.csdn.net/m0_67129275/article/details/130380158
今日推荐