{"record":{"id":"8cb545fbab1f0fca","repo":"QuestPDF/QuestPDF","slug":"the-ensurespace-minimum-height-cannot-be-negative","errorCode":null,"errorMessage":"The EnsureSpace minimum height cannot be negative.","messagePattern":"The EnsureSpace minimum height cannot be negative\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/dotnet/library/QuestPDF/Fluent/ElementExtensions.cs","lineNumber":229,"sourceCode":"        /// If the required space is not available, the content is moved to the next page.\n        /// </para>\n        /// <para>\n        /// This rule applies only to the first page where the content appears. If the content spans multiple pages,\n        /// all subsequent pages are rendered without this restriction. \n        /// </para>\n        /// <para>\n        /// This method is particularly useful for structured elements like tables, where rendering only a small fragment \n        /// at the bottom of a page could negatively impact readability. By ensuring a minimum height, \n        /// you can prevent undesired content fragmentation.\n        /// </para>\n        /// <br />\n        /// <a href=\"https://www.questpdf.com/api-reference/ensure-space.html\">Learn more</a>\n        /// </summary>\n        /// <param name=\"minHeight\">The minimum height, in points, that the element should occupy before a page break.</param>\n        public static IContainer EnsureSpace(this IContainer element, float minHeight = Elements.EnsureSpace.DefaultMinHeight)\n        {\n            if (minHeight < 0)\n                throw new ArgumentOutOfRangeException(nameof(minHeight), \"The EnsureSpace minimum height cannot be negative.\");\n            \n            return element.Element(new EnsureSpace\n            {\n                MinHeight = minHeight\n            });\n        }\n        \n        /// <summary>\n        /// <para>\n        /// Attempts to keep the container's content together on its first page of occurrence.\n        /// If the content does not fit entirely on that page, it is moved to the next page. \n        /// If it spans multiple pages, all subsequent pages are rendered as usual without restriction.\n        /// </para>\n        /// <para>\n        /// This method is useful for ensuring that content remains visually coherent and is not arbitrarily split.\n        /// </para>\n        /// <br />\n        /// <a href=\"https://www.questpdf.com/api-reference/prevent-page-break.html\">Learn more</a>","sourceCodeStart":211,"sourceCodeEnd":247,"githubUrl":"https://github.com/QuestPDF/QuestPDF/blob/43ab12559671556e10a6a0a5afe542c919f48a21/src/dotnet/library/QuestPDF/Fluent/ElementExtensions.cs#L211-L247","documentation":"EnsureSpace(this IContainer, float minHeight) pushes content to the next page when less than minHeight remains, to avoid tiny fragments (e.g. a lone table row at the page bottom). Negative heights are meaningless for that guarantee, so the method throws ArgumentOutOfRangeException when minHeight < 0. Zero is allowed and disables the minimum-height behavior; the default is EnsureSpace.DefaultMinHeight.","triggerScenarios":"Calling .EnsureSpace(minHeight) with a negative float, often from a subtracted or computed value.","commonSituations":"Passing page-margin math that goes negative on small pages; pulling minHeight from config without bounds checking; sign error when subtracting padding.","solutions":["Clamp minHeight to 0 or a sensible positive value before calling: `Math.Max(0, requested)`.","Use the default argument (.EnsureSpace()) when you have no specific minimum.","If the value is computed from page geometry, validate the intermediate calculation rather than trusting it."],"exampleFix":"// before\ncontainer.EnsureSpace(contentHeight - pageHeight); // can be negative\n\n// after\ncontainer.EnsureSpace(Math.Max(0, contentHeight - pageHeight));","handlingStrategy":"validation","validationCode":"float safe = Math.Max(0f, minHeight);\ncontainer.EnsureSpace(safe);","typeGuard":"static float ClampEnsureSpace(float h) => h < 0f ? 0f : h;","tryCatchPattern":"try { container.EnsureSpace(minHeight); }\ncatch (ArgumentOutOfRangeException) {\n    container.EnsureSpace(0f);\n}","preventionTips":["Clamp computed heights to 0 before passing.","Use the default argument when you have no specific minimum.","Validate page-geometry arithmetic at the point of computation."],"tags":["questpdf","fluent-api","argument-validation","ensure-space","paging"],"backgroundTag":null,"analyzedSha":"43ab12559671556e10a6a0a5afe542c919f48a21","analyzedAt":"2026-08-13T17:02:41.303Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}