Skip to main content

Styling text using CSS

Styling text using CSS

Shared CSS gives a document a consistent look while letting individual pages and elements vary where needed. Style text at document, page, and element levels, and apply CSS to embedded HTML. This example follows StylingTextUsingCss.cs and shows how defaults and overrides work together.

Apply document defaults, override them on a page, and style individual text elements.

Fluent recipe / 05Styling text using CSSApply document, page, and element CSS styles to text and embedded HTML.

What you’ll create​

  • A document with automatic text defaults and a class color override.
  • A two-page document comparing document styles with page overrides.
  • A document showing an element color override between two page-styled text elements.
  • A document with HTML spans styled by a selector, a class, and inline CSS.

Each example uses A5 pages with margins of 15 and saves a separate PDF in your chosen output directory:

MethodOutput file
DocumentLevelStyleStylingTextUsingCss_document_output.pdf
PageLevelStyleStylingTextUsingCss_page_output.pdf
ElementLevelStyleStylingTextUsingCss_element_output.pdf
EmbeddedHtmlClassStyleStylingTextUsingCss_html_output.pdf

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 four styling examples and saves each document to an existing output directory.

using DynamicPDF.Flow;

namespace FluentExamples.Examples;

internal static class StylingTextUsingCss
{
public static void Run(string outputRoot)
{
DocumentLevelStyle(outputRoot);
PageLevelStyle(outputRoot);
ElementLevelStyle(outputRoot);
EmbeddedHtmlClassStyle(outputRoot);
}

// Uses the document's text selector to set blue, 24pt
// Helvetica defaults without requiring a class. On the
// same page, the sample class changes one element to green;
// the unclassed element keeps the document defaults.
private static void DocumentLevelStyle(string outputDir)
{
var path = Path.Combine(outputDir,
"StylingTextUsingCss_document_output.pdf");

DocumentBuilder.Create()
.Title("Document styles")
.Style("""
text {
font-family: 'Helvetica';
font-size: 24pt;
color: blue;
}
.sample {
color: green;
}
""")
.Pages(page => page
.Size(PageSize.A5)
.Margins(15)
.Text("Page 1: document green", "sample")
.Text("Page 1: document blue"))
.Save(path);

Console.WriteLine($"Created {path}");
}

// Uses document styles for green, 24pt Helvetica on page 1.
// Page 2 overrides the text size and sample class color
// with 18pt and red, while retaining the document font.
private static void PageLevelStyle(string outputDir)
{
var path = Path.Combine(outputDir,
"StylingTextUsingCss_page_output.pdf");

DocumentBuilder.Create()
.Title("Page styles override document styles")
.Style("""
text {
font-size: 24pt;
}
.sample {
font-family: 'Helvetica';
color: green;
}
""")
.Pages(page => page
.Size(PageSize.A5)
.Margins(15)
.Text("Page 1: document green, 24pt",
"sample"))
.Pages(page => page
.Size(PageSize.A5)
.Margins(15)
.Style("""
text {
font-size: 18pt;
}
.sample {
color: red;
}
""")
.Text("Page 2: page red, 18pt", "sample"))
.Save(path);

Console.WriteLine($"Created {path}");
}

// Overrides the document's green sample class with page red.
// A direct Text style changes only the middle element to
// blue; the next stays red. All retain 24pt Helvetica.
private static void ElementLevelStyle(string outputDir)
{
var path = Path.Combine(outputDir,
"StylingTextUsingCss_element_output.pdf");

DocumentBuilder.Create()
.Title("Element styles override page styles")
.Style("""
.sample {
font-family: 'Helvetica';
font-size: 24pt;
color: green;
}
""")
.Pages(page => page
.Size(PageSize.A5)
.Margins(15)
.Style(".sample { color: red; }")
.Text("Page style: red", "sample")
.Text("Element style: blue", text => text
.Class("sample")
.Style("color: blue;"))
.Text("Next element: page red", "sample"))
.Save(path);

Console.WriteLine($"Created {path}");
}

// Styles spans with a document selector: purple and 18pt.
// A class adds bold and blue; inline CSS changes blue to
// green while retaining the bold weight and 18pt size.
private static void EmbeddedHtmlClassStyle(string outputDir)
{
var path = Path.Combine(outputDir,
"StylingTextUsingCss_html_output.pdf");

DocumentBuilder.Create()
.Title("Embedded HTML styles")
.Style("""
.doc-bold-text {
font-weight: bold;
color: blue;
}
span {
color: purple;
font-size: 18pt;
}
""")
.Pages(page => page
.Size(PageSize.A5)
.Margins(15)
.Text("Selector: <span>purple text.</span>")
.Text("Class: "
+ "<span class='doc-bold-text'>"
+ "blue bold text.</span>")
.Text("Inline override: "
+ "<span class='doc-bold-text' "
+ "style='color: green;'>"
+ "green bold text.</span>"))
.Save(path);

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

Friend Module StylingTextUsingCss
Public Sub Run(outputRoot As String)
DocumentLevelStyle(outputRoot)
PageLevelStyle(outputRoot)
ElementLevelStyle(outputRoot)
EmbeddedHtmlClassStyle(outputRoot)
End Sub

Private Sub DocumentLevelStyle(outputDir As String)
Dim outputPath = Path.Combine(outputDir,
"StylingTextUsingCss_document_output.pdf")

DocumentBuilder.Create().
Title("Document styles").
Style("text {
font-family: 'Helvetica';
font-size: 24pt;
color: blue;
}
.sample {
color: green;
}").
Pages(Sub(page) page.
Size(PageSize.A5).
Margins(15).
Text("Page 1: document green", "sample").
Text("Page 1: document blue")).
Save(outputPath)

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

Private Sub PageLevelStyle(outputDir As String)
Dim outputPath = Path.Combine(outputDir,
"StylingTextUsingCss_page_output.pdf")

DocumentBuilder.Create().
Title("Page styles override document styles").
Style("text {
font-size: 24pt;
}
.sample {
font-family: 'Helvetica';
color: green;
}").
Pages(Sub(page) page.
Size(PageSize.A5).
Margins(15).
Text("Page 1: document green, 24pt",
"sample")).
Pages(Sub(page) page.
Size(PageSize.A5).
Margins(15).
Style("text {
font-size: 18pt;
}
.sample {
color: red;
}").
Text("Page 2: page red, 18pt", "sample")).
Save(outputPath)

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

Private Sub ElementLevelStyle(outputDir As String)
Dim outputPath = Path.Combine(outputDir,
"StylingTextUsingCss_element_output.pdf")

DocumentBuilder.Create().
Title("Element styles override page styles").
Style(".sample {
font-family: 'Helvetica';
font-size: 24pt;
color: green;
}").
Pages(Sub(page) page.
Size(PageSize.A5).
Margins(15).
Style(".sample { color: red; }").
Text("Page style: red", "sample").
Text("Element style: blue", Sub(text) text.
Class("sample").
Style("color: blue;")).
Text("Next element: page red", "sample")).
Save(outputPath)

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

Private Sub EmbeddedHtmlClassStyle(outputDir As String)
Dim outputPath = Path.Combine(outputDir,
"StylingTextUsingCss_html_output.pdf")

DocumentBuilder.Create().
Title("Embedded HTML styles").
Style(".doc-bold-text {
font-weight: bold;
color: blue;
}
span {
color: purple;
font-size: 18pt;
}").
Pages(Sub(page) page.
Size(PageSize.A5).
Margins(15).
Text("Selector: <span>purple text.</span>").
Text("Class: " &
"<span class='doc-bold-text'>" &
"blue bold text.</span>").
Text("Inline override: " &
"<span class='doc-bold-text' " &
"style='color: green;'>" &
"green bold text.</span>")).
Save(outputPath)

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

How it works​

1. Choose where to save the PDFs​

Run calls the four example methods in order. Each method uses Path.Combine to construct its output path under outputRoot. Make sure that directory exists. Each .Save(path) saves through the fluent builder, overwrites an existing file with the same name, and is followed by a printed output path.

2. Apply automatic document defaults​

DocumentLevelStyle defines a text rule on DocumentBuilder before adding pages. It applies 24-point Helvetica and a blue text color automatically, without requiring a class.

The .sample rule changes only the color to green. .Text("Page 1: document green", "sample") uses that class while retaining the default font and size. Both text elements are on the same page. The second text element displays the literal label Page 1: document blue and uses the document's blue color.

Password dialog asking for the Document Open Password for the protected PDF.

3. Override document styles on a page​

PageLevelStyle defines a document-wide text rule with a 24-point font size and a .sample class with green Helvetica text. Page 1 uses these document styles.

Page 2 supplies its own .Style(...) stylesheet using the same selectors. Its text rule changes the size to 18 points, and its .sample rule changes the color to red. Helvetica remains in effect because the page does not replace the font family. These overrides apply to Page 2.

4. Override a page style on one text element​

ElementLevelStyle defines .sample at document level with green, 24-point Helvetica text. The page overrides the class color with red.

The first and third text elements use the page's red color. The middle element also uses .Class("sample"), then applies .Style("color: blue;") directly on its TextElementBuilder. This element is blue while retaining the font and size; its inline style does not change the following element.

Password dialog asking for the Document Open Password for the protected PDF.

5. Style embedded HTML​

EmbeddedHtmlClassStyle defines a span selector with purple, 18-point text and a .doc-bold-text class with bold, blue text.

The first HTML span uses the selector's purple color. The second uses the class to become blue and bold. The third uses the same class plus style='color: green;', overriding the class color while retaining bold text and the selector's 18-point size.

Password dialog asking for the Document Open Password for the protected PDF.

Next steps​

Change the selectors, classes, and inline styles to compare their effects. Use * for defaults across element types, or text to target text elements. Explore Create a PDF for document-wide CSS, Lists for styling list content, or browse all examples.