Apply Styles to Document Content
- 8 minutes to read
A style is a named set of formatting settings. Apply a style to multiple paragraphs, text fragments, or tables to format them consistently. When you modify the style, the document updates all content that uses the style.
The Word Processing Document API supports the following style types:
| Style Type | Class | Document Collection | Apply to Content | Formatting Settings |
|---|---|---|---|---|
| Paragraph style | ParagraphStyle | WordDocument.ParagraphStyles | Paragraph.StyleName | Paragraph formatting, character formatting, and list numbering. |
| Character style | CharacterStyle | WordDocument.CharacterStyles | TextFragment.CharacterStyleName | Character formatting. |
| Table style | TableStyle | WordDocument.TableStyles | Table.StyleName | Table, cell, paragraph, and character formatting, and formatting for individual table elements. |
Styles use the same CharacterProperties and ParagraphProperties objects as direct formatting. Refer to the following help topic for additional information about available formatting settings: Format Document Content Directly.
Built-in Styles
A new document contains the following styles:
| Style Type | Styles |
|---|---|
| Paragraph styles | Normal |
| Character styles | Default Paragraph Font, Line Number, Hyperlink |
| Table styles | Normal Table, Table Simple 1 |
When you load a document, style collections contain styles from the document file.
The Normal, Default Paragraph Font, and Normal Table styles are default styles. Paragraphs, text fragments, and tables without an assigned style use default styles. You cannot remove default styles from a document.
The Table(rowCount, columnCount) constructor applies the table style specified by the static Table.DefaultStyleName property to a new table.
Create a Style
To create a style, call the StyleCollectionBase<TStyle>.Add(name) method of a style collection (for example, document.ParagraphStyles.Add("Report Heading")). The Add method creates a style, adds it to the document, and returns the style object. You can also create a ParagraphStyle, CharacterStyle, or TableStyle object and pass it to the StyleCollectionBase<TStyle>.Add(style) method.
Style names are case-sensitive (Heading and heading are different styles). A collection cannot contain two styles with the same name. If the collection contains a style with the specified name, the Add method throws an InvalidOperationException.
Use ParagraphProperties and CharacterProperties properties of a style to specify formatting settings. Set the StyleBase.Primary property to true to display the style in the Microsoft Word Styles gallery.
Apply a Style
To apply a style, set the Paragraph.StyleName, TextFragment.CharacterStyleName, or Table.StyleName property to the style name. Set the property to null (Nothing in VB.NET) to apply the default style.
If the document does not contain a style with the specified name, the paragraph, text fragment, or table uses the default style. When you add a style with this name to the document, the API applies the added style.
Use the StyleCollectionBase<TStyle>[name] indexer to get a style by name. The indexer returns null if the collection does not contain a style with the specified name.
The following code snippet creates a paragraph style and a character style and applies them to document content:
using System.Drawing;
using System.IO;
using DevExpress.Docs;
using DevExpress.Docs.Word;
using var document = new WordDocument();
// Create a paragraph style based on the Normal style.
var headingStyle = document.ParagraphStyles.Add("Report Heading");
headingStyle.ParentName = "Normal";
headingStyle.Primary = true;
headingStyle.ParagraphProperties.SpacingAfter = 60;
headingStyle.ParagraphProperties.KeepWithNext = true;
headingStyle.CharacterProperties.FontSize = 16;
headingStyle.CharacterProperties.Bold = true;
headingStyle.CharacterProperties.ForeColor = new OfficeColor(Color.DarkBlue);
var termStyle = document.CharacterStyles.Add("Term");
termStyle.CharacterProperties.Italic = true;
termStyle.CharacterProperties.ForeColor = new OfficeColor(Color.DarkRed);
var heading = document.Body.AddParagraph("Project Summary");
heading.StyleName = headingStyle.Name;
var paragraph = document.Body.AddParagraph("The ");
var term = paragraph.AddText("critical path");
term.CharacterStyleName = termStyle.Name;
paragraph.AddText(" includes all tasks that affect the release date.");
Directory.CreateDirectory("output");
using var stream = File.Create(Path.Combine("output", "Document.docx"));
document.Save(stream, DocumentFormat.Docx);
Style Inheritance
Use the StyleBase.ParentName property to base a style on another style of the same type. A style gets formatting settings that it does not specify from the parent style. If you modify the parent style, child styles reflect the modified settings.
If the document does not contain a style with the specified parent name, the style does not inherit formatting settings. When you add a style with this name to the document, the child style inherits settings from the added style.
A style cannot inherit from itself directly or through other styles (for example, Style A based on Style B, and Style B based on Style A). If a parent style name creates an inheritance cycle, the API throws an InvalidOperationException.
Formatting Precedence
A text fragment can get formatting settings from multiple sources. If multiple sources specify the same setting, the setting from the source with the higher priority takes precedence. The following list starts with the highest priority:
- Direct formatting (
TextFragment.CharacterPropertiesandParagraph.Propertiesproperties). - The character style.
- The paragraph style.
- The table style (for paragraphs in table cells).
Copy Styles
Call the Clone() method of a style object (for example, ParagraphStyle.Clone()) to copy a style to another document. The copy has the same name, formatting settings, and parent style name as the source style. In the target document, the copy inherits settings from the style with the parent style name.
Call the Clone(name) method (for example, ParagraphStyle.Clone(name)) to create a style with a new name and the same settings as the source style. You can add this copy to the same document.
The following code snippet copies the Heading 1 style from Document.docx to another document and creates the Heading 1 Accent style with a different font color:
using var source = new WordDocument(File.OpenRead("Document.docx"));
var heading1 = source.ParagraphStyles["Heading 1"];
document.ParagraphStyles.Add(heading1.Clone());
var accent = heading1.Clone("Heading 1 Accent");
accent.CharacterProperties.ForeColor = new OfficeColor(Color.DarkOrange);
document.ParagraphStyles.Add(accent);
Remove Styles
Call the StyleCollectionBase<TStyle>.Remove(style) or StyleCollectionBase<TStyle>.RemoveAt(index) method to remove a style from a document. If the specified style is a default style, Remove and RemoveAt methods do not remove the style and throw an InvalidOperationException.
After you remove a style, the following changes apply:
- Paragraphs, text fragments, and tables with the removed style use the default style. If you add a style with the removed style name to the document later, the API applies the added style to this content.
- Child styles of the removed style do not inherit formatting settings until you add a style with the parent style name.
Table Styles
A TableStyle object contains the following formatting settings for the entire table:
| Property | Settings |
|---|---|
| TableProperties | Table alignment, layout, borders, cell margins, cell spacing, and background color. |
| TableCellProperties | Cell borders, margins, background color, and vertical alignment. |
| ParagraphProperties | Paragraph formatting in table cells. |
| CharacterProperties | Character formatting in table cells. |
Conditional Styles
Use the ConditionalStyles property to format individual table elements (for example, the header row, the first column, banded rows, or corner cells). The TableConditionalStyleCollection maps a TableConditionalStyleType value to a TableConditionalStyle object that contains formatting settings for the table element.
Call the TableConditionalStyleCollection.Add(type, style) method to add a conditional style. If the collection contains a conditional style for the specified table element, the Add method throws an ArgumentException. To replace a conditional style, use the TableConditionalStyleCollection[type] indexer.
Use the Table.Look property to specify conditional styles that apply to a table (for example, header row formatting and row banding).
The following code snippet creates a table style with a formatted header row and banded rows and applies the style to a table:
var tableStyle = document.TableStyles.Add("Price List");
tableStyle.ParentName = "Table Simple 1";
tableStyle.CharacterProperties.FontSize = 10;
var header = new TableConditionalStyle();
header.CharacterProperties.Bold = true;
header.CharacterProperties.ForeColor = new OfficeColor(Color.White);
header.TableCellProperties.BackColor = new OfficeColor(Color.SteelBlue);
tableStyle.ConditionalStyles.Add(TableConditionalStyleType.FirstRow, header);
var banding = new TableConditionalStyle();
banding.TableCellProperties.BackColor = new OfficeColor(Color.AliceBlue);
tableStyle.ConditionalStyles.Add(TableConditionalStyleType.OddRowBanding, banding);
var table = new Table(4, 2);
table.StyleName = tableStyle.Name;
table.Look = TableLookTypes.ApplyFirstRow;
document.Body.Add(table);
List Numbering in Paragraph Styles
A paragraph style can apply list numbering to paragraphs. Use the following properties to specify numbering settings:
- ParagraphStyle.NumberingList specifies the list that numbers paragraphs with the style.
- ParagraphStyle.ListLevelIndex specifies the list level.
- ParagraphStyle.NumberingDisabled specifies whether the style removes numbering that the parent style applies.
If the NumberingList or ListLevelIndex property is null, the style gets the setting from the parent style.
Refer to the following help topic for additional information about lists: Manage Lists.