{"record":{"id":"f09abfd2efdddc12","repo":"dotnet/wpf","slug":"sr-flowdocumentinvalidcontnetchange","errorCode":null,"errorMessage":"SR.FlowDocumentInvalidContnetChange","messagePattern":"SR\\.FlowDocumentInvalidContnetChange","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/FlowDocumentPaginator.cs","lineNumber":537,"sourceCode":"                Size newPageSize = value;\n                if (double.IsNaN(newPageSize.Width))\n                {\n                    newPageSize.Width = _defaultPageSize.Width;\n                }\n                if (double.IsNaN(newPageSize.Height))\n                {\n                    newPageSize.Height = _defaultPageSize.Height;\n                }\n                Size oldActualSize = ComputePageSize();\n                _pageSize = newPageSize;\n                Size newActualSize = ComputePageSize();\n                if (!DoubleUtil.AreClose(oldActualSize, newActualSize))\n                {\n                    // Detect invalid content change operations.\n                    if (_document.StructuralCache.IsFormattingInProgress)\n                    {\n                        _document.StructuralCache.OnInvalidOperationDetected();\n                        throw new InvalidOperationException(SR.FlowDocumentInvalidContnetChange);\n                    }\n\n                    // Any change of page metrics invalidates entire break record table.\n                    // Hence page metrics change is treated in the same way as ContentChanged\n                    // spanning entire content.\n                    // NOTE: May execute external code, so it is possible to get\n                    //       an exception here.\n                    InvalidateBRT();\n                }\n            }\n        }\n\n        /// <summary>\n        /// Whether content is paginated in the background.\n        /// When True, the Paginator will paginate its content in the background,\n        /// firing the PaginationCompleted and PaginationProgress events as appropriate.\n        /// Background pagination begins immediately when set to True. If the\n        /// PageSize is modified and this property is set to True, then all pages","sourceCodeStart":519,"sourceCodeEnd":555,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/FlowDocumentPaginator.cs#L519-L555","documentation":"FlowDocumentPaginator's page-metrics change handler detects an invalid content change: page dimensions changed while formatting (pagination) was in progress. It reports the violation via StructuralCache.OnInvalidOperationDetected() and throws InvalidOperationException (SR.FlowDocumentInvalidContnetChange). This is an internal consistency check.","triggerScenarios":"Changing PageWidth/PageHeight/PagePadding (or other page metrics) on the FlowDocument from code that runs during pagination — e.g. inside GetPage, during a formatting callback, or from a handler invoked synchronously by layout.","commonSituations":"Custom paginators adjusting PagePadding inside GetPage overrides; reactive bindings that update page size while a print job is paginating; handlers triggered by the pagination pass itself updating page metrics.","solutions":["Set page metrics only outside active pagination — before calling GetPage/ComputePageCount or after pagination completes.","Defer metric changes with Dispatcher.BeginInvoke so they run after the current formatting pass.","If writing a derived paginator, never mutate document page properties within overridden GetPage or pagination callbacks."],"exampleFix":"// before\noverride void OnPageChanged(...) { flowDocument.PagePadding = newThickness; }\n// after\nvoid ApplyAfterPagination(Thickness t) { Dispatcher.BeginInvoke(() => flowDocument.PagePadding = t); }","handlingStrategy":"validation","validationCode":"// Change page metrics only when no formatting is in progress:\nif (!flowDocument.StructuralCache.IsFormattingInProgress)\n    flowDocument.PagePadding = newThickness;\nelse\n    Dispatcher.BeginInvoke(() => flowDocument.PagePadding = newThickness);","typeGuard":null,"tryCatchPattern":"try { flowDocument.PageWidth = w; }\ncatch (InvalidOperationException)\n{ Dispatcher.BeginInvoke(() => flowDocument.PageWidth = w); }","preventionTips":["Never mutate PageWidth/PageHeight/PagePadding inside GetPage overrides or pagination callbacks","Apply page metric changes before starting a print/pagination job","Use Dispatcher deferral for metric changes triggered during layout"],"tags":["wpf","flowdocument","invariant-violation"],"backgroundTag":"internal-invariant-violation","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"}