zoukankan      html  css  js  c++  java
  • 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+
                                    "
    "+
                                    "颜色:"+ 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

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

    文本高亮读取结果:

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

  • 相关阅读:
    android开发中调用python代码(带参数)
    安卓开发中实现自动点击功能、获取网络信息’-博客新人初来乍到,欢迎大佬多多指教。
    一文读懂 Spring Boot、微服务架构和大数据治理三者之间的故事
    EditText搜索关键字,返回结果匹配关键字改变颜色
    Android studio无法创建类和接口问题解决办法。提示 Unable to parse template "Class"
    我的主页
    博客园美化-coffee
    apple面容、指纹验证使用
    iOS数据库FMDB操作
    UIBezierPath绘图基础教程
  • 原文地址:https://www.cnblogs.com/Yesi/p/14544227.html
Copyright © 2011-2022 走看看