Back to Devexpress

How to: Apply or Modify a Workbook Theme

officefileapi-403098-spreadsheet-document-api-examples-formatting-how-to-apply-or-modify-a-workbook-theme.md

latest5.7 KB
Original Source

How to: Apply or Modify a Workbook Theme

  • Apr 22, 2021
  • 3 minutes to read

A document theme is a set of fonts, colors, and graphic effects you can use to change the entire look of your document. Every workbook has an associated theme returned by the Workbook.Theme property. A new document has the default Office 2007 - 2010 theme applied. You can use a custom theme for your document or change predefined theme colors.

Important

The Workbook class is defined in the DevExpress.Docs.v25.2.dll assembly. Add this assembly to your project to use the Workbook API. You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use this assembly in production code.

Load and Apply a Custom Theme

In Microsoft® Excel®, you can save a document theme as a .thmx file to reuse this theme in other documents. Refer to the following topic for details: Save a Custom Theme for Reuse. The WorkbookTheme.Load method allows you to load a file with a custom theme and to apply it to your workbook.

The following code snippet applies the Quotable theme to the document:

csharp
using DevExpress.Spreadsheet;
// ...

using (Workbook workbook = new Workbook())
{
    WorkbookTheme docTheme = workbook.Theme;
    docTheme.Load(@"CustomThemes\Quotable.thmx");
}
vb
Imports DevExpress.Spreadsheet
' ...

Using workbook As New Workbook()
    Dim docTheme As WorkbookTheme = workbook.Theme
    docTheme.Load("CustomThemes\Quotable.thmx")
End Using

Customize Theme Colors

Call the WorkbookTheme.GetColors method to return a ThemeColorScheme object that defines a palette of 12 complementary theme colors. The object’s properties allow you to customize theme colors. Call the WorkbookTheme.SetColors method to apply new colors to the theme.

The following example demonstrates how to use colors of the Slipstream palette instead of the Office 2013 theme colors:

csharp
using DevExpress.Spreadsheet;
using System.Drawing;
// ...

using (Workbook workbook = new Workbook())
{
    var docTheme = workbook.Theme;
    var themeColors = docTheme.GetColors();
    themeColors.Name = "Slipstream";
    themeColors.Dark1 = Color.FromKnownColor(KnownColor.WindowText);
    themeColors.Light1 = Color.FromKnownColor(KnownColor.Window);
    themeColors.Dark2 = Color.FromArgb(0x21, 0x27, 0x45);
    themeColors.Light2 = Color.FromArgb(0xB4, 0xDC, 0xFA);
    themeColors.Accent1 = Color.FromArgb(0x4E, 0x67, 0xC8);
    themeColors.Accent2 = Color.FromArgb(0x5E, 0xCC, 0xF3);
    themeColors.Accent3 = Color.FromArgb(0xA7, 0xEA, 0x52);
    themeColors.Accent4 = Color.FromArgb(0x5D, 0xCE, 0xAF);
    themeColors.Accent5 = Color.FromArgb(0xFF, 0x80, 0x21);
    themeColors.Accent6 = Color.FromArgb(0xF1, 0x41, 0x24);
    themeColors.Hyperlink = Color.FromArgb(0x56, 0xC7, 0xAA);
    themeColors.FollowedHyperlink = Color.FromArgb(0x59, 0xA8, 0xD1);
    docTheme.SetColors(themeColors);
}
vb
Imports DevExpress.Spreadsheet
Imports System.Drawing
' ...

Using workbook As New Workbook()
    Dim docTheme As WorkbookTheme = workbook.Theme
    Dim themeColors As ThemeColorScheme = docTheme.GetColors()
    themeColors.Name = "Slipstream"
    themeColors.Dark1 = Color.FromKnownColor(KnownColor.WindowText)
    themeColors.Light1 = Color.FromKnownColor(KnownColor.Window)
    themeColors.Dark2 = Color.FromArgb(&H21, &H27, &H45)
    themeColors.Light2 = Color.FromArgb(&HB4, &HDC, &HFA)
    themeColors.Accent1 = Color.FromArgb(&H4E, &H67, &HC8)
    themeColors.Accent2 = Color.FromArgb(&H5E, &HCC, &HF3)
    themeColors.Accent3 = Color.FromArgb(&HA7, &HEA, &H52)
    themeColors.Accent4 = Color.FromArgb(&H5D, &HCE, &HAF)
    themeColors.Accent5 = Color.FromArgb(&HFF, &H80, &H21)
    themeColors.Accent6 = Color.FromArgb(&HF1, &H41, &H24)
    themeColors.Hyperlink = Color.FromArgb(&H56, &HC7, &HAA)
    themeColors.FollowedHyperlink = Color.FromArgb(&H59, &HA8, &HD1)
    docTheme.SetColors(themeColors)
End Using

Reset a Theme

Call the Reset method to use one of the predefined Office themes instead of a custom theme.

csharp
using DevExpress.Spreadsheet;
// ...

using (Workbook workbook = new Workbook())
{
    WorkbookTheme docTheme = workbook.Theme;
    // Change the document theme
    // to the default Office 2013 theme.
    docTheme.Reset(ThemePreset.Office2013);
}
vb
Imports DevExpress.Spreadsheet
' ...

Using workbook As New Workbook()
    Dim docTheme As WorkbookTheme = workbook.Theme
    ' Change the document theme
    ' to the default Office 2013 theme.
    docTheme.Reset(ThemePreset.Office2013)
End Using