gpt4 book ai didi

如何通过C#/VB.NET代码在Word中更改字体颜色

转载 作者:我是一只小鸟 更新时间:2023-02-17 14:31:09 27 4
gpt4 key购买 nike

在日常工作中,我们有时会需要修改字体的颜色来突出文本重点,让读者更容易抓住文章要点。在今天这篇文章中,我将为大家介绍如何以编程方式,在Word更改字体颜色。本文将分为两部分分别介绍如何实现此操作。以下是我整理的步骤及方法,并附上C#/VB.NET代码供大家参考.

更改段落字体颜色 。

更改特定文本字体颜色 。

程序环境 。

本次测试时,在程序中引入Free Spire.Doc for .NET。可通过以下方法引用 Free Spire.Doc.dll文件:

方法1 :将 Free Spire.Doc for .NET 下载到本地,解压,安装。安装完成后,找到安装路径下BIN文件夹中的 Spire.Doc.dll。然后在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“添加引用”,将本地路径BIN文件夹下的dll文件添加引用至程序.

方法2: 通过 NuGet 安装。可通过以下2种方法安装:

(1)可以在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“管理NuGet包”,然后搜索“Free Spire.Doc”,点击“安装”。等待程序安装完成.

(2)将以下内容复制到PM控制台安装.

Install-Package FreeSpire.Doc -Version 10.8.0 。

