QuestPDF/QuestPDF · error · ArgumentOutOfRangeException

The maximum width cannot be negative.

Error message

The maximum width cannot be negative.

What it means

ConstrainedExtensions.ConstrainedWidth rejects a negative maximum width. A negative max bound is physically meaningless, so the validator throws ArgumentOutOfRangeException on max < 0 before applying the constraint.

Source

Thrown at src/dotnet/library/QuestPDF/Fluent/ConstrainedExtensions.cs:19

using System;
using QuestPDF.Elements;
using QuestPDF.Infrastructure;

namespace QuestPDF.Fluent
{
    public static class ConstrainedExtensions
    {
        #region Width
        
        private static IContainer ConstrainedWidth(this IContainer element, float? min = null, float? max = null)
        {
            var constrained = element as Constrained ?? new Constrained();

            if (min < 0)
                throw new ArgumentOutOfRangeException(nameof(min), "The minimum width cannot be negative.");
            
            if (max < 0)
                throw new ArgumentOutOfRangeException(nameof(max), "The maximum width cannot be negative.");
            
            if (min > max)
                throw new ArgumentOutOfRangeException(nameof(min), "The minimum width cannot be greater than the maximum width.");
            
            if (min.HasValue)
                constrained.MinWidth = min;
            
            if (max.HasValue)
                constrained.MaxWidth = max;
            
            return element.Element(constrained);
        }
        
        /// <summary>
        /// Sets the exact width of its content.
        /// <a href="https://www.questpdf.com/api-reference/width.html">Learn more</a>
        /// </summary>
        /// <returns>The container with the specified exact width.</returns>

View on GitHub (pinned to 43ab125596)

Solutions

  1. Clamp max to >= 0 with Math.Max(0, max) before calling the constraint.
  2. If you mean 'no maximum', omit the argument or pass null rather than a negative sentinel.
  3. Verify unit conversions producing the value.

Example fix

// before
container.MaxWidth(measured - margin);
// after
container.MaxWidth(Math.Max(0, measured - margin));
Defensive patterns

Strategy: validation

Validate before calling

float maxWidth = Math.Max(0, measured - margin);
container.MaxWidth(maxWidth);

Prevention

When it happens

Trigger: Calling .MaxWidth(n) or the combined .Width(min, max) with a negative max value, usually from layout math or unit conversion that went negative.

Common situations: Computed maximums that subtract too much; inverted unit conversions; passing a sentinel like -1 intended to mean 'unbounded' (QuestPDF uses null for that, not negatives).

Related errors


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