{"record":{"id":"886de86f13747c3c","repo":"QuestPDF/QuestPDF","slug":"the-line-thickness-cannot-be-negative","errorCode":null,"errorMessage":"The Line thickness cannot be negative.","messagePattern":"The Line thickness cannot be negative\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/dotnet/library/QuestPDF/Fluent/LineExtensions.cs","lineNumber":68,"sourceCode":"        public LineDescriptor LineGradient(Color[] colors)\n        {\n            if (colors == null)\n                throw new ArgumentNullException(nameof(colors), \"The gradient colors cannot be null.\");\n\n            if (colors.Length == 0)\n                throw new ArgumentException(\"The gradient colors cannot be empty.\", nameof(colors));\n\n            Line.GradientColors = colors;\n            return this;\n        }\n    }\n    \n    public static class LineExtensions\n    {\n        private static LineDescriptor Line(this IContainer element, LineType type, float thickness)\n        {\n            if (thickness < 0)\n                throw new ArgumentOutOfRangeException(nameof(thickness), \"The Line thickness cannot be negative.\");\n            \n            var descriptor = new LineDescriptor();\n            descriptor.Line.Thickness = thickness;\n            descriptor.Line.Type = type;\n            \n            element.Element(descriptor.Line);\n            return descriptor;\n        }\n        \n        /// <summary>\n        /// Renders a vertical line with a specified thickness.\n        /// <a href=\"https://www.questpdf.com/api-reference/line.html\">Learn more</a>\n        /// </summary>\n        /// <remarks>\n        /// The line is not just a visual element; it occupies actual space within the document.\n        /// </remarks>\n        /// <returns>A descriptor to modify line attributes.</returns>\n        public static LineDescriptor LineVertical(this IContainer element, float thickness, Unit unit = Unit.Point)","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/QuestPDF/QuestPDF/blob/43ab12559671556e10a6a0a5afe542c919f48a21/src/dotnet/library/QuestPDF/Fluent/LineExtensions.cs#L50-L86","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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)."],"exampleFix":"// before\n.Line(thickness: available - used); // can be negative\n\n// after\n.Line(thickness: Math.Max(0, available - used));","handlingStrategy":"validation","validationCode":"thickness = Math.Max(0, thickness);\ncontainer.Line(thickness);","typeGuard":"static bool IsNonNegativeThickness(float t) => t >= 0;","tryCatchPattern":null,"preventionTips":["Clamp computed thickness with Math.Max(0, value).","Double-check unit conversions producing the thickness.","Validate source measurements are non-negative before deriving thickness."],"tags":["questpdf","argument-out-of-range","validation","line","csharp"],"backgroundTag":null,"analyzedSha":"43ab12559671556e10a6a0a5afe542c919f48a21","analyzedAt":"2026-08-13T17:02:41.303Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}