Spire.Doc支持获取Word文档中段落(Paragraph)和文本范围(TextRange)的样式,例如标题(Title)、标题1(Heading 1)、副标题(Subtitle)等。当然,我们也可以根据标题样式获取对应的文本。

Word段落样式名称 Spire.Doc中对应的样式名称
Title Title
Heading 1 Heading1
Heading 2 Heading2
Heading 3 Heading3
Heading 4 Heading3
Subtitle Subtitle

本文将展示如何从以下文档中获取2级标题对应的文本。

【教程】Spire.Doc系列教程(8):C# 根据 Word 的标题样式获取文字

代码段:

//创建Document对象
Document doc = new Document();

//加载Word文档
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx");

//遍历章节
foreach (Section section in doc.Sections)
{
    //遍历段落
    foreach (Paragraph paragraph in section.Paragraphs)
    {
        //判断段落样式是否为Heading 2
        if (paragraph.StyleName == "Heading2")
        {
            //输出标题2对应的文本
            System.Console.WriteLine(paragraph.Text);
        }
    }
}

结果:

【教程】Spire.Doc系列教程(8):C# 根据 Word 的标题样式获取文字

                                                                      【下载Spire.Doc最新试用版

相关文章:

  • 2021-08-08
  • 2021-09-29
  • 2021-07-19
  • 2021-08-02
  • 2021-10-11
  • 2022-12-23
  • 2021-12-11
  • 2022-12-23
猜你喜欢
  • 2021-06-06
  • 2021-05-04
  • 2021-03-31
  • 2021-05-29
  • 2022-01-20
  • 2022-01-21
  • 2021-07-31
相关资源
相似解决方案