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

第 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);}}}}

结果

TextRanges

完整代码

[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.DocImports Spire.Doc.DocumentsImports Spire.Doc.FieldsImports System.Text.RegularExpressionsNamespace RetrieveStyleNamesClass ProgramPrivate Shared Sub Main(args As String())Dim doc As New Document()doc.LoadFromFile("Sample.docx")For Each section As Section In doc.SectionsFor Each paragraph As Paragraph In section.ParagraphsFor Each docObject As DocumentObject In paragraph.ChildObjectsIf docObject.DocumentObjectType = DocumentObjectType.TextRange ThenDim text As TextRange = TryCast(docObject, TextRange)Console.WriteLine(text.StyleName)End IfNextConsole.WriteLine()NextNextEnd SubEnd ClassEnd Namespace

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


欢迎下载|体验更多E-iceblue产品

获取更多信息请咨询在线客服  


标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年8月4日
下一篇 2022年8月4日

相关推荐

发表回复

登录后才能评论