dotnet/wpf · error · InvalidOperationException
SR.RightsManagementExceptionNoRightsForOperation
Error message
SR.RightsManagementExceptionNoRightsForOperation
What it means
DocumentApplicationDocumentViewer.OnPrintCommand checks the RightsManagementPolicy before printing; if the AllowPrint policy bit is not granted for the rights-managed document it throws InvalidOperationException(SR.RightsManagementExceptionNoRightsForOperation). The user's rights grant simply does not include printing.
Solutions
- Check the effective rights (RightsManagementPolicy.AllowPrint) before enabling the Print command; disable the UI instead of relying on the throw.
- Request a new end-user license with print rights from the rights issuer if printing is legitimately needed.
- Catch InvalidOperationException around the print invocation and show a 'printing not permitted' dialog.
Example fix
// before printButton.IsEnabled = true; // after printButton.IsEnabled = (rmPolicy & RightsManagementPolicy.AllowPrint) != 0;
Defensive patterns
Strategy: validation
Validate before calling
bool canPrint = (doc.RightsManagementPolicy & RightsManagementPolicy.AllowPrint) != 0; if (!canPrint) DisablePrintUi();
Type guard
bool HasRight(RightsManagementPolicy granted, RightsManagementPolicy needed) => (granted & needed) == needed;
Try / catch
try { docViewer.Print(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("NoRightsForOperation"))
{ ShowMessage("Printing is not permitted for this document."); } Prevention
- Gate print UI on granted rights.
- Request print rights in the publish license when authoring.
- Handle offline license refresh scenarios.
When it happens
Trigger: Invoking the Print command (ApplicationCommands.Print) on a rights-managed XPS document whose granted UseRights mask lacks RightsManagementPolicy.AllowPrint.
Common situations: User opens a protected document licensed only for view/copy; pressing Ctrl+P or a print toolbar button in the XPS Document Application.
Understand the failure class
Background: Permission denied / not authorized / 403 Forbidden: access-control rejections when the caller lacks the required role, grant, or ownership — this error's family across 18 libraries.
Related errors
- Exception of type 'UnauthorizedAccessException' was thrown.
- SR.RMProviderExceptionNoRightsToDocument
- SR.XpsViewerRightsManagementException
- SR.XpsViewerRightsManagementException
- " }} " element found. Expected fixed page element ( }} ).
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/d924c946c1306247.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationUI/MS/Internal/Documents/DocumentApplicationDocumentViewer.cs:291
SetupUIControls();
SetupUITabIndices();
_isUISetup = true;
}
}
/// <summary>
/// Handler for the Print command.
/// </summary>
protected override void OnPrintCommand()
{
if ((RightsManagementPolicy & RightsManagementPolicy.AllowPrint) ==
RightsManagementPolicy.AllowPrint)
{
OnPrintCommandPageRangeOverride();
}
else
{
throw new InvalidOperationException(
SR.RightsManagementExceptionNoRightsForOperation);
}
}
/// <summary>
/// Handler for the CancelPrint command.
/// </summary>
protected override void OnCancelPrintCommand()
{
#if !DONOTREFPRINTINGASMMETA
_documentWriter?.CancelAsync();
#endif // DONOTREFPRINTINGASMMETA
}
/// <summary>
/// Called when WritingCompleted event raised by a DocumentWriter (during printing).
/// </summary>
/// <param name="sender">Sender of the event.</param>View on GitHub (pinned to 81131a70a4)