dotnet/wpf · error · ArgumentException

SR.IDPInvalidContentPosition

Error message

SR.IDPInvalidContentPosition

What it means

FixedDocument.GetPageNumber throws ArgumentException with SR.IDPInvalidContentPosition when the supplied IContentPosition is not a FixedTextPointer. GetPageNumber can only map content positions created by this FixedDocument's own text container; any other IContentPosition implementation is invalid.

Solutions

  1. Only pass IContentPosition values that were obtained from the same FixedDocument (via GetContentPositionForElement or similar), which are FixedTextPointer instances.
  2. Guard before calling: if (!(contentPosition is FixedTextPointer)) skip or throw a clearer application-level error.
  3. If working across documents, look up the page via the owning document's APIs rather than reusing foreign content positions.

Example fix

// before
int pageNo = fixedDocument.GetPageNumber(someOtherPointer);

// after
if (someOtherPointer is FixedTextPointer ftp)
{
    int pageNo = fixedDocument.GetPageNumber(ftp);
}
Defensive patterns

Strategy: type-guard

Validate before calling

bool isValidContentPosition = contentPosition is FixedTextPointer;

Type guard

static bool IsFixedTextPointer(System.Windows.Documents.IContentPosition p) => p is FixedTextPointer;

Try / catch

try
{
    int pageNo = fixedDocument.GetPageNumber(contentPosition);
}
catch (ArgumentException ex)
{
    logger.LogWarning(ex, "Content position does not belong to this FixedDocument.");
}

Prevention

When it happens

Trigger: Calling GetPageNumber and passing a content position obtained from a different document type (e.g. a FlowDocument TextPointer), a null-derived cast, or a custom IContentPosition implementation instead of a FixedTextPointer returned by this FixedDocument.

Common situations: Mixing content positions between FixedDocument and FlowDocument viewers; caching a content position from one document and reusing it against another; implementing IContentPosition in custom code.

Understand the failure class

Background: "must be a positive integer", "cannot be empty", "invalid argument": how invalid-argument errors work across open-source libraries — this error's family across 33 libraries.

Related errors


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

Appendix: source

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

                _NotifyGetPageAsyncCompleted(DocumentPage.Missing, pageNumber, null, false, userState);
            }
        }

        /// <summary>
        /// <see cref="DynamicDocumentPaginator.GetPageNumber"/>
        /// </summary>
        /// <exception cref="ArgumentNullException">contentPosition is NULL.</exception>
        /// <exception cref="ArgumentException">ContentPosition does not exist within this element?s tree.</exception>
        internal int GetPageNumber(ContentPosition contentPosition)
        {
//             Dispatcher.VerifyAccess();

            ArgumentNullException.ThrowIfNull(contentPosition);

            FixedTextPointer fixedTextPointer = contentPosition as FixedTextPointer;
            if (fixedTextPointer == null)
            {
                throw new ArgumentException(SR.IDPInvalidContentPosition);
            }

            return fixedTextPointer.FixedTextContainer.GetPageNumber(fixedTextPointer);
        }

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

            ArgumentNullException.ThrowIfNull(userState);

            GetPageAsyncRequest asyncRequest;
            if (_asyncOps.TryGetValue(userState,out asyncRequest))

View on GitHub (pinned to 81131a70a4)