-
Notifications
You must be signed in to change notification settings - Fork 4
Styles
Styling an element via a style requires two steps:
-
Defining of a style
-
Binding the style to markdown elements according to their type, name, properties and position in the document tree
A style is a collection of styling properties like color, indentation, font, etc. Not all properties can be used for all elements. In such a case, they are ignored. For details, see Element reference. Any style can be bound to multiple element types, but each element can use one style only (see below - the style conflicts).
A style can be created from scratch:
var style = pdf.StyleManager.AddStyle("myStyle", MarkdownStyleNames.Break);Or it can be derived from other style:
var asterismStyle = pdf.StyleManager.AddStyle("asterismRuler", MarkdownStyleNames.Break);In such a case, when the style is used, all it's parents properties are used, unless redefined by the style or any later ancestor
The style name is not mandatory, but can be useful for later access to the style definition
After the style is created, the properties can be set to define the actual styling:
style.Font.Color = Colors.Blue;
style.Paragraph.Alignment = ParagraphAlignment.Justify;The style is bound to an element using a chain of selectors.
The simplest case is just binding to an element type:
pdf.StyleManager.ForElement(ElementType.Paragraph).Bind(style);Or, binding to an element type with style name set via an attribute {.styleName}
pdf.StyleManager.ForElement(ElementType.Paragraph, "styleName").Bind(style);To apply the style only to elements with special position in the document tree, selectors WithParent, WithAncestor and Where can be used.
For example:
pdf.StyleManager.ForElement(ElementType.UnorderedListItem).WithAncestor(ElementType.UnorderedListItem).Bind(style);binds the style only to nested list items, but not to first-level lists
If multiple styles fulfil the binding conditions, the style is selected by these rules:
-
binding with explicit style name is preferred to type-only based binding
-
binding
WithParentis preferred toWithAncestor -
more specific binding is preferred to a less specific one