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

  1. Clamp thickness to 0 or a minimum positive value: Math.Max(0, computed).
  2. Use the correct unit and verify the source measurement is non-negative before calling.
  3. 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

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


AI-assisted analysis of QuestPDF/QuestPDF@43ab125596 (2026-08-13). Data as JSON: /api/errors/886de86f13747c3c. Report an issue: GitHub.