Theme colours and fonts
A Word document's theme holds twelve colours (two dark, two light, six accents, two hyperlink colours) and two fonts (the heading font and the body font). Text, styles, tables and borders that use a theme colour or a theme font name a slot of the theme, not a value, so changing the theme recolours or refonts all of them at once. This is what Word's Design > Colors and Design > Fonts, and their Customize Colors and Customize Fonts dialogs, do.
SetThemeColors and SetThemeFonts make the same change as one undoable edit:
using DocWright.Core.Primitives;
using DocWright.Dom;
using DocWright.Dom.Editing;
DocumentPosition at = DocumentPosition.StartOf(paragraph);
DocumentEdit colors = document.SetThemeColors(at, c =>
{
c.Name = "Custom 1";
c.Accent1 = ColorRgb.Parse("C00000");
c.Dark2 = ColorRgb.Parse("1F2937");
});
DocumentEdit fonts = document.SetThemeFonts(at, f =>
{
f.Name = "Custom fonts";
f.Major.Latin = "Georgia"; // headings
f.Minor.Latin = "Verdana"; // body text
});
colors.Undo(); // the theme and every colour it reaches, back as they were
What is written
- Only what changed. In the theme part, the colour slots and typefaces you changed are rewritten and the scheme's name is written; the rest of the part (its format scheme, its per-script fonts, its extensions) is kept as it was. This is what Word does.
- The cached colours. Beside each theme colour, Word stores the RGB value it resolves to (
w:valnext tow:themeColor). Every colour on a slot that changed gets its stored value rewritten, with its tint or shade, in the body, headers, footers, notes, styles, numbering, tables, page borders and page background. - A theme part when there is none. A document built in memory, or a package without
theme1.xml, gets a minimal valid theme part.
Each call returns an empty edit when nothing changed. ArgumentException refuses an auto colour, an empty Latin
typeface, and a typeface longer than 31 characters or with a control character, before anything changes.
Reading the theme
The theme is document.Theme: Colors (a ThemeColorScheme, with its Name), MajorFonts and MinorFonts
(ThemeFontSet: Latin, EastAsian, ComplexScript, ScriptFonts) and FontSchemeName. Layout resolves theme
colours and fonts from it, so a change shows in the next layout.
Note
Office's built-in colour and font sets (Blue, Green, Georgia …) are Microsoft's data and are not shipped with DocWright. An application that offers them sets their values with these same two calls.