打开XML:Word - 将所有Paragraph标记为"Heading1"样式

Ian*_*ink 5 ms-word openxml openxml-sdk

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

我希望得到all the paragraphs哪些用Word中的" Heading1"字样style.

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

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

Ian*_*ink 9

    [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();
        }
    }
Run Code Online (Sandbox Code Playgroud)