Changing Background Color Dynamically
Leave feedback
View as MarkdownMarkdown
This feature is supported by version 17.12 or greater
You can set text background color for document contents dynamically using backColor tags.
The code uses some of the objects defined in:
The Business Layer
A color expression of a backColor tag must return a value of one of the following types:
- A string containing the name of a known color, that is, the case-insensitive name of a member of the KnownColor enumeration such as “red”
- A string containing a hex value: “#RRGGBB”, “#RGB” or “#AARRGGBB”, such as “#FF0000” (red)
- An integer value defining RGB (red, green, blue) components of the color such as 0xFFFF00 (yellow)
- A value of the Color type
Hex strings are supported by version 26.11 or greater. Before, they gave no color.
In the following example, the colors come from data as hex strings and a color name:
<<foreach [s in statuses]>><<backColor [s.Color]>><<[s.Name]>><</backColor>>
<</foreach>>
public class OrderStatus
{
public string Name { get; set; }
public string Color { get; set; }
}
OrderStatus[] statuses =
{
new OrderStatus { Name = "Paid", Color = "#C6EFCE" },
new OrderStatus { Name = "Pending", Color = "#FFEB9C" },
new OrderStatus { Name = "Overdue", Color = "#F00" },
new OrderStatus { Name = "Cancelled", Color = "LightGray" },
};
new DocumentAssembler().AssembleDocument("Hex color.docx", "output.docx",
new DataSourceInfo(statuses, "statuses"));
The four lines get the backgrounds C6EFCE, FFEB9C, FF0000 and D3D3D3. Example: SetBackColorFromHexString.cs.
We value your opinion. Your feedback will help us improve our documentation.