{"record":{"id":"8e849a67de4ae37b","repo":"QuestPDF/QuestPDF","slug":"the-maximum-width-cannot-be-negative","errorCode":null,"errorMessage":"The maximum width cannot be negative.","messagePattern":"The maximum width cannot be negative\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/dotnet/library/QuestPDF/Fluent/ConstrainedExtensions.cs","lineNumber":19,"sourceCode":"﻿using System;\nusing QuestPDF.Elements;\nusing QuestPDF.Infrastructure;\n\nnamespace QuestPDF.Fluent\n{\n    public static class ConstrainedExtensions\n    {\n        #region Width\n        \n        private static IContainer ConstrainedWidth(this IContainer element, float? min = null, float? max = null)\n        {\n            var constrained = element as Constrained ?? new Constrained();\n\n            if (min < 0)\n                throw new ArgumentOutOfRangeException(nameof(min), \"The minimum width cannot be negative.\");\n            \n            if (max < 0)\n                throw new ArgumentOutOfRangeException(nameof(max), \"The maximum width cannot be negative.\");\n            \n            if (min > max)\n                throw new ArgumentOutOfRangeException(nameof(min), \"The minimum width cannot be greater than the maximum width.\");\n            \n            if (min.HasValue)\n                constrained.MinWidth = min;\n            \n            if (max.HasValue)\n                constrained.MaxWidth = max;\n            \n            return element.Element(constrained);\n        }\n        \n        /// <summary>\n        /// Sets the exact width of its content.\n        /// <a href=\"https://www.questpdf.com/api-reference/width.html\">Learn more</a>\n        /// </summary>\n        /// <returns>The container with the specified exact width.</returns>","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/QuestPDF/QuestPDF/blob/43ab12559671556e10a6a0a5afe542c919f48a21/src/dotnet/library/QuestPDF/Fluent/ConstrainedExtensions.cs#L1-L37","documentation":"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.","triggerScenarios":"Calling .MaxWidth(n) or the combined .Width(min, max) with a negative max value, usually from layout math or unit conversion that went negative.","commonSituations":"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).","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."],"exampleFix":"// before\ncontainer.MaxWidth(measured - margin);\n// after\ncontainer.MaxWidth(Math.Max(0, measured - margin));","handlingStrategy":"validation","validationCode":"float maxWidth = Math.Max(0, measured - margin);\ncontainer.MaxWidth(maxWidth);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp width bounds to >= 0.","Use null/omit for 'no maximum' rather than a negative sentinel like -1.","Verify unit conversions feeding the constraint."],"tags":["questpdf","constrained","width","validation","fluent"],"backgroundTag":null,"analyzedSha":"43ab12559671556e10a6a0a5afe542c919f48a21","analyzedAt":"2026-08-13T17:02:41.303Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}