• Word控件Spire.Doc 【文本】教程(20) ;如何在 C#、VB.NET 下检索 Word 中所有 TextRanges 的样式名称


    Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。下面我们将给您介绍如何在 C#、VB.NET 下检索 Word 中所有 TextRanges 的样式名称

    Spire.Doc for.NET 最新下载(qun:767755948)https://www.evget.com/product/3368/download

    程序员可能需要确定一段文本的样式名称,或者在文档中查找以指定样式名称出现的文本,例如“标题 1”。本文将向您展示如何使用 Spire.Doc 与 C# 和 VB.NET 检索应用于 Word 文档的样式名称。

    第 1 步:创建一个 Document 实例。

    Document doc = new Document();

    第 2 步:加载示例 Word 文件。

    doc.LoadFromFile("Sample.docx");

    第 3 步:遍历文档中的所有TextRanges,通过StyleName属性获取它们的样式名称。

    foreach (Section section in doc.Sections)
    {
    foreach (Paragraph paragraph in section.Paragraphs)
    {
    foreach (DocumentObject docObject in paragraph.ChildObjects)
    {
    if (docObject.DocumentObjectType == DocumentObjectType.TextRange)
    {
    TextRange text = docObject as TextRange;
    Console.WriteLine(text.StyleName);
    }
    }
    }
    }

    结果

    完整代码

    [C#]

    using Spire.Doc;
    using Spire.Doc.Documents;
    using Spire.Doc.Fields;
    using System;
    using System.Text.RegularExpressions;
    namespace RetrieveStyleNames
    {
    class Program
    {
    static void Main(string[] args)
    {
    Document doc = new Document();
    doc.LoadFromFile("Sample.docx");
    
    foreach (Section section in doc.Sections)
    {
    foreach (Paragraph paragraph in section.Paragraphs)
    {
    foreach (DocumentObject docObject in paragraph.ChildObjects)
    {
    if (docObject.DocumentObjectType == DocumentObjectType.TextRange)
    {
    TextRange text = docObject as TextRange;
    Console.WriteLine(text.StyleName);
    }
    }
    Console.WriteLine();
    }
    }
    }
    }
    }
    

    [VB.NET]

    Imports Spire.Doc
    Imports Spire.Doc.Documents
    Imports Spire.Doc.Fields
    Imports System.Text.RegularExpressions
    Namespace RetrieveStyleNames
    Class Program
    Private Shared Sub Main(args As String())
    Dim doc As New Document()
    doc.LoadFromFile("Sample.docx")
    
    For Each section As Section In doc.Sections
    For Each paragraph As Paragraph In section.Paragraphs
    For Each docObject As DocumentObject In paragraph.ChildObjects
    If docObject.DocumentObjectType = DocumentObjectType.TextRange Then
    Dim text As TextRange = TryCast(docObject, TextRange)
    Console.WriteLine(text.StyleName)
    End If
    Next
    Console.WriteLine()
    Next
    Next
    End Sub
    End Class
    End Namespace

    以上便是如何在 C#、VB.NET 下检索 Word 中所有 TextRanges 的样式名称,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。

  • 相关阅读:
    HFSS中激励方式学习笔记(总)
    肝了这篇文章,我对服务器硬件有了深刻的认识!
    PX4模块设计之十五:PX4 Log设计
    MSF生成后门木马
    Go 企业级框架 Beego 版全新发布
    老卫带你学---leetcode刷题(438. 找到字符串中所有字母异位词)
    Hadoop:模板虚拟机的创建
    学生花卉网网页设计作品 学生鲜花网页模板 简单在线花店主页成品 鲜花网页制作 HTML学生花店商城网站作业设计
    机器人运动学、动力学与控制及Matlab实现
    黑盒测试用例设计 - 等价类划分法
  • 原文地址:https://blog.csdn.net/m0_67129275/article/details/127881467