{"record":{"id":"49d8f2c67cdae819","repo":"dotnet/wpf","slug":"sr-idpinvalidcontentposition-fixeddocument","errorCode":null,"errorMessage":"SR.IDPInvalidContentPosition","messagePattern":"SR\\.IDPInvalidContentPosition","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedDocument.cs","lineNumber":404,"sourceCode":"                _NotifyGetPageAsyncCompleted(DocumentPage.Missing, pageNumber, null, false, userState);\n            }\n        }\n\n        /// <summary>\n        /// <see cref=\"DynamicDocumentPaginator.GetPageNumber\"/>\n        /// </summary>\n        /// <exception cref=\"ArgumentNullException\">contentPosition is NULL.</exception>\n        /// <exception cref=\"ArgumentException\">ContentPosition does not exist within this element?s tree.</exception>\n        internal int GetPageNumber(ContentPosition contentPosition)\n        {\n//             Dispatcher.VerifyAccess();\n\n            ArgumentNullException.ThrowIfNull(contentPosition);\n\n            FixedTextPointer fixedTextPointer = contentPosition as FixedTextPointer;\n            if (fixedTextPointer == null)\n            {\n                throw new ArgumentException(SR.IDPInvalidContentPosition);\n            }\n\n            return fixedTextPointer.FixedTextContainer.GetPageNumber(fixedTextPointer);\n        }\n\n        /// <summary>\n        /// <see cref=\"System.Windows.Documents.DocumentPaginator.CancelAsync\"/>\n        /// </summary>\n        /// <exception cref=\"ArgumentNullException\">userState is NULL.</exception>\n        internal void CancelAsync(object userState)\n        {\n            DocumentsTrace.FixedFormat.IDF.Trace($\"IDP.GetPageAsyncCancel([{userState}])\");\n//             Dispatcher.VerifyAccess();\n\n            ArgumentNullException.ThrowIfNull(userState);\n\n            GetPageAsyncRequest asyncRequest;\n            if (_asyncOps.TryGetValue(userState,out asyncRequest))","sourceCodeStart":386,"sourceCodeEnd":422,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedDocument.cs#L386-L422","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Only pass IContentPosition values that were obtained from the same FixedDocument (via GetContentPositionForElement or similar), which are FixedTextPointer instances.","Guard before calling: if (!(contentPosition is FixedTextPointer)) skip or throw a clearer application-level error.","If working across documents, look up the page via the owning document's APIs rather than reusing foreign content positions."],"exampleFix":"// before\nint pageNo = fixedDocument.GetPageNumber(someOtherPointer);\n\n// after\nif (someOtherPointer is FixedTextPointer ftp)\n{\n    int pageNo = fixedDocument.GetPageNumber(ftp);\n}","handlingStrategy":"type-guard","validationCode":"bool isValidContentPosition = contentPosition is FixedTextPointer;","typeGuard":"static bool IsFixedTextPointer(System.Windows.Documents.IContentPosition p) => p is FixedTextPointer;","tryCatchPattern":"try\n{\n    int pageNo = fixedDocument.GetPageNumber(contentPosition);\n}\ncatch (ArgumentException ex)\n{\n    logger.LogWarning(ex, \"Content position does not belong to this FixedDocument.\");\n}","preventionTips":["Only obtain content positions from the same FixedDocument instance you query.","Never share IContentPosition values between FixedDocument and FlowDocument objects.","Do not implement IContentPosition yourself for use with FixedDocument APIs."],"tags":["wpf","invalid-argument","fixeddocument","type-mismatch"],"backgroundTag":"invalid-argument","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"}