private int?TryFindNumberingIdUsingStyleId(string styleId) { if (Styles == null) { return(null); } while (styleId != null) { var style = Styles .Elements <DocumentFormat.OpenXml.Wordprocessing.Style>() .SingleOrDefault(x => x.StyleId?.Value == styleId); int?result = ExtractNumberingId(style); if (result.HasValue) { return(result); } styleId = style .Descendants <BasedOn>() .SingleOrDefault()?.Val?.Value; } return(null); }
public static bool IsStyleIdInDocument(MainDocumentPart mainPart, string styleid) { // Get access to the Styles element for this document. DocumentFormat.OpenXml.Wordprocessing.Styles s = mainPart.StyleDefinitionsPart.Styles; // Check that there are styles and how many. int n = s.Elements <DocumentFormat.OpenXml.Wordprocessing.Style>().Count(); if (n == 0) { return(false); } // Look for a match on styleid. DocumentFormat.OpenXml.Wordprocessing.Style style = s.Elements <DocumentFormat.OpenXml.Wordprocessing.Style>() .Where(st => (st.StyleId == styleid) && (st.Type == StyleValues.Paragraph)) .FirstOrDefault(); if (style == null) { return(false); } return(true); }
/// <summary> /// Returns the document style specified by the styleId /// </summary> /// <param name="styleId"></param> /// <returns></returns> public Style DocumentStyle(string styleId) { var element = styles.Elements <DocumentFormat.OpenXml.Wordprocessing.Style>().Where(p => p.StyleId == styleId).FirstOrDefault(); if (element == null) { element = new DocumentFormat.OpenXml.Wordprocessing.Style() { StyleId = styleId, Type = StyleValues.Paragraph, StyleName = new StyleName() { Val = styleId } }; styles.AppendChild(element); } return(new Style(element)); }