QuestPDF/QuestPDF · error · ArgumentOutOfRangeException
The Line thickness cannot be negative.
Error message
The Line thickness cannot be negative.
What it means
Thrown by the internal Line(this IContainer, LineType, float thickness) helper when thickness is negative. A line stroke must have a non-negative width; negative values have no physical meaning in the PDF coordinate model and are rejected with an ArgumentOutOfRangeException.
Source
Thrown at src/dotnet/library/QuestPDF/Fluent/LineExtensions.cs:68
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;
}
}
public static class LineExtensions
{
private static LineDescriptor Line(this IContainer element, LineType type, float thickness)
{
if (thickness < 0)
throw new ArgumentOutOfRangeException(nameof(thickness), "The Line thickness cannot be negative.");
var descriptor = new LineDescriptor();
descriptor.Line.Thickness = thickness;
descriptor.Line.Type = type;
element.Element(descriptor.Line);
return descriptor;
}
/// <summary>
/// Renders a vertical line with a specified thickness.
/// <a href="https://www.questpdf.com/api-reference/line.html">Learn more</a>
/// </summary>
/// <remarks>
/// The line is not just a visual element; it occupies actual space within the document.
/// </remarks>
/// <returns>A descriptor to modify line attributes.</returns>
public static LineDescriptor LineVertical(this IContainer element, float thickness, Unit unit = Unit.Point)View on GitHub (pinned to 43ab125596)
Solutions
- Clamp thickness to 0 or a minimum positive value: Math.Max(0, computed).
- Use the correct unit and verify the source measurement is non-negative before calling.
- If zero thickness is intended, pass 0 (it is allowed; only negatives throw).
Example fix
// before .Line(thickness: available - used); // can be negative // after .Line(thickness: Math.Max(0, available - used));
Defensive patterns
Strategy: validation
Validate before calling
thickness = Math.Max(0, thickness); container.Line(thickness);
Type guard
static bool IsNonNegativeThickness(float t) => t >= 0;
Prevention
- Clamp computed thickness with Math.Max(0, value).
- Double-check unit conversions producing the thickness.
- Validate source measurements are non-negative before deriving thickness.
When it happens
Trigger: Calling .Line(thickness: -1), .VerticalLine(-2), or any Line API whose thickness argument evaluates to a negative float (often a subtracted margin or an inverted measurement). Guard: if (thickness < 0).
Common situations: Computing thickness from a subtraction (e.g. elementWidth - contentWidth) that goes negative under small content; reading thickness from config where a sign was mistyped; unit conversion bug producing a negative value.
Related errors
- The dash pattern cannot be null.
- The dash pattern cannot be empty.
- The dash pattern must contain an even number of elements.
- The gradient colors cannot be null.
- The gradient colors cannot be empty.
AI-assisted analysis of QuestPDF/QuestPDF@43ab125596 (2026-08-13).
Data as JSON: /api/errors/886de86f13747c3c.
Report an issue: GitHub.