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

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

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 Section
For 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()
    Next
Next
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值