dotnet/wpf · error · ArgumentOutOfRangeException

SR.IDPNegativePageNumber

Error message

SR.IDPNegativePageNumber

What it means

DocumentSequenceDocumentPage's GetPage validates that the requested global page number is not negative and throws ArgumentOutOfRangeException with SR.IDPNegativePageNumber. Page numbers are zero-based across the whole FixedDocumentSequence.

Solutions

  1. Clamp or validate the page number is >= 0 before calling GetPage
  2. Guard state that may hold -1/uninitialized values (e.g. 'no page selected')
  3. Validate user input page numbers before use
  4. Use DocumentPaginator.PageCount to bound computed page indices

Example fix

// before
var page = paginator.GetPage(selectedIndex - 1);
// after
if (selectedIndex > 0)
    var page = paginator.GetPage(selectedIndex - 1);
Defensive patterns

Strategy: validation

Validate before calling

if (pageNumber < 0 || pageNumber >= paginator.PageCount) return null;

Try / catch

try { var page = paginator.GetPage(pageNumber); } catch (ArgumentOutOfRangeException) { page = null; }

Prevention

When it happens

Trigger: Calling GetPage(pageNumber) on the FixedDocumentSequence's paginator (e.g. via DocumentViewer's docPage path) with a negative int, typically from a bad computation like index-1 before bounds checks or an uninitialized counter.

Common situations: Off-by-one arithmetic computing the page to jump to; storing page indices in state and passing an uninitialized (-1) value; user-supplied page number parsed without validation.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentSequence.cs:241

        #endregion IDocumentPaginatorSource Members

        #region DocumentPaginator

        /// <summary>
        /// <see cref="System.Windows.Documents.DocumentPaginator.GetPage"/>
        /// </summary>
        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);
            }

            DocumentPage innerDP = null;
            DynamicDocumentPaginator innerPaginator;
            int innerPageNumber;
            // Find the appropriate inner IDF and BR
            if (TranslatePageNumber(pageNumber, out innerPaginator, out innerPageNumber))
            {
                innerDP = innerPaginator.GetPage(innerPageNumber);
                Debug.Assert(innerDP != null);

                // Now warp inner DP and return it
                return new FixedDocumentSequenceDocumentPage(this, innerPaginator, innerDP);
            }
            return DocumentPage.Missing;
        }

        /// <summary>

View on GitHub (pinned to 81131a70a4)