Table of Contents

Class StyleDefinitionEditingExtensions

Namespace
DocWright.Dom.Editing
Assembly
DocWright.Dom.dll

Changes style definitions as undoable edits, as Word's Styles pane does (WQ-0103): Modify a style, Update a style to Match Selection, and Create a Style from the selection's formatting. A changed definition reaches every paragraph and run that uses it through the style chain; each edit reports every paragraph of the document changed.

public static class StyleDefinitionEditingExtensions
Inheritance
StyleDefinitionEditingExtensions
Inherited Members

Remarks

Linked styles. A paragraph style and the character style linked to it (w:link, Heading1 and Heading1Char) carry the same run formatting: when one's changes, Word writes the other's to match, and these edits do the same.

Reapplying a style. Update and Create leave each paragraph of the style without its direct paragraph formatting (numbering, a tracked format change and the style stay), and remove each direct run property set on more than half of the paragraph's characters, the paragraph mark not counted; a property set on half or less stays, and so does a character style.

Methods

ApplyBuiltInStyle(WordDocument, DocumentSpan, string)

Applies one of Word's built-in styles by name, as the Styles gallery and Ctrl+Alt+1 do (WQ-0106). The document's style of that name is used when it has one (names compared ignoring case, heading 1 as Heading 1); a style hidden until used (w:semiHidden with w:unhideWhenUsed) is unhidden, and so is its linked style. A document without one gets Word's definition from BuiltInStyles, and for a linked paragraph style its … Char character style too (or the document's character style of that name), under Word's ids, numbered when an id is taken, based on the document's default styles. A paragraph style is applied to every paragraph the span touches, a character style to the text it covers; direct formatting stays.

CanDeleteStyle(WordDocument, string)

Gets whether DeleteStyle(WordDocument, DocumentPosition, string) deletes the style: a paragraph, character or table style that is not a default style and whose name (its id when it has none) is not one of Word's built-in names (IsWordStyleName(string)). Word deletes no other (WQ-0109).

CreateStyleFromSelection(WordDocument, DocumentSpan, string, bool)

Create a Style from the selection: a new paragraph style named name, based on the first paragraph's style, holding that paragraph's direct paragraph formatting and the direct run formatting at the span's start, with a linked character style (name Char) holding the same run formatting, as Word adds them. Every paragraph the span touches takes the new style, which is then reapplied to it.

DeleteStyle(WordDocument, DocumentPosition, string)

Deletes a style, as the Styles pane's Delete does (WQ-0109). Paragraphs of the style take the default paragraph style, and runs and tables of it lose their style; direct formatting stays. A style based on it is based on the default style of its type instead and takes the deleted chain's formatting under its own, so it looks as it did. A w:next naming it names the default paragraph style. A paragraph style and the character style linked to it are deleted together; when the other one is a built-in style it stays, without the link.

FormatStyle(WordDocument, DocumentPosition, string, Action<Style>)

Changes a style's definition, Modify Style: change runs on a copy that replaces the style. Direct formatting in the document is not touched. A paragraph style's run formatting is copied to its linked character style, and a character style's to its linked paragraph style.

SetDocumentDefaults(WordDocument, DocumentPosition, Action<DocDefaults>)

Changes the document's defaults, Manage Styles > Set Defaults (Phase 150, WQ-0123): change runs on a copy of DocDefaults (w:docDefaults) that replaces it. Every style resolves over the defaults, so every paragraph of the body, the headers and footers and the notes is reported changed. Styles and direct formatting are not touched.

SetStyleList(WordDocument, DocumentPosition, string, ListTemplate?)

Modify Style > Format > Numbering (WQ-0120): a paragraph style gets a new list made from a template, as Word writes it: the style's w:numPr names the list (no w:ilvl: level 0) and the list's first level names the style (w:pStyle), so every paragraph of the style is numbered. A null template is None: the style states no list (w:numId="0" where a base style has one). Direct formatting stays.

UpdateStyleToMatch(WordDocument, DocumentSpan)

Update Style to Match Selection: the style of the span's first paragraph takes that paragraph's direct paragraph formatting and the direct run formatting at the span's start (the first character a selection covers; at a caret, the formatting typing there would get), and is then reapplied to every paragraph of the style (see the remarks on StyleDefinitionEditingExtensions). When that leaves the style as it was, nothing changes and the edit is empty.