/// <summary> /// Gets the <see cref="RunPropertiesBaseStyle" /> ancestor of the document's styles element. /// </summary> /// <param name="document">The document</param> /// <returns>The <see cref="RunPropertiesBaseStyle" /></returns> public static RunPropertiesBaseStyle GetRunPropertiesBaseStyle(this WordprocessingDocument document) { if (document == null) { throw new ArgumentNullException("document"); } var docDefaults = document.ProduceStylesElement().DocDefaults; return(docDefaults.RunPropertiesDefault.RunPropertiesBaseStyle); }
/// <summary> /// Gets the paragraph <see cref="Style" /> with the given id. /// </summary> /// <param name="document">The document</param> /// <param name="styleId">The style's id</param> /// <returns>The corresponding style</returns> public static Style GetParagraphStyle(this WordprocessingDocument document, string styleId) { if (document == null) { throw new ArgumentNullException("document"); } if (styleId == null) { throw new ArgumentNullException("styleId"); } var styles = document.ProduceStylesElement(); return(styles.Elements <Style>().FirstOrDefault <Style>( style => style.StyleId == styleId && style.Type == StyleValues.Paragraph)); }
/// <summary> /// Creates a new paragraph style with the specified style ID, primary /// style name, and aliases and add it to the specified style definitions /// part. Saves the data in the DOM tree back to the part. /// </summary> /// <param name="document">The document</param> /// <param name="styleId">The style's unique ID</param> /// <param name="styleName">The style's name</param> /// <param name="basedOn">The base style</param> /// <param name="nextStyle">The next paragraph's style</param> /// <returns>The newly created style</returns> public static Style CreateParagraphStyle(this WordprocessingDocument document, string styleId, string styleName, string basedOn, string nextStyle) { // Check parameters if (document == null) { throw new ArgumentNullException("document"); } if (styleId == null) { throw new ArgumentNullException("styleId"); } if (styleName == null) { throw new ArgumentNullException("styleName"); } if (basedOn == null) { throw new ArgumentNullException("basedOn"); } if (nextStyle == null) { throw new ArgumentNullException("nextStyle"); } // Check whether the style already exists. var style = document.GetParagraphStyle(styleId); if (style != null) { throw new ArgumentException("Style '" + styleId + "' already exists!", styleId); } // Create a new paragraph style element and specify key attributes. style = new Style { Type = StyleValues.Paragraph, CustomStyle = true, StyleId = styleId }; // Add key child elements style.Produce <StyleName>().Val = styleName; style.Produce <BasedOn>().Val = basedOn; style.Produce <NextParagraphStyle>().Val = nextStyle; // Add the style to the styles part return(document.ProduceStylesElement().AppendChild(style)); }