{"record":{"id":"369634c3e776cad1","repo":"QuestPDF/QuestPDF","slug":"the-grid-horizontal-spacing-cannot-be-negative","errorCode":null,"errorMessage":"The Grid horizontal spacing cannot be negative.","messagePattern":"The Grid horizontal spacing cannot be negative\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/dotnet/library/QuestPDF/Fluent/GridExtensions.cs","lineNumber":34,"sourceCode":"        \n        public void Spacing(float value, Unit unit = Unit.Point)\n        {\n            VerticalSpacing(value, unit);\n            HorizontalSpacing(value, unit);\n        }\n        \n        public void VerticalSpacing(float value, Unit unit = Unit.Point)\n        {\n            if (value < 0)\n                throw new ArgumentOutOfRangeException(nameof(value), \"The Grid vertical spacing cannot be negative.\");\n            \n            Grid.VerticalSpacing = value.ToPoints(unit);\n        }\n         \n        public void HorizontalSpacing(float value, Unit unit = Unit.Point)\n        {\n            if (value < 0)\n                throw new ArgumentOutOfRangeException(nameof(value), \"The Grid horizontal spacing cannot be negative.\");\n            \n            Grid.HorizontalSpacing = value.ToPoints(unit);\n        }\n        \n        public void Columns(int value = Grid.DefaultColumnsCount)\n        {\n            if (value < 1)\n                throw new ArgumentOutOfRangeException(nameof(value), \"The Grid columns count cannot be less than 1.\");\n            \n            Grid.ColumnsCount = value;\n        }\n        \n        public void Alignment(HorizontalAlignment alignment)\n        {\n            Grid.Alignment = alignment;\n        }\n\n        public void AlignLeft() => Alignment(HorizontalAlignment.Left);","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/QuestPDF/QuestPDF/blob/43ab12559671556e10a6a0a5afe542c919f48a21/src/dotnet/library/QuestPDF/Fluent/GridExtensions.cs#L16-L52","documentation":"Grid.HorizontalSpacing(value, unit) sets the horizontal gap between grid columns and throws ArgumentOutOfRangeException when value < 0. Negative horizontal spacing is meaningless (columns would overlap). Conversion from the supplied Unit to points happens before assignment; zero is permitted.","triggerScenarios":"Calling descriptor.HorizontalSpacing(value) or Spacing(value) with a negative value.","commonSituations":"Shared spacing variable that goes negative for one axis; theme/config value not bounds-checked; sign error in computed layout metrics.","solutions":["Clamp the value to 0 before calling: `Math.Max(0, value)`.","Sanitize spacing values at the theme/config layer.","Use Spacing(value) with a validated non-negative number to keep both axes consistent."],"exampleFix":"// before\ngrid.HorizontalSpacing(gap); // throws when gap < 0\n\n// after\ngrid.HorizontalSpacing(Math.Max(0, gap));","handlingStrategy":"validation","validationCode":"grid.HorizontalSpacing(Math.Max(0f, value));","typeGuard":"static float SafeSpacing(float v) => v < 0f ? 0f : v;","tryCatchPattern":"try { grid.HorizontalSpacing(value); }\ncatch (ArgumentOutOfRangeException) { grid.HorizontalSpacing(0f); }","preventionTips":["Clamp horizontal spacing to 0.","Bounds-check shared spacing variables before applying.","Sanitize config values once at startup."],"tags":["questpdf","fluent-api","argument-validation","grid","spacing"],"backgroundTag":null,"analyzedSha":"43ab12559671556e10a6a0a5afe542c919f48a21","analyzedAt":"2026-08-13T17:02:41.303Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}