C# 获取Word文本高亮和背景(附vb.net代码)

  • A+
所属分类:.NET技术
摘要

Word中的文本高亮和背景是通过不同方法来设置的。文本高亮(Text Highlight Color)是通过【字体】中的快速工具栏设置;文本背景(Text Background/Shading)是通过【设计】-【页面边框】-【底纹】来设置。因此,在读取文档中的文本高亮或背景时需要采用不同方法。下面通过调用Spire.doc.dll文件 提供的方法来分别获取。

Word中的文本高亮和背景是通过不同方法来设置的。文本高亮(Text Highlight Color)是通过【字体】中的快速工具栏设置;文本背景(Text Background/Shading)是通过【设计】-【页面边框】-【底纹】来设置。因此,在读取文档中的文本高亮或背景时需要采用不同方法。下面通过调用Spire.doc.dll文件提供的方法来分别获取。

需在【解决方案资源管理器】中引用以下必要程序集文件:

C# 获取Word文本高亮和背景(附vb.net代码)

 

 

程序中用于测试的Word文档如图:

C# 获取Word文本高亮和背景(附vb.net代码)

C#

using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System; using System.Drawing;  namespace GetTextBackground {     class Program     {         static void Main(string[] args)         {             //加载Word文档             Document doc = new Document();             doc.LoadFromFile("test.docx");              //获取section             Section section = doc.Sections[0];              //遍历所有段落             for (int i = 0; i < section.Paragraphs.Count;i++ )             {                 Paragraph paragraph = section.Paragraphs[i];                  //遍历段落中的所有对象                 for (int j = 0; j < paragraph.ChildObjects.Count;j++ )                 {                     object obj = paragraph.ChildObjects[j];                     if (obj is TextRange)                     {                         string text = ((TextRange)obj).Text;//获取文本                         //Color color = ((TextRange)obj).CharacterFormat.HighlightColor;//获取文本的高亮颜色(即突出显示颜色)                         Color color = ((TextRange) obj).CharacterFormat.TextBackgroundColor;//获取文字背景色(底纹)                         if (!(color.IsEmpty))                         {                             //获取文本和颜色                             Console.WriteLine("文本内容:"+ text+                                 "n"+                                 "颜色:"+ color);                             Console.ReadLine();                                                                                                          }                     }                  }              }         }     } }

VB.NET

Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Imports System.Drawing  Namespace GetTextBackground     Class Program         Private Shared Sub Main(args As String())             '加载Word文档             Dim doc As New Document()             doc.LoadFromFile("test.docx")              '获取section             Dim section As Section = doc.Sections(0)              '遍历所有段落             For i As Integer = 0 To section.Paragraphs.Count - 1                 Dim paragraph As Paragraph = section.Paragraphs(i)                  '遍历段落中的所有对象                 For j As Integer = 0 To paragraph.ChildObjects.Count - 1                     Dim obj As Object = paragraph.ChildObjects(j)                     If TypeOf obj Is TextRange Then                         Dim text As String = DirectCast(obj, TextRange).Text                         '获取文本                         'Color color = ((TextRange)obj).CharacterFormat.HighlightColor;//获取文本的高亮颜色(即突出显示颜色)                         Dim color As Color = DirectCast(obj, TextRange).CharacterFormat.TextBackgroundColor                         '获取文字背景色(底纹)                         If Not (color.IsEmpty) Then                             '获取文本和颜色                             Console.WriteLine((Convert.ToString("文本内容:") & text) + vbLf + "颜色:" + color)                              Console.ReadLine()                         End If                      End If                  Next             Next         End Sub     End Class End Namespace

文本背景(底纹)读取结果:

C# 获取Word文本高亮和背景(附vb.net代码)

文本高亮读取结果:

C# 获取Word文本高亮和背景(附vb.net代码)

 

 

 

(如需转载,请务必注明出处!!)