Styles are useful for applying a set of formatting consistently throughout the document. In Document Editor, styles are created and added to a document programmatically or via the built-in Styles dialog.
A Style in Document Editor should have the following properties:
The style type should match the inherited style type. For example, it is not possible to have a character style inherit a paragraph style.
The default style for span and paragraph properties is normal. It internally inherits the default style of the document loaded or Document Editor component.
Each style initially checks its local value for the property that is being evaluated and turns to the style it is based on. If no local value is found, it turns to its default style.
Style inheritance of different styles are listed as follows:
Character styles are based only on other character styles.
The inheritance is: Character properties are inherited from the base character style.
Paragraph styles are based on other paragraph styles or on linked styles. When a paragraph style is based on another paragraph style, the inheritance of the properties is as follows:
When a paragraph style is based on a linked style, the inheritance of the properties is as follows:
Linked styles are composite styles and their components are paragraph and character styles with link between them. To apply paragraph properties, take the properties from the linked paragraph style. Similarly, to apply character properties, take the properties from linked character style. Linked styles are based on other linked styles or on paragraph styles.
When a linked style is based on a paragraph style, the hierarchy of the properties is as follows:
When a linked style is based on another linked style, the hierarchy of the properties is as follows:
New Styles are defined and added to the style collection of the document. In this way, they will be discovered by the default UI and applied to the parts of a document.
The following example shows how to programmatically create a character style.
//Initialize Document Editor component.
var documentEditor = new ej.documenteditor.DocumentEditor({ enableEditor: true, isReadOnly: false, enableSelection: true });
// Create custom style object.
var styleJson = {
"type": "Character",
"name": "New CharacterStyle",
"basedOn": "Default Paragraph Font",
"characterFormat": {
"fontSize": 16.0,
"fontFamily": "Calibri Light",
"fontColor": "#2F5496",
"bold": true,
"italic": true,
"underline": "Single"
}
};
//Created new style using `createStyle` method.
documentEditor.editor.createStyle(JSON.stringify(styleJson));
The following example shows how to programmatically create a paragraph style.
//Initialize Document Editor component.
var documentEditor = new ej.documenteditor.DocumentEditor({ enableEditor: true, isReadOnly: false, enableSelection: true });
// Create custom style object.
var styleJson = {
"type": "Paragraph",
"name": "New ParagraphStyle",
"basedOn": "Normal",
"characterFormat": {
"fontSize": 16.0,
"fontFamily": "Calibri Light",
"fontColor": "#2F5496",
"bold": true,
"italic": true,
"underline": "Single"
},
"paragraphFormat": {
"leftIndent": 0.0,
"rightIndent": 0.0,
"firstLineIndent": 0.0,
"beforeSpacing": 12.0,
"afterSpacing": 0.0,
"lineSpacing": 1.0791666507720947,
"lineSpacingType": "Multiple",
"textAlignment": "Left",
"outlineLevel": "Level1"
}
};
//Created new style using `createStyle` method.
documentEditor.editor.createStyle(JSON.stringify(styleJson));
The following example shows how to programmatically create linked style.
//Initialize Document Editor component.
var documentEditor = new ej.documenteditor.DocumentEditor({ enableEditor: true,isReadOnly: false, enableSelection: true });
// Create custom style object.
var styleJson = {
"type": "Paragraph",
"name": "New Linked",
"basedOn": "Normal",
"next": "Normal",
"link": "New Linked Char",
"characterFormat": {
"fontSize": 16.0,
"fontFamily": "Calibri Light",
"fontColor": "#2F5496"
},
"paragraphFormat": {
"leftIndent": 0.0,
"rightIndent": 0.0,
"firstLineIndent": 0.0,
"beforeSpacing": 12.0,
"afterSpacing": 0.0,
"lineSpacing": 1.0791666507720947,
"lineSpacingType": "Multiple",
"textAlignment": "Left",
"outlineLevel": "Level1"
}
};
//Created new style using `createStyle` method.
documentEditor.editor.createStyle(JSON.stringify(styleJson));
The styles are applied using the applyStyle method of editorModule, the parameter should be passed is the Name of the Style.
The styles of the Character type is applied to the currently selected part of the document. If there is no selection, the values that will be applied to the word at caret position. The styles of Paragraph type follow the same logic and are applied to all paragraphs in the selection or the current paragraph.
When there is no selection, styles of Linked type will change the values of the paragraph, and apply both the Paragraph and Character properties. When there is selection, Linked Style changes only the character properties of the selected text.
For example, the following line will apply the “New Linked” to the current paragraph.
//Apply specified style for selected paragraph.
editor.editorModule.applyStyle('New Linked');
//Clear direct formatting and apply the specified style
editor.editorModule.applyStyle('New Linked', true);