QuestPDF/QuestPDF · error · ArgumentOutOfRangeException
The Grid horizontal spacing cannot be negative.
Error message
The Grid horizontal spacing cannot be negative.
What it means
Grid.HorizontalSpacing(value, unit) sets the horizontal gap between grid columns and throws ArgumentOutOfRangeException when value < 0. Negative horizontal spacing is meaningless (columns would overlap). Conversion from the supplied Unit to points happens before assignment; zero is permitted.
Source
Thrown at src/dotnet/library/QuestPDF/Fluent/GridExtensions.cs:34
public void Spacing(float value, Unit unit = Unit.Point)
{
VerticalSpacing(value, unit);
HorizontalSpacing(value, unit);
}
public void VerticalSpacing(float value, Unit unit = Unit.Point)
{
if (value < 0)
throw new ArgumentOutOfRangeException(nameof(value), "The Grid vertical spacing cannot be negative.");
Grid.VerticalSpacing = value.ToPoints(unit);
}
public void HorizontalSpacing(float value, Unit unit = Unit.Point)
{
if (value < 0)
throw new ArgumentOutOfRangeException(nameof(value), "The Grid horizontal spacing cannot be negative.");
Grid.HorizontalSpacing = value.ToPoints(unit);
}
public void Columns(int value = Grid.DefaultColumnsCount)
{
if (value < 1)
throw new ArgumentOutOfRangeException(nameof(value), "The Grid columns count cannot be less than 1.");
Grid.ColumnsCount = value;
}
public void Alignment(HorizontalAlignment alignment)
{
Grid.Alignment = alignment;
}
public void AlignLeft() => Alignment(HorizontalAlignment.Left);View on GitHub (pinned to 43ab125596)
Solutions
- Clamp the value to 0 before calling: `Math.Max(0, value)`.
- Sanitize spacing values at the theme/config layer.
- Use Spacing(value) with a validated non-negative number to keep both axes consistent.
Example fix
// before grid.HorizontalSpacing(gap); // throws when gap < 0 // after grid.HorizontalSpacing(Math.Max(0, gap));
Defensive patterns
Strategy: validation
Validate before calling
grid.HorizontalSpacing(Math.Max(0f, value));
Type guard
static float SafeSpacing(float v) => v < 0f ? 0f : v;
Try / catch
try { grid.HorizontalSpacing(value); }
catch (ArgumentOutOfRangeException) { grid.HorizontalSpacing(0f); } Prevention
- Clamp horizontal spacing to 0.
- Bounds-check shared spacing variables before applying.
- Sanitize config values once at startup.
When it happens
Trigger: Calling descriptor.HorizontalSpacing(value) or Spacing(value) with a negative value.
Common situations: Shared spacing variable that goes negative for one axis; theme/config value not bounds-checked; sign error in computed layout metrics.
Related errors
- The Grid vertical spacing cannot be negative.
- The Grid columns count cannot be less than 1.
- The Grid item cannot span less than 1 column.
- The Inlined vertical spacing cannot be negative.
- The Inlined horizontal spacing cannot be negative.
AI-assisted analysis of QuestPDF/QuestPDF@43ab125596 (2026-08-13).
Data as JSON: /api/errors/369634c3e776cad1.
Report an issue: GitHub.