Themes, in a Word document, helps in making the global formatting changes, giving your document a consistent and professional look. Hence, themes are generally used to reinforce a brand across every document that is generated by a business.
DsWord allows you to customize the themes applied to a Word document through the Theme class which can be accessed using Theme property of the GcWordDocument class. The Theme class saves the font and colors that would be used in the document using the FontScheme and ColorScheme properties.
The theme font can be set using the FontScheme property of the Theme class. This property is of type FontScheme class which provides the MajorFont and MinorFont properties to specify the font for heading and body respectively.
And, to set the theme colors you would need to fetch the theme color scheme using the GetThemeColor method of the Settings class which accepts the ThemeColorId as a parameter. This method returns an instance of the ThemeColor class, which provides the RGB property to set the value of a color in the color scheme of the theme.
To change the theme color and font in a Word document:
C# |
Copy Code |
---|---|
GcWordDocument doc = new GcWordDocument(); var section = doc.Body.Sections.First; //Add Heading section.GetRange().Paragraphs.Add("Working with Themes", doc.Styles[BuiltInStyleId.Heading1]); //Add the first paragraph var p = section.GetRange().Paragraphs.Add( "This sample shows how you can access and manipulate" + " the document theme properties." + " For more information, refer to "); //Add a hyperlink Hyperlink link1 = p.GetRange().Hyperlinks.Add( new Uri("https://www.google.co.in/"), "", "Google"); //Customizing the color scheme(theme colors) of the document. //Setting the Accent1 theme color doc.Settings.GetThemeColor[ThemeColorId.Accent1].RGB = Color.DarkOrange; //Setting the Hyperlink theme color doc.Settings.GetThemeColor[ThemeColorId.Hyperlink].RGB = Color.DarkBlue; //Setting the FollowedHyperlink theme color doc.Settings.GetThemeColor[ThemeColorId.FollowedHyperlink].RGB = Color.DarkGray; //Customizing the font scheme(theme fonts) of the document //Setting Major(Heading) theme font for Latin characters doc.Theme.FontScheme.MajorFont.Latin.Name = "Calibri"; //Setting Minor(Body) theme font for Latin characters doc.Theme.FontScheme.MinorFont.Latin.Name = "Century Gothic"; //Saves the document doc.Save("Themes.docx"); |
For more information on how to modify theme colors using DsWord, see DsWord sample browser.