Table of Contents

Format, style and list a span

Bold three words in the middle of a run, center two paragraphs, put a selection in a bulleted list, apply a heading style. Each method takes a DocumentSpan, changes what it covers the way Word does, and returns a DocumentEdit that takes the change back exactly. StyleResolver.ResolveSpanFormatting tells a toolbar what formatting the span has.

Bold, a list, the toolbar's state, undo

using DocWright.Dom;
using DocWright.Dom.Editing;
using DocWright.Layout;

using var document = new WordDocument();
Paragraph first = document.AppendParagraph("Make the middle bold");
Paragraph milk = document.AppendParagraph("Milk");
Paragraph bread = document.AppendParagraph("Bread");

// Bold "the middle": the run is split at both ends of the span.
var middle = new DocumentSpan(DocumentPosition.InParagraph(first, 5), DocumentPosition.InParagraph(first, 15));
DocumentEdit bold = document.FormatText(middle, f => f.Bold = true);
Console.WriteLine(string.Join(" | ", first.Inlines.OfType<TextRun>().Select(r => r.Text + (r.Format?.Bold == true ? " (bold)" : ""))));

// Two paragraphs into a bulleted list: the list definition is added from Word's template.
var shopping = new DocumentSpan(DocumentPosition.StartOf(milk), DocumentPosition.EndOf(bread));
DocumentEdit bullets = document.ApplyList(shopping, ListKind.Bullet);
Console.WriteLine($"lists: {document.Numbering.Instances.Count}, Milk and Bread in list {milk.Format.NumberingId} and {bread.Format.NumberingId}");

// What a toolbar shows for the selection: the formatting in effect, resolved through the styles.
SpanFormatting state = StyleResolver.ResolveSpanFormatting(document, middle);
bool allBold = state.Paragraphs.SelectMany(p => p.Content).All(c => c.Bold);
Console.WriteLine($"bold everywhere in the selection: {allBold}, size {state.Paragraphs[0].Content[0].FontSize}");

// Undo puts back the very run and format instances, and takes the list definition out again.
bullets.Undo();
bold.Undo();
Console.WriteLine($"after undo: {first.Inlines.Count} run, {document.Numbering.Instances.Count} lists");

Output

Make  | the middle (bold) |  bold
lists: 1, Milk and Bread in list 1 and 1
bold everywhere in the selection: True, size 200tw
after undo: 1 run, 0 lists
Call Does
FormatText(span, f => …) Changes a copy of the character formatting of every run, tab and break the span covers; a run the span starts or ends inside is split there. Set a property to state it, set it to null to inherit it again. A field's result is formatted, its code is not.
FormatParagraphs(span, f => …) Changes the paragraph formatting of every paragraph the span touches (alignment, indents, spacing).
ApplyParagraphStyle(span, styleId) / ApplyCharacterStyle(span, styleId) Applies a style the document defines; direct formatting stays.
ApplyList(span, ListKind.Bullet or Decimal) Puts the paragraphs in a list, continuing the first paragraph's list or the one before it when it is the same kind, otherwise adding one from Word's default template.
RemoveList(span) / ChangeListLevel(span, delta) Takes paragraphs out of their list (with Word's explicit "no list" when the style lists them), or moves them deeper or shallower within levels 0 to 8.

Which marks are formatted

A paragraph's mark is formatted when the span runs past it into the next paragraph, as when a whole paragraph is selected. An empty span in an empty paragraph formats that paragraph's mark, so the next character typed there takes the formatting.

The formatting in effect

ResolveSpanFormatting resolves only the paragraphs the span touches, and for a paragraph in a table that table's style. For each paragraph it returns the resolved paragraph properties, the list numbering and, in Content, the resolved character properties of each piece of content the span covers. For an empty span, the one entry is what text typed there would get. "Bold everywhere" is All(c => c.Bold), and "mixed" is a set of more than one value.

List galleries and restarting numbering

using DocWright.Dom;
using DocWright.Dom.Editing;

var all = new DocumentSpan(DocumentPosition.StartOf(items[0]), DocumentPosition.EndOf(items[^1]));
document.ApplyListTemplate(all, ListLibrary.Numbering[1]);   // 1) 2) 3)
document.RestartNumbering(items[2]);                         // Restart at 1 at "Build"
Console.WriteLine($"galleries: {ListLibrary.Bullets.Count} bullets, {ListLibrary.Numbering.Count} numberings, {ListLibrary.Multilevel.Count} multilevel");

Output

galleries: 6 bullets, 7 numberings, 7 multilevel
markers: 1) 2) 1) 2)

ListLibrary.Bullets, .Numbering and .Multilevel are Word's Home tab galleries, read from Word level by level. ApplyListTemplate continues the list above when it was made from the same template, and otherwise adds one. RestartNumbering (Restart at 1, or any start with start:) moves the paragraph and the rest of its list to a new numbering of the same definition; ContinueNumbering joins them to the list before again.

Undo

Every changed run or paragraph gets a new format object, and undo puts the original one back, so the document saves byte for byte as it did. Formatting that is already there changes nothing: the edit is empty (IsEmpty). ChangedParagraphKeys lists the paragraphs a layout cache must redo, as for text edits.