Document Themes
The document model comes with several predefined themes called Document themes. They enable you to specify colors, fonts, and a variety of graphic effects in a document and affect the look and feel of the whole workbook. Each theme contains a color scheme and a font scheme and is represented by the DocumentTheme class.
Color Schemes
A color scheme has a unique name and contains several predefined colors. Its representation in the document model is the ThemeColorScheme class. A scheme defines twelve colors and each of these is assigned a sole ThemeColorType. The following list contains all ThemeColorType values:
-
background1
-
text1
-
background2
-
text2
-
accent1
-
accent2
-
accent3
-
accent4
-
accent5
-
accent6
-
hyperlink
-
followed hyperlink
The twelve color types are used for creating ThemableColor objects. They determine the color of the scheme that appears as the actual color of the ThemableColor instance. As you change the theme or the color scheme, the actual color of the ThemableColor object changes as well. For example, if you set the fill of a cell to be a ThemableColor, applying a new theme or another scheme also affects the solid fill.
Example 1 demonstrates how to create a ThemeColorScheme object. The example passes a name and twelve colors to the constructor. Every color has a comment next to it, so you can see its corresponding ThemeColorType.
Example 1: Create ThemeColorScheme
ThemeColorScheme colorScheme = new ThemeColorScheme(
"Mine",
Colors.Black, // background 1
Colors.Blue, // text 1
Colors.Brown, // background 2
Colors.Cyan, // text 2
Colors.DarkGray, // accent 1
Colors.Gray, // accent 2
Colors.Green, // accent 3
Colors.LightGray, // accent 4
Colors.Magenta, // accent 5
Colors.Orange, // accent 6
Colors.Purple, // hyperlink
Colors.Red); // followedHyperlink
There are several ways to create a ThemableColor object:
-
Passing two parameters to the constructor – ThemeColorType and double.
-
ThemeColorTypeis an enum which has twelve possible values (the aforementioned color types). -
The second parameter is of type
doubleand must be between -1 and 1. It represents the tint and shade to be applied to the selected color. -
Passing ThemeColorType and ColorShadeType.
-
ThemeColorTypeis the same as in the previously mentioned constructor.
To create colors that depend on the current document theme, you need to use ThemableColor objects.
Example 2 shows how you can create a ThemableColor.
Example 2: Create ThemableColor
ThemableColor themableColor = new ThemableColor(ThemeColorType.Accent1);
Font Schemes
A font scheme is represented by the ThemeFontScheme class. Every font scheme consists of a name and several predefined font families. Each font family corresponds to one of two font types:
-
Major
-
Minor
To create a ThemeFontScheme you need to pass a name and two font family names to the font scheme constructor. The former font family name corresponds to the Major ThemeFontType and the latter corresponds to the Minor.
Example 3 illustrates how to create a ThemeFontScheme object.
Example 3: Create ThemeFontScheme
ThemeFontScheme fontScheme = new ThemeFontScheme(
"Mine",
"Times New Roman", // latinMajor
"Arial"); // latinMinor
To use the document theme fonts, you need to use ThemableFontFamily objects. There are several ways you can create one:
-
Passing a
ThemeFontTypeobject as a constructor parameter – this way you bind the object to the currently selected document theme. -
Passing a
FontFamilyobject or a string representing a FontFamily name – the result is a static FontFamily that does not change when the document theme changes.
When you need to create a font that depends on the current document theme, use the ThemableFontFamily objects.
Example 4 shows how to create a ThemableFontFamily.
Example 4: Create ThemableFontFamily
ThemableFontFamily themableFont = new ThemableFontFamily(ThemeFontType.Major);
Document Themes
Now that you have a color and a font scheme, you can create a new DocumentTheme. You need to specify a name and pass the already created color and font schemes.
Example 5 demonstrates how to create a DocumentTheme using the color scheme from Example 1 and the font scheme from Example 3.
Example 5: Create DocumentTheme
DocumentTheme theme = new DocumentTheme("Mine", colorScheme, fontScheme);
In the predefined static class PredefinedThemeSchemes, you can find several predefined color and font schemes. The class exposes the properties ColorSchemes and FontSchemes that hold all predefined schemes.
Example 6 shows how you can create a document theme using the predefined color and font schemes.
Example 6: Create DocumentTheme from Predefined Schemes
DocumentTheme predefined_theme = new DocumentTheme("From predefined schemes", PredefinedThemeSchemes.ColorSchemes[0], PredefinedThemeSchemes.FontSchemes[5]);
To change the current document theme, set a single property:
Example 7 changes the theme of a newly created workbook.
Example 7: Change DocumentTheme
Workbook workbook = new Workbook();
workbook.Worksheets.Add();
workbook.Theme = theme;
Getting Actual Values
To get the actual value from ThemableColor or ThemableFontFamily, call the GetActualValue() method on the corresponding object.
Example 8: Get Actual Color
Color actualColor = themableColor.GetActualValue(theme);
//the actual color is the same as Accent1 color of the colorScheme
Example 9: Get Actual Font
var actualFont = themableFont.GetActualValue(theme);
//the actualFont is the same as the Major font of the fontScheme