Skip to main content

Color formats

Color formats

CSS color strings let you reuse familiar color values across text and graphics. Compare opaque CSS color strings accepted by the DynamicPDF Flow API. This example follows ColorFormats.cs and applies each string to both a rectangle background and a text element.

Fluent recipe / 06Color formatsCompare accepted CSS color strings using labeled swatches and colored text.

What you’ll create​

  • A Letter-sized page with 40-point margins.
  • Seven labeled color swatches, each followed by colored sample text.
  • A file named ColorFormats_output.pdf in your chosen output directory.
Generated PDF comparing labeled CSS color swatches and text.

Example code​

Add the following class to a console project that references the DynamicPDF Flow API. See the quick start for an overview. The class creates labeled color samples and saves the document to an existing output directory.

using System;
using System.IO;
using DynamicPDF.Flow;

namespace FluentExamples.Examples;

internal static class ColorFormats
{
public static void Run(string outputRoot)
{
var colors = new[]
{
"red", "rebeccapurple", "#ABC", "#AABBCC",
"rgb(255, 0, 128)", "rgb(100%, 0%, 50%)",
"hsl(120, 100%, 50%)"
};
var path = Path.Combine(
outputRoot, "ColorFormats_output.pdf");

DocumentBuilder.Create()
.Title("CSS color formats")
.DefaultUnit(LengthUnit.Point)
.Pages(page =>
{
page.Size(PageSize.Letter).Margins(40);
page.Text("CSS color formats", text => text
.FontSize(20));
page.Spacing(12);

foreach (var color in colors)
{
page.Text(color, text => text.FontSize(10));
page.Rectangle(rectangle => rectangle
.Width(200)
.Height(14)
.BackgroundColor(color)
.Border(1, LengthUnit.Point, "black"));
page.Text("Text sample", text => text
.FontSize(10)
.Color(color));
page.Spacing(6);
}
})
.Save(path);

Console.WriteLine($"Created {path}");
}
}
Imports System
Imports System.IO
Imports DynamicPDF.Flow

Friend Module ColorFormats
Public Sub Run(outputRoot As String)
Dim colors = {
"red", "rebeccapurple", "#ABC", "#AABBCC",
"rgb(255, 0, 128)", "rgb(100%, 0%, 50%)",
"hsl(120, 100%, 50%)"
}
Dim outputPath = Path.Combine(
outputRoot, "ColorFormats_output.pdf")

DocumentBuilder.Create().
Title("CSS color formats").
DefaultUnit(LengthUnit.Point).
Pages(Sub(page)
page.Size(PageSize.Letter).Margins(40)
page.Text("CSS color formats", Sub(text) text.
FontSize(20))
page.Spacing(12)

For Each color In colors
page.Text(color, Sub(text) text.FontSize(10))
page.Rectangle(Sub(rectangle) rectangle.
Width(200).
Height(14).
BackgroundColor(color).
Border(1, LengthUnit.Point, "black"))
page.Text("Text sample", Sub(text) text.
FontSize(10).
Color(color))
page.Spacing(6)
Next
End Sub).
Save(outputPath)

Console.WriteLine($"Created {outputPath}")
End Sub
End Module

How it works​

1. Define the color strings​

The array contains examples of the following accepted formats:

FormatExample inputs
Named colorsred, rebeccapurple
Short RGB hexadecimal#ABC
RGB hexadecimal#AABBCC
RGB channelsrgb(255, 0, 128)
RGB percentagesrgb(100%, 0%, 50%)
Hue, saturation, lightnesshsl(120, 100%, 50%)

RGB channels describe red, green, and blue. HSL describes hue, saturation, and lightness.

2. Set up the document and page​

DocumentBuilder.Create() starts the document and .Title(...) sets its metadata title. .DefaultUnit(LengthUnit.Point) makes unqualified dimensions use points. The page uses Letter paper and 40-point margins, with a 20-point heading above the samples.

3. Draw each color sample​

The loop adds the color string as a black label. It then creates a rectangle measuring 200 by 14 points with .BackgroundColor(color) and a 1-point black border.

The following text element uses .Color(color) to apply the same input to text. Labels stay readable independently of the sample color, and .Spacing(6) separates the examples.

4. Save the PDF​

Path.Combine joins outputRoot with ColorFormats_output.pdf. Make sure the directory exists before calling Run. .Save(path) saves through the fluent builder and overwrites an existing file with the same name. Console.WriteLine prints its path.

Next steps​

Replace the array values with your own colors. See Styling text using CSS for stylesheet examples, Text & formatted text for inline HTML colors, or browse all examples.