Changing Background Color Dynamically

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Assembly MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-assembly -- dnx GroupDocs.Assembly.Mcp --yes
Folders, licensing and more →
Note
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

Note
The code uses some of the objects defined in: The Business Layer

Color Values

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
Note
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.

Download