Struct CssPageSize
The size descriptor of @page (CSS Paged Media 3 section 7.1): auto, one or two lengths, or a page size
name such as A4 or letter with an optional orientation.
public readonly record struct CssPageSize : IEquatable<CssPageSize>
- Implements
- Inherited Members
Examples
CssStyleSheet sheet = CssStyleSheet.Parse("""
@page { size: A4; margin: 2cm; font: 9pt serif;
@bottom-center { content: counter(page) } }
@page :first { margin-top: 5cm; @bottom-center { content: none } }
@page :left { margin-left: 3cm }
@page appendix { size: A4 landscape }
h1.appendix { page: appendix; break-before: right }
""");
CssPageRule first = (CssPageRule)sheet.Rules[1];
CssPageSelector selector = first.Selectors[0]; // :first, specificity (0, 1, 0)
CssPageMarginRule footer = ((CssPageRule)sheet.Rules[0]).MarginRules[0]; // CssMarginBox.BottomCenter
// Cascade the page rules for each page the layout makes, then compute the page context from the root element's
// computed style and each margin box from the page context's.
var cascade = new CssCascade([(sheet, CssOrigin.Author)]);
CssPageStyle pageStyle = cascade.CascadePage(new CssPage(Name: null, IsFirst: false, IsLeft: true));
CssComputedStyle root = cascade.Compute(new CssCascadedStyle(), null);
CssComputedStyle page = cascade.Compute(pageStyle.Page, root);
string? marginLeft = page.GetValueText("margin-left"); // "3cm"
bool isSize = CssPageSize.TryParse(page.GetValueText("size")!, out CssPageSize a4); // 210mm by 297mm
CssComputedStyle box = cascade.Compute(pageStyle.MarginBoxes[CssMarginBox.BottomCenter], page);
string? content = box.GetValueText("content"); // "counter(page)"
string? fontSize = box.GetValueText("font-size"); // "9pt": inherited from the page context
// A named page: the appendix pages turn A4 to landscape.
CssPageStyle appendix = cascade.CascadePage(new CssPage("appendix", IsFirst: false, IsLeft: false));
bool isTurned = CssPageSize.TryParse(appendix.Page.GetValueText("size")!, out CssPageSize turned);
bool landscape = turned.Orientation == CssPageOrientation.Landscape; // true: 297mm by 210mm
Properties
Height
public CssLength? Height { get; init; }
Property Value
IsAuto
Gets whether the user agent picks the size (no width and height given).
public bool IsAuto { get; }
Property Value
Name
Gets the page size name in lowercase, such as a4, or null.
public string? Name { get; init; }
Property Value
Names
Gets the page size names, in lowercase.
public static IEnumerable<string> Names { get; }
Property Value
Orientation
Gets the orientation given. With a size it is already applied to Width and Height; alone it asks the user agent's default size to be turned.
public CssPageOrientation Orientation { get; init; }
Property Value
Width
Gets the page width, or null when the size leaves it to the user agent (auto, or an orientation alone).
public CssLength? Width { get; init; }
Property Value
Methods
ToString()
Formats the size as CSS text, such as a4 landscape or 8.5in 11in.
public override string ToString()
Returns
- string
The text.
TryParse(IReadOnlyList<CssComponentValue>, out CssPageSize)
Parses a size value from component values.
public static bool TryParse(IReadOnlyList<CssComponentValue> values, out CssPageSize size)
Parameters
valuesIReadOnlyList<CssComponentValue>The component values.
sizeCssPageSizeThe page size.
Returns
- bool
True when the values are a valid
sizevalue.
TryParse(string, out CssPageSize)
Parses a size value, such as A4 landscape or 8.5in 11in.
public static bool TryParse(string text, out CssPageSize size)
Parameters
textstringThe CSS text.
sizeCssPageSizeThe page size.
Returns
- bool
True when the text is a valid
sizevalue.