QuestPDF/QuestPDF · error · ArgumentException
The dash pattern must contain an even number of elements.
Error message
The dash pattern must contain an even number of elements.
What it means
Thrown by LineDescriptor.LineDashPattern when dashPattern has an odd number of elements. QuestPDF consumes the array as alternating (dash, gap) pairs, so an odd length leaves a dangling dash with no gap; the guard dashPattern.Length % 2 != 0 rejects it with an ArgumentException.
Source
Thrown at src/dotnet/library/QuestPDF/Fluent/LineExtensions.cs:41
Line.Color = color;
return this;
}
/// <summary>
/// Configures a dashed pattern for the line.
/// For example, a pattern of [2, 3] creates a dash of 2 units followed by a gap of 3 units.
/// </summary>
/// <param name="dashPattern">The length of this array must be even.</param>
public LineDescriptor LineDashPattern(float[] dashPattern, Unit unit = Unit.Point)
{
if (dashPattern == null)
throw new ArgumentNullException(nameof(dashPattern), "The dash pattern cannot be null.");
if (dashPattern.Length == 0)
throw new ArgumentException("The dash pattern cannot be empty.", nameof(dashPattern));
if (dashPattern.Length % 2 != 0)
throw new ArgumentException("The dash pattern must contain an even number of elements.", nameof(dashPattern));
Line.DashPattern = dashPattern.Select(x => x.ToPoints(unit)).ToArray();
return this;
}
/// <summary>
/// Applies a linear gradient to a line using the specified colors.
/// </summary>
public LineDescriptor LineGradient(Color[] colors)
{
if (colors == null)
throw new ArgumentNullException(nameof(colors), "The gradient colors cannot be null.");
if (colors.Length == 0)
throw new ArgumentException("The gradient colors cannot be empty.", nameof(colors));
Line.GradientColors = colors;
return this;View on GitHub (pinned to 43ab125596)
Solutions
- Provide pairs: every odd index is a gap. Minimum valid is two elements, e.g. new[] { 2f, 3f }.
- If your data is dash-only, interleave a default gap: pattern.SelectMany(d => new[] { d, gap }).ToArray().
- Add a config-time assertion that pattern.Length % 2 == 0 with a domain-specific message.
Example fix
// before
.Line(l => l.LineDashPattern(new[] { 2f, 3f, 4f })); // 3 elements, odd
// after
.Line(l => l.LineDashPattern(new[] { 2f, 3f, 4f, 3f })); // dash-gap-dash-gap Defensive patterns
Strategy: validation
Validate before calling
if (dashPattern.Length % 2 != 0)
throw new InvalidOperationException($"Dash pattern must have an even count, got {dashPattern.Length}.");
line.LineDashPattern(dashPattern); Type guard
static bool IsEvenLength(float[] p) => p != null && p.Length % 2 == 0;
Prevention
- Document dash patterns as dash-gap-dash-gap to users editing config.
- Assert even length at config load time.
- If collecting dash-only sizes, interleave a gap programmatically.
When it happens
Trigger: Passing new[] { 2f, 3f, 4f } (3 elements) or any odd-length array. Common when a user supplies a flat list of dash lengths and forgets gaps must be interleaved.
Common situations: Config authored as "2,3,4" (intended as three dashes) rather than the required dash-gap-dash-gap form; data binding from a UI that collects dash sizes only.
Related errors
- The dash pattern cannot be empty.
- The gradient colors cannot be empty.
- The dash pattern cannot be null.
- The gradient colors cannot be null.
- The Line thickness cannot be negative.
AI-assisted analysis of QuestPDF/QuestPDF@43ab125596 (2026-08-13).
Data as JSON: /api/errors/399c7d0324b5db31.
Report an issue: GitHub.