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
- Clamp max to >= 0 with Math.Max(0, max) before calling the constraint.
- If you mean 'no maximum', omit the argument or pass null rather than a negative sentinel.
- 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
- Clamp width bounds to >= 0.
- Use null/omit for 'no maximum' rather than a negative sentinel like -1.
- Verify unit conversions feeding the constraint.
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
- The minimum width cannot be negative.
- The minimum width cannot be greater than the maximum width.
- The minimum height cannot be negative.
- The maximum height cannot be negative.
- The minimum height cannot be greater than the maximum height
AI-assisted analysis of QuestPDF/QuestPDF@43ab125596 (2026-08-13).
Data as JSON: /api/errors/8e849a67de4ae37b.
Report an issue: GitHub.