dotnet/wpf · error · ArgumentOutOfRangeException

SR.IDPNegativePageNumber

Error message

SR.IDPNegativePageNumber

What it means

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.

Solutions

  1. Validate pageNumber >= 0 before calling GetPage
  2. Replace sentinel -1 values with explicit checks before requesting a page
  3. Clamp or recompute indices derived from searches (IndexOf etc.)
  4. Catch ArgumentOutOfRangeException around GetPage when index origin is uncertain

Example fix

// before
int idx = pages.IndexOf(page);
var docPage = fixedDocument.GetPage(idx); // idx may be -1
// after
int idx = pages.IndexOf(page);
var docPage = idx >= 0 ? fixedDocument.GetPage(idx) : null;
Defensive patterns

Strategy: validation

Validate before calling

if (pageNumber < 0) throw new ArgumentException("pageNumber must be non-negative");

Try / catch

try { var page = fixedDocument.GetPage(n); } catch (ArgumentOutOfRangeException) { page = null; }

Prevention

When it happens

Trigger: Calling GetPage(-1) or GetPage(someUninitializedIndex) where an index variable defaulted to -1 or was computed from a failed lookup.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/eaa7ecc714147147. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedDocument.cs:323

        /// <summary>
        /// <see cref="System.Windows.Documents.DocumentPaginator.GetPage(int)"/>
        /// </summary>
        /// <exception cref="ArgumentOutOfRangeException">pageNumber is less than zero.</exception>
        /// <param name="pageNumber">
        /// The page number.
        /// </param>
        internal DocumentPage GetPage(int pageNumber)
        {
            DocumentsTrace.FixedFormat.IDF.Trace($"IDP.GetPage({pageNumber})");

            // Make sure that the call is in the right context.
//             Dispatcher.VerifyAccess();

            // Page number cannot be negative.
            if (pageNumber < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(pageNumber), SR.IDPNegativePageNumber);
            }

            if (pageNumber < Pages.Count)
            {
                //
                // If we are not out of bound, try next page
                //
                FixedPage page = SyncGetPage(pageNumber, false/*forceReload*/);

                if (page == null)
                {
                    return DocumentPage.Missing;
                }

                Debug.Assert(page != null);
                Size fixedSize = ComputePageSize(page);

                // Always measure with fixed size instead of using constraint

View on GitHub (pinned to 81131a70a4)