{"record":{"id":"3465fcc8161e720c","repo":"dotnet/wpf","slug":"sr-paginatornegativepagenumber","errorCode":null,"errorMessage":"SR.PaginatorNegativePageNumber","messagePattern":"SR\\.PaginatorNegativePageNumber","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Documents/DocumentPaginator.cs","lineNumber":77,"sourceCode":"            GetPageAsync(pageNumber, null);\n        }\n\n        /// <summary>\n        /// Async version of <see cref=\"DocumentPaginator.GetPage\"/>\n        /// </summary>\n        /// <param name=\"pageNumber\">Page number.</param>\n        /// <param name=\"userState\">Unique identifier for the asynchronous task.</param>\n        /// <exception cref=\"ArgumentOutOfRangeException\">\n        /// Throws ArgumentOutOfRangeException if PageNumber is negative.\n        /// </exception>\n        public virtual void GetPageAsync(int pageNumber, object userState)\n        {\n            DocumentPage page;\n\n            // Page number cannot be negative.\n            if (pageNumber < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(pageNumber), SR.PaginatorNegativePageNumber);\n            }\n\n            page = GetPage(pageNumber);\n            OnGetPageCompleted(new GetPageCompletedEventArgs(page, pageNumber, null, false, userState));\n        }\n\n        /// <summary>\n        /// Computes the number of pages of content. IsPageCountValid will be \n        /// True immediately after this is called.\n        /// </summary>\n        /// <remarks>\n        /// If content is modified or PageSize is changed (or any other change \n        /// that causes a repagination) after this method is called, \n        /// IsPageCountValid will likely revert to False.\n        /// </remarks>\n        public virtual void ComputePageCount()\n        {\n            // Force pagination of entire content.","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Documents/DocumentPaginator.cs#L59-L95","documentation":"DocumentPaginator.GetPageAsync throws ArgumentOutOfRangeException when the pageNumber parameter is negative. Page numbers are zero-based; a negative value can never identify a valid page, so the async wrapper validates before calling GetPage.","triggerScenarios":"Calling paginator.GetPageAsync(pageNumber, userState) with a negative pageNumber, typically a value computed from an uninitialized variable, an off-by-one subtraction, or data bound from a saved page index of -1 (no page).","commonSituations":"XPS/FixedDocument printing code that saved -1 as 'no current page' and later passes it to GetPageAsync; index arithmetic like currentPage-1 on page 0; UI bindings without validation.","solutions":["Validate pageNumber >= 0 before calling GetPageAsync","Treat -1 as 'no page' sentinel and skip the call","Clamp with Math.Max(0, pageNumber) where a page is guaranteed to exist"],"exampleFix":"// before\npaginator.GetPageAsync(currentPage - 1, userState);\n// after\nint pageIndex = currentPage - 1;\nif (pageIndex >= 0)\n{\n    paginator.GetPageAsync(pageIndex, userState);\n}","handlingStrategy":"validation","validationCode":"if (pageNumber < 0) throw new ArgumentOutOfRangeException(nameof(pageNumber), \"Page number must be zero or greater.\");","typeGuard":"bool IsValidPage(int pageNumber) => pageNumber >= 0;","tryCatchPattern":"try { await paginator.GetPageAsync(pageNumber, null); }\ncatch (ArgumentOutOfRangeException) { pageNumber = 0; }","preventionTips":["Remember DocumentPaginator page numbers are zero-based","Treat -1 sentinel values ('no page') before calling GetPageAsync","Validate data-bound page indexes in the UI layer"],"tags":["wpf","argumentoutofrangeexception","pagination","documents"],"backgroundTag":"argument-out-of-range","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}