Open XML: Word - Getting all Paragraphs marked as “Heading1” style

夙愿已清 提交于 2020-08-21 05:36:46

问题


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

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

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

回答1:


    [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();
        }
    }


来源:https://stackoverflow.com/questions/11941332/open-xml-word-getting-all-paragraphs-marked-as-heading1-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!