Spire.Doc 教程:在C#,VB.NET的Word中检索所有TextRanges样式名称

Step 1: 创建一个文档实例。

Document doc = new Document();

Step 2: 加载一个示例Word文件。

doc.LoadFromFile("Sample.docx");

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

结果:

图片1

完整代码:

[C#]

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]

Document doc = New Document()doc.LoadFromFile("Sample.docx")Dim section As SectionFor Each section In doc.Sections    Dim paragraph As Paragraph    For Each paragraph In section.Paragraphs        Dim docObject As DocumentObject        For Each docObject In paragraph.ChildObjects            If docObject.DocumentObjectType = DocumentObjectType.TextRange Then                Dim text As TextRange = docObject as TextRange                Console.WriteLine(text.StyleName)            End If        Next        Console.WriteLine()    NextNext

标签:

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

上一篇 2017年11月24日
下一篇 2017年11月24日

相关推荐

发表回复

登录后才能评论