打开 XML:Word - 将所有段落标记为“标题 1";风格 [英] Open XML: Word - Getting all Paragraphs marked as "Heading1" style

查看:111
本文介绍了打开 XML:Word - 将所有段落标记为“标题 1";风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Word 创建了一个带有标准 normal.dot 的 Docx 作为测试.Hello-world 级别的复杂性.

Using Word I have created a Docx with the standard normal.dot as a test. Hello-world level complexity.

我希望获得在 Word 中使用Heading1"style 设置样式的所有段落.

I wish to get all the paragraphs which are styled with the "Heading1" style in Word.

我可以得到所有段落,但不知道如何过滤到 Heading1.

I can get all the paragraphs, but don't know how to filter down to Heading1.

using (var doc = WordprocessingDocument.Open(documentFileName, false))
{
    paragraphs = doc.MainDocumentPart.Document.Body
                    .OfType<Paragraph>().ToList();
}

推荐答案

    [Test]
    public void FindHeadingParagraphs()
    {

        var paragraphs = new List<Paragraph>();

        // Open the file read-only since we don't need to change it.
        using (var wordprocessingDocument = WordprocessingDocument.Open(documentFileName, false))
        {
            paragraphs = wordprocessingDocument.MainDocumentPart.Document.Body
                .OfType<Paragraph>()
                .Where(p => p.ParagraphProperties != null && 
                            p.ParagraphProperties.ParagraphStyleId != null && 
                            p.ParagraphProperties.ParagraphStyleId.Val.Value.Contains("Heading1")).ToList();
        }
    }

这篇关于打开 XML:Word - 将所有段落标记为“标题 1";风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