更改段落字体颜色

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

  • 创建一个Document实例。
  • 使用 Document.LoadFromFile() 方法加载 Word 文档。
  • 使用 Document.Sections[sectionIndex] 属性获取所需的节。
  • 使用 Section.Paragraphs[paragraphIndex] 属性获取要更改字体颜色的所需段落。
  • 创建一个 ParagraphStyle 实例。
  • 使用 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)
        {
            
                
                
                  //
                
                
                  创建一个Document实例
                
                
            Document document = 
                
                  new
                
                
                   Document();
            
                
                
                  //
                
                
                  Load a Word document
                
                
            document.LoadFromFile(
                
                  "
                
                
                  生死疲劳.docx
                
                
                  "
                
                
                  );

            
                
                
                  //
                
                
                  获取第一节
                
                
            Section section = document.Sections[
                
                  0
                
                
                  ];

            
                
                
                  //
                
                
                  更改第一段文本颜色
                
                
            Paragraph p1 = section.Paragraphs[
                
                  0
                
                
                  ];
            ParagraphStyle s1 
                
                = 
                
                  new
                
                
                   ParagraphStyle(document);
            s1.Name 
                
                = 
                
                  "
                
                
                  Color1
                
                
                  "
                
                
                  ;
            s1.CharacterFormat.TextColor 
                
                =
                
                   Color.Blue;
            document.Styles.Add(s1);
            p1.ApplyStyle(s1.Name);

            
                
                
                  //
                
                
                  更改第二段文本颜色
                
                
            Paragraph p2 = section.Paragraphs[
                
                  1
                
                
                  ];
            ParagraphStyle s2 
                
                = 
                
                  new
                
                
                   ParagraphStyle(document);
            s2.Name 
                
                = 
                
                  "
                
                
                  Color2
                
                
                  "
                
                
                  ;
            s2.CharacterFormat.TextColor 
                
                =
                
                   Color.Green;
            document.Styles.Add(s2);
            p2.ApplyStyle(s2.Name);

            
                
                
                  //
                
                
                  保存结果文档
                
                
            document.SaveToFile(
                
                  "
                
                
                  更改段落字体颜色.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
                
                
                  ())
            
                
                
                  '
                
                
                  创建一个Document实例
                
                
                  Dim
                
                 document 
                
                  As
                
                 Document = 
                
                  New
                
                
                   Document()
            
                
                
                  '
                
                
                  Load a Word document
                
                
            document.LoadFromFile(
                
                  "
                
                
                  生死疲劳.docx
                
                
                  "
                
                
                  )

            
                
                
                  '
                
                
                  获取第一节
                
                
                  Dim
                
                 section 
                
                  As
                
                 Section = document.Sections(
                
                  0
                
                
                  )

            
                
                
                  '
                
                
                  更改第一段文本颜色
                
                
                  Dim
                
                 p1 
                
                  As
                
                 Paragraph = section.Paragraphs(
                
                  0
                
                
                  )
            
                
                
                  Dim
                
                 s1 
                
                  As
                
                 ParagraphStyle = 
                
                  New
                
                
                   ParagraphStyle(document)
            s1.Name 
                
                = 
                
                  "
                
                
                  Color1
                
                
                  "
                
                
                  
            s1.CharacterFormat.TextColor 
                
                =
                
                   Color.Blue
            document.Styles.Add(s1)
            p1.ApplyStyle(s1.Name)

            
                
                
                  '
                
                
                  更改第二段文本颜色
                
                
                  Dim
                
                 p2 
                
                  As
                
                 Paragraph = section.Paragraphs(
                
                  1
                
                
                  )
            
                
                
                  Dim
                
                 s2 
                
                  As
                
                 ParagraphStyle = 
                
                  New
                
                
                   ParagraphStyle(document)
            s2.Name 
                
                = 
                
                  "
                
                
                  Color2
                
                
                  "
                
                
                  
            s2.CharacterFormat.TextColor 
                
                =
                
                   Color.Green
            document.Styles.Add(s2)
            p2.ApplyStyle(s2.Name)

            
                
                
                  '
                
                
                  保存结果文档
                
                
            document.SaveToFile(
                
                  "
                
                
                  更改段落字体颜色.docx
                
                
                  "
                
                
                  , FileFormat.Docx)
        
                
                
                  End Sub
                
                
                  End Class
                
                
                  End Namespace
                
              

效果图

更改特定文本字体颜色

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

  • 创建一个Document实例。
  • 使用 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)
        {
            
                
                
                  //
                
                
                  创建一个Document实例
                
                
            Document document = 
                
                  new
                
                
                   Document();
            
                
                
                  //
                
                
                  加载 Word 文档
                
                
            document.LoadFromFile(
                
                  "
                
                
                  生死疲劳.docx
                
                
                  "
                
                
                  );

            
                
                
                  //
                
                
                  查找指定文本
                
                
            TextSelection[] text = document.FindAllString(
                
                  "
                
                
                  生死疲劳
                
                
                  "
                
                , 
                
                  false
                
                , 
                
                  true
                
                
                  );

            
                
                
                  //
                
                
                  更改特定文本的字体颜色
                
                
                  foreach
                
                 (TextSelection seletion 
                
                  in
                
                
                   text)
            {
                seletion.GetAsOneRange().CharacterFormat.TextColor 
                
                =
                
                   Color.HotPink;
            }

            
                
                
                  //
                
                
                  保存结果文档
                
                
            document.SaveToFile(
                
                  "
                
                
                  更改特定文本字体颜色.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
                
                
                  ())
            
                
                
                  '
                
                
                  创建一个Document实例
                
                
                  Dim
                
                 document 
                
                  As
                
                 Document = 
                
                  New
                
                
                   Document()
            
                
                
                  '
                
                
                  加载 Word 文档
                
                
            document.LoadFromFile(
                
                  "
                
                
                  生死疲劳.docx
                
                
                  "
                
                
                  )

            
                
                
                  '
                
                
                  查找指定文本
                
                
                  Dim
                
                 text 
                
                  As
                
                 TextSelection() = document.FindAllString(
                
                  "
                
                
                  生死疲劳
                
                
                  "
                
                , 
                
                  False
                
                , 
                
                  True
                
                
                  )

            
                
                
                  '
                
                
                  更改特定文本的字体颜色
                
                
                  For
                
                
                  Each
                
                 seletion 
                
                  As
                
                 TextSelection 
                
                  In
                
                
                   text
                seletion.GetAsOneRange().CharacterFormat.TextColor 
                
                =
                
                   Color.HotPink
            
                
                
                  Next
                
                
                  '
                
                
                  保存结果文档
                
                
            document.SaveToFile(
                
                  "
                
                
                  更改特定文本字体颜色.docx
                
                
                  "
                
                
                  , FileFormat.Docx)
        
                
                
                  End Sub
                
                
                  End Class
                
                
                  End Namespace
                
              

效果图

—本文完— 。

最后此篇关于如何通过C#/VB.NET代码在Word中更改字体颜色的文章就讲到这里了,如果你想了解更多关于如何通过C#/VB.NET代码在Word中更改字体颜色的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com