{"record":{"id":"0d35123ca227dfe4","repo":"dotnet/wpf","slug":"sr-invalidselectionpages","errorCode":null,"errorMessage":"SR.InvalidSelectionPages","messagePattern":"SR\\.InvalidSelectionPages","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/AnnotationHelper.cs","lineNumber":597,"sourceCode":"            }\n\n            bool allPagesVisible = true;\n\n            ITextSelection selection = GetTextSelection((FrameworkElement)service.Root);\n            Invariant.Assert(selection != null, \"TextSelection is null\");\n            int selStartPage = 0, selEndPage = 0;\n\n            if(viewer != null)\n            {\n                //if this is a DocumentViewerBase check the selection pages\n                TextSelectionHelper.GetPointerPage(selection.Start, out selStartPage);\n                TextSelectionHelper.GetPointerPage(selection.End, out selEndPage);\n\n                // If either page cannot be found, the selection we are trying to anchor to\n                // is invalid.  This can happen if the selection was created programmatically\n                // for TextPointers that don't have pages because pagination failed.\n                if (selStartPage == -1 || selEndPage == -1)\n                    throw new ArgumentException(SR.InvalidSelectionPages);\n\n                allPagesVisible = AreAllPagesVisible(viewer, selStartPage, selEndPage);\n            }\n\n            IList<IAttachedAnnotation> attachedAnnotations = null;\n\n            if (allPagesVisible)\n            {\n                // If viewer is not a DocumentViewerBase or the selection has\n                // no parts on non-visible pages, just use the attached annotations\n                attachedAnnotations = service.GetAttachedAnnotations();\n            }\n            else\n            {\n                // Use the method specific to the kind of content we are displaying\n                if (isFlow)\n                {\n                    attachedAnnotations = GetSpannedAnnotationsForFlow(service, selection);","sourceCodeStart":579,"sourceCodeEnd":615,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/AnnotationHelper.cs#L579-L615","documentation":"GetSpannedAnnotations resolves which pages the start and end of the selection fall on. If either TextPointer maps to page -1 (pagination failed or the pointer has no page), the selection cannot be anchored and ArgumentException is thrown.","triggerScenarios":"Calling AnnotationHelper.GetSpannedAnnotations (or attachedAnnotations/spannedAnnots helpers) with a selection whose start or end TextPointer is not associated with a paginated page — typically a programmatically built selection inside a viewer where pagination has not completed.","commonSituations":"Querying spanned annotations immediately after changing document content or page size before layout/pagination finished; selections over content in an unloaded or collapsed DocumentViewer page.","solutions":["Wait until pagination/layout is complete (IsPageCountValid / LayoutUpdated) before calling GetSpannedAnnotations.","Build selections from TextPointers obtained from rendered content in the viewer rather than from an arbitrary TextContainer.","Catch ArgumentException and defer or retry the call after layout.","Ensure the viewer is loaded and visible so TextPointer pages can be resolved."],"exampleFix":"// before\nvar annots = AnnotationHelper.GetSpannedAnnotations(service, viewer);\n// after\nif (viewer.Document is FlowDocument fd && fd.IsPageCountValid)\n    var annots = AnnotationHelper.GetSpannedAnnotations(service, viewer);","handlingStrategy":"try-catch","validationCode":"bool SelectionHasPages(FlowDocumentScrollViewer v) { var sel = ...; return TextSelectionHelper.GetPointerPage(sel.Start, out var p0) && p0 >= 0; } // resolve pages before calling","typeGuard":null,"tryCatchPattern":"try { var annots = AnnotationHelper.GetSpannedAnnotations(service, viewer); } catch (ArgumentException ex) { log.Warn(\"Selection not paginated yet, retrying after layout: \" + ex.Message); viewer.LayoutUpdated += Once(GetSpannedAnnotations); }","preventionTips":["Wait for IsPageCountValid/LayoutUpdated before querying spanned annotations.","Build selections from TextPointers within the rendered viewer.","Avoid calling annotation page helpers during pagination churn (resize, content change)."],"tags":["wpf","annotations","pagination"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}