{"record":{"id":"eaa7ecc714147147","repo":"dotnet/wpf","slug":"sr-idpnegativepagenumber-fixeddocument","errorCode":null,"errorMessage":"SR.IDPNegativePageNumber","messagePattern":"SR\\.IDPNegativePageNumber","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedDocument.cs","lineNumber":323,"sourceCode":"\n        /// <summary>\n        /// <see cref=\"System.Windows.Documents.DocumentPaginator.GetPage(int)\"/>\n        /// </summary>\n        /// <exception cref=\"ArgumentOutOfRangeException\">pageNumber is less than zero.</exception>\n        /// <param name=\"pageNumber\">\n        /// The page number.\n        /// </param>\n        internal DocumentPage GetPage(int pageNumber)\n        {\n            DocumentsTrace.FixedFormat.IDF.Trace($\"IDP.GetPage({pageNumber})\");\n\n            // Make sure that the call is in the right context.\n//             Dispatcher.VerifyAccess();\n\n            // Page number cannot be negative.\n            if (pageNumber < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(pageNumber), SR.IDPNegativePageNumber);\n            }\n\n            if (pageNumber < Pages.Count)\n            {\n                //\n                // If we are not out of bound, try next page\n                //\n                FixedPage page = SyncGetPage(pageNumber, false/*forceReload*/);\n\n                if (page == null)\n                {\n                    return DocumentPage.Missing;\n                }\n\n                Debug.Assert(page != null);\n                Size fixedSize = ComputePageSize(page);\n\n                // Always measure with fixed size instead of using constraint","sourceCodeStart":305,"sourceCodeEnd":341,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedDocument.cs#L305-L341","documentation":"This ArgumentOutOfRangeException is thrown by FixedDocument.GetPage when pageNumber is negative. Document page indices are zero-based; negative values are meaningless, so the API rejects them explicitly with the IDPNegativePageNumber resource string.","triggerScenarios":"Calling GetPage(-1) or GetPage(someUninitializedIndex) where an index variable defaulted to -1 or was computed from a failed lookup.","commonSituations":"Using a 'not found = -1' result (e.g. from IndexOf) directly as a page number; off-by-one arithmetic in paging code; IDocumentPaginatorSource interactions with sentinel indices.","solutions":["Validate pageNumber >= 0 before calling GetPage","Replace sentinel -1 values with explicit checks before requesting a page","Clamp or recompute indices derived from searches (IndexOf etc.)","Catch ArgumentOutOfRangeException around GetPage when index origin is uncertain"],"exampleFix":"// before\nint idx = pages.IndexOf(page);\nvar docPage = fixedDocument.GetPage(idx); // idx may be -1\n// after\nint idx = pages.IndexOf(page);\nvar docPage = idx >= 0 ? fixedDocument.GetPage(idx) : null;","handlingStrategy":"validation","validationCode":"if (pageNumber < 0) throw new ArgumentException(\"pageNumber must be non-negative\");","typeGuard":null,"tryCatchPattern":"try { var page = fixedDocument.GetPage(n); } catch (ArgumentOutOfRangeException) { page = null; }","preventionTips":["Treat page numbers as zero-based, non-negative","Never pass IndexOf-style -1 sentinels directly to GetPage","Validate indices computed from searches or arithmetic before calling"],"tags":["wpf","argumentoutofrangeexception","xps-fixeddocument","pagination","negative-index"],"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-22T01:17:13.364Z"}