dotnet/wpf · error · ArgumentException

SR.IDPInvalidContentPosition

Error message

SR.IDPInvalidContentPosition

What it means

DocumentSequence's GetPageNumber resolves a ContentPosition back to a page number by splitting the sequence text pointer into a child paginator and child ContentPosition. If the child content position cannot be extracted (null), it throws ArgumentException with SR.IDPInvalidContentPosition — the object is not a content position belonging to this document sequence.

Solutions

  1. Only pass ContentPositions obtained from this same FixedDocumentSequence paginator (e.g. from GetPage(...).ContentPosition)
  2. Re-fetch the ContentPosition after the document is reloaded instead of caching stale ones
  3. Handle ContentPosition.Null explicitly and skip the GetPageNumber call
  4. Use a TextPointer/Logical tree position from the sequence itself when resolving

Example fix

// before
int page = sequencePaginator.GetPageNumber(otherDocPage.ContentPosition);
// after
var seqPage = sequencePaginator.GetPage(i);
int page = sequencePaginator.GetPageNumber(seqPage.ContentPosition);
Defensive patterns

Strategy: type-guard

Validate before calling

if (contentPosition == null || contentPosition == ContentPosition.Null) return -1;

Type guard

static bool IsValidContentPosition(ContentPosition cp) => cp != null && cp != ContentPosition.Null;

Try / catch

try { int page = paginator.GetPageNumber(cp); } catch (ArgumentException) { page = -1; }

Prevention

When it happens

Trigger: Calling GetPageNumber with a ContentPosition from a different document/paginator, a stale or disposed ContentPosition, or ContentPosition.Null obtained from an earlier failed GetPage.

Common situations: Mixing ContentPositions between multiple document viewers/paginators; caching a ContentPosition across document reloads; passing ContentPosition.Null; using a position from a child FixedDocument directly with the sequence paginator.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

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

        internal int GetPageNumber(ContentPosition contentPosition)
        {
            ArgumentNullException.ThrowIfNull(contentPosition);

            // ContentPosition may be only created by DynamicDocumentPaginator.GetObjectPosition or
            // DynamicDocumentPaginator.GetPagePosition.
            // Because of that we are expecting one of 2 types here.
            DynamicDocumentPaginator childPaginator = null;
            ContentPosition childContentPosition = null;
            if (contentPosition is DocumentSequenceTextPointer dsTextPointer)
            {

                childPaginator = GetPaginator(dsTextPointer.ChildBlock.DocRef);
                childContentPosition = dsTextPointer.ChildPointer as ContentPosition;
            }

            if (childContentPosition == null)
            {
                throw new ArgumentException(SR.IDPInvalidContentPosition);
            }

            int childPageNumber = childPaginator.GetPageNumber(childContentPosition);
            int pageNumber;
            _SynthesizeGlobalPageNumber(childPaginator, childPageNumber, out pageNumber);
            return pageNumber;
        }

        /// <summary>
        /// <see cref="System.Windows.Documents.DocumentPaginator.CancelAsync"/>
        /// </summary>
        internal void CancelAsync(object userState)
        {
            DocumentsTrace.FixedFormat.IDF.Trace($"IDP.GetPageAsyncCancel([{userState}])");

            ArgumentNullException.ThrowIfNull(userState);

            if (_asyncOps.ContainsKey(userState))

View on GitHub (pinned to 81131a70a4)