VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > C#教程 >
  • C# 获取Word文本高亮和背景(附vb.net代码)

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

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

 

 

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

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
复制代码

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

文本高亮读取结果:

 



出处:https://www.cnblogs.com/Yesi/p/14544227.html


相关教程