Export (0) Print
Expand All

Style class

Style Definition. When the object is serialized out as xml, its qualified name is w:style.

Namespace:  DocumentFormat.OpenXml.Wordprocessing
Assembly:  DocumentFormat.OpenXml (in DocumentFormat.OpenXml.dll)

[ChildElementInfoAttribute(typeof(PrimaryStyle))]
[ChildElementInfoAttribute(typeof(PersonalCompose))]
[ChildElementInfoAttribute(typeof(PersonalReply))]
[ChildElementInfoAttribute(typeof(TableStyleConditionalFormattingTableRowProperties))]
[ChildElementInfoAttribute(typeof(StyleName))]
[ChildElementInfoAttribute(typeof(Aliases))]
[ChildElementInfoAttribute(typeof(BasedOn))]
[ChildElementInfoAttribute(typeof(NextParagraphStyle))]
[ChildElementInfoAttribute(typeof(LinkedStyle))]
[ChildElementInfoAttribute(typeof(AutoRedefine))]
[ChildElementInfoAttribute(typeof(StyleHidden))]
[ChildElementInfoAttribute(typeof(UIPriority))]
[ChildElementInfoAttribute(typeof(SemiHidden))]
[ChildElementInfoAttribute(typeof(UnhideWhenUsed))]
[ChildElementInfoAttribute(typeof(Locked))]
[ChildElementInfoAttribute(typeof(Personal))]
[ChildElementInfoAttribute(typeof(Rsid))]
[ChildElementInfoAttribute(typeof(StyleParagraphProperties))]
[ChildElementInfoAttribute(typeof(StyleRunProperties))]
[ChildElementInfoAttribute(typeof(StyleTableProperties))]
[ChildElementInfoAttribute(typeof(StyleTableCellProperties))]
[ChildElementInfoAttribute(typeof(TableStyleProperties))]
public class Style : OpenXmlCompositeElement

[ISO/IEC 29500-1 1st Edition]

style (Style Definition)

This element specifies the definition of a single style within a WordprocessingML document. A style is a predefined set of table, numbering, paragraph, and/or character properties which can be applied to regions within a document.

The style definition for any style definition can be divided into three segments:

  • General style properties

  • Style type

  • Style type-specific properties

General style properties refers to the set of properties which can be used regardless of the style type; for example, the style name, additional aliases for the style, a style ID (used by the document content to refer to the style), if style is hidden, if style is locked, etc.

[Example: Consider a style called Heading a document as follows:

<w:style w:type="paragraph" w:styleId="Heading1">
  <w:name w:val="Heading 1"/>
  <w:basedOn w:val="Normal"/>
  <w:next w:val="Normal"/>
  <w:link w:val="Heading1Char"/>
  <w:uiPriority w:val="1"/>
  <w:qformat/>
  <w:rsid w:val="00F303CE"/>
  …</w:style>

Above the formatting information specific to this style type are a set of general style properties which define information shared by all style types. end example]

Style types refers to the property on a style which defines the type of style created with this style definition. WordprocessingML supports six types of style definitions by the values for the style definition's type attribute:

  • Paragraph styles

  • Character styles

  • Linked styles (paragraph + character) [Note: Accomplished via the link element (§17.7.4.6). end note]

  • Table styles

  • Numbering styles

  • Default paragraph + character properties

[Example: Consider a style called Heading a document as follows:

<w:style w:type="paragraph" w:styleId="Heading1">
  <w:name w:val="heading 1"/>
  <w:basedOn w:val="Normal"/>
  <w:next w:val="Normal"/>
  <w:link w:val="Heading1Char"/>
  <w:uiPriority w:val="1"/>
  <w:qformat/>
  <w:rsid w:val="00F303CE"/>
  …</w:style>

The type attribute has a value of paragraph, which indicates that the following style definition is a paragraph style. end example]

Style type-specific properties refers to the payload of the style: its formatting information as well as any properties which apply only to that style type.

[Example: Consider a table style with primary name Normal Table defined as follows:

<w:style w:type="table" w:default="1" w:styleId="TableNormal">
  <w:name w:val="Normal Table"/>
  …  <w:tblPr>
    <w:tblInd w:w="0" w:type="dxa"/>
    <w:tblCellMar>
      <w:top w:w="0" w:type="dxa"/>
      <w:left w:w="108" w:type="dxa"/>
      <w:bottom w:w="0" w:type="dxa"/>
      <w:right w:w="108" w:type="dxa"/>
    </w:tblCellMar>
  </w:tblPr>
</w:style>

The tblPr element contains the formatting payload for this table style, which is only applicable to a table style. end example]

Parent Elements

styles (§17.7.4.18)

Child Elements

Subclause

aliases (Alternate Style Names)

§17.7.4.1

autoRedefine (Automatically Merge User Formatting Into Style Definition)

§17.7.4.2

basedOn (Parent Style ID)

§17.7.4.3

hidden (Hide Style From User Interface)

§17.7.4.4

link (Linked Style Reference)

§17.7.4.6

locked (Style Cannot Be Applied)

§17.7.4.7

name (Primary Style Name)

§17.7.4.9

next (Style For Next Paragraph)

§17.7.4.10

personal (E-Mail Message Text Style)

§17.7.4.11

personalCompose (E-Mail Message Composition Style)

§17.7.4.12

personalReply (E-Mail Message Reply Style)

§17.7.4.13

pPr (Style Paragraph Properties)

§17.7.8.2

qFormat (Primary Style)

§17.7.4.14

rPr (Run Properties)

§17.7.9.1

rsid (Revision Identifier for Style Definition)

§17.7.4.15

semiHidden (Hide Style From Main User Interface)

§17.7.4.16

tblPr (Style Table Properties)

§17.7.6.4

tblStylePr (Style Conditional Table Formatting Properties)

§17.7.6.6

tcPr (Style Table Cell Properties)

§17.7.6.9

trPr (Style Table Row Properties)

§17.7.6.11

uiPriority (Optional User Interface Sorting Order)

§17.7.4.19

unhideWhenUsed (Remove Semi-Hidden Property When Style Is Used)

§17.7.4.20

Attributes

Description

customStyle (User-Defined Style)

Specifies that this style is a user-defined style (i.e. it is not a style which was automatically generated by an application). This setting (specifically a value of true or its equivalents) shall not allow the formatting associated with the style to be changed automatically by an application, but can be used to specify that if the associated style ID is known, certain user interface behaviors can be applied to its definition. [Example: The style's primary name can be localized to match the current user interface language. end example]

If this attribute is omitted, then the style shall be assumed to be a built-in style.

[Example: Consider a paragraph style defined as follows:

<w:style w:type="paragraph" w:styleId="MyStyle" w:customStyle="true">
  <w:name w:val="My Paragraph Style"/>
  <w:rPr>
    <w:b/>
  </w:rPr>
</w:style>

This paragraph style specifies that it is a user-defined style using the customStyle attribute's value of true. An application can therefore take action on the style if it has behaviors associated with the style ID MyStyle. end example]

The possible values for this attribute are defined by the ST_OnOff simple type (§22.9.2.7).

default (Default Style)

Specifies that this style is the default for this style type.

This property is used in conjunction with the type attribute to determine the style which is applied to objects that do not explicitly declare a style. [Example: The paragraph style with the default attribute set is the paragraph style applied to all paragraphs which do not explicitly reference a paragraph style using the pStyle element (§17.3.1.27). end example]

If this attribute is not specified for any style, then no properties shall be applied to objects of the specified style type. If this attribute is specified by multiple styles, then the last instance of a style with this property shall be used.

[Example: Consider a paragraph style defined as follows:

<w:style w:type="paragraph" w:default="1" w:styleId="MyStyle" >
  <w:name w:val="My Paragraph Style"/>
  <w:rPr>
    <w:b/>
  </w:rPr>
</w:style>

This paragraph style specifies that it is the default paragraph style, and therefore all paragraphs which do not explicitly reference a paragraph style must have this style applied.

For example, consider the following paragraphs from the same WordprocessingML document:

<w:p>
  <w:pPr>
    <w:pStyle w:val="Normal"/>
  </w:pPr>
  …</w:p>
<w:p>
  …</w:p>

The contents of the first paragraph must have the Normal paragraph style applied to them, while the contents of the second paragraph must have the MyStyle paragraph style applied, since it does not explicitly reference a paragraph style and therefore inherits the default. end example]

The possible values for this attribute are defined by the ST_OnOff simple type (§22.9.2.7).

styleId (Style ID)

Specifies a unique identifier for the parent style definition. This identifier shall be used in multiple contexts to uniquely reference this style definition within the document.

[Example: The following are examples of elements which reference a style via its styleId attribute value:

  • To reference a style from content using elements like the pStyle element (§17.3.1.27), rStyle element (§17.3.2.29), and the tblStyle element (§17.4.63) for paragraphs, runs, and tables, respectively.

  • To link the paragraph and character versions of a style via the link element (§17.7.4.6)

  • To reference the parent style for style inheritance via the basedOn element (§17.7.4.3)

end example]

If multiple style definitions each declare the same value for their styleId, then the first such instance shall keep its current identifier with all other instances being reassigned in any manner desired. This reassignment shall not require references to those style definitions to be 'repaired' in the content (i.e. some content might lose its style definition information, since the document was ill-formed).

If this attribute is not specified, then a style ID can be assigned in any manner desired.

[Example: Consider a paragraph style defined as follows:

<w:style w:type="paragraph" w:styleId="MyStyle" >
  <w:name w:val="My Paragraph Style"/>
  <w:rPr>
    <w:b/>
  </w:rPr>
</w:style>

This paragraph style specifies that its style identifier must be MyStyle using the styleId attribute.

Now consider the following paragraphs from the same WordprocessingML document:

<w:p>
  <w:pPr>
    <w:pStyle w:val="MyStyle"/>
  </w:pPr>
  …</w:p>
<w:p>
  …</w:p>

The contents of the first paragraph must have the bold paragraph property applied to them because their paragraph properties specify that they must inherit the paragraph style whose styleId is MyStyle therefore inheriting its properties using the rules of the style hierarchy. end example]

The possible values for this attribute are defined by the ST_String simple type (§22.9.2.13).

type (Style Type)

Specifies the type of style definition defined by this element. WordprocessingML supports six types of style definitions:

  • Paragraph styles

  • Character styles

  • Table styles

  • Numbering styles

  • Linked styles (paragraph + character)

  • Default paragraph + character properties

Each of the first four style types corresponds to a different value in this attribute, and therefore defines the style type of the current style. [Note: The last two style types are unique in that they are not simply a style type: a linked style is a pairing of a character and paragraph style via the link element (§17.7.4.6); and the document default properties are defined via the docDefaults element (§17.7.5.1). end note]

If this attribute is not specified, then the default value shall be assumed to be paragraph.

[Example: Consider a style defined as follows:

<w:style w:type="paragraph" … >
  <w:name w:val="My Paragraph Style"/>
  <w:rPr>
    <w:b/>
  </w:rPr>
</w:style>

The type attribute value of paragraph specifies that this style definition creates a paragraph style. end example]

The possible values for this attribute are defined by the ST_StyleType simple type (§17.18.83).

[Note: The W3C XML Schema definition of this element’s content model (CT_Style) is located in §A.1. end note]

© ISO/IEC29500: 2008.

The following code example opens a word processing file that contains some text, and applies the style "Heading 1" to the first paragraph.

using System;
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

namespace StyleEx
{
    class Program
    {
        // Apply the Heading 1 style to a paragraph.
        static void Main(string[] args)
        {
            string fileName = @"C:\users\public\documents\StyleEx.docx";

            using (WordprocessingDocument document = 
                WordprocessingDocument.Open(fileName, true))
            {
                // Get the first paragraph.
                Paragraph p =
                    document.MainDocumentPart.Document.Body.Elements<Paragraph>().First();

                // If the paragraph has no ParagraphProperties objects, create a new one for it.
                if (p.Elements<ParagraphProperties>().Count() == 0)
                {
                    p.PrependChild<ParagraphProperties>(new ParagraphProperties());
                }
        
                // Get the paragraph properties element of the paragraph.
                ParagraphProperties pPr = p.Elements<ParagraphProperties>().First();

                // Set the paragraph style ID to "Heading1".
                pPr.ParagraphStyleId = new ParagraphStyleId() { Val = "Heading1" };              
            }
        }
    }
}

Imports System.Linq
Imports DocumentFormat.OpenXml.Wordprocessing
Imports DocumentFormat.OpenXml.Packaging

Module Module1
    ' Apply the Heading 1 style to a paragraph.
    Sub Main()
        Dim fileName As String = "C:\Users\Public\Documents\StyleEx.docx"

        Using doc As WordprocessingDocument = _
            WordprocessingDocument.Open(fileName, True)
            ' Get the first paragraph.
            Dim p As Paragraph = doc.MainDocumentPart.Document.Body.Elements _
                (Of Paragraph).First()

            ' If the paragraph has no ParagraphProperties objects, create a new one for it.
            If p.Elements(Of ParagraphProperties).Count() = 0 Then
                p.PrependChild(Of ParagraphProperties)(New ParagraphProperties())
            End If

            ' Get the paragraph properties element of the paragraph.
            Dim pPr As ParagraphProperties = p.Elements(Of ParagraphProperties).First()

            ' Set the paragraph style ID to "Heading1".
            pPr.ParagraphStyleId = New ParagraphStyleId() With {.Val = "Heading1"}
        End Using
    End Sub
End Module

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

Show:
© 2014 Microsoft