Command Manager in Blazor SfPdfViewer Component

19 Mar 20242 minutes to read

The Pdfviewer provides support to map or bind command execution with a desired combination of key gestures.

The PdfViewerCommandManager provides support to define custom commands. These custom commands are executed when the specified key gesture is recognized.

Command Execution

To execute custom commands, simply provide a list of keyboard shortcuts along with the corresponding actions. These actions will be triggered when the specified keyboard shortcuts are pressed.

  • Commands:
    Represents the list of custom keyboard shortcuts along with the name of the action will be executed in PDF viewer

  • CommandExecuted:
    When you use the keyboard shortcut, it triggers the execution of this method, which carries out the specified action within the PDF viewer.

How to create custom command:

You can create custom keyboard commands by specifying the name of the action and the corresponding keyboard combination within the PDF viewer.

  • ActionName: Specifies the name of the action to be executed upon pressing the keyboard shortcuts within the PDF viewer.

  • Gesture:
    Specifies the combination of keys and modifiers, including Control, Shift, Alt or Meta key that are utilized within the PDF viewer.

The following code snippet explains how to create custom command.

@using Syncfusion.Blazor.SfPdfViewer

<SfPdfViewer2 Height="100%"
    <PdfViewerEvents CommandExecuted="@CommandExecute"></PdfViewerEvents>
    <PdfViewerCommandManager Commands="@command" ></PdfViewerCommandManager>                

@code {
    // Reference to the Pdf viewer 
    SfPdfViewer2 pdfViewer;
    public string DocumentPath { get; set; } = "wwwroot/Data/PDF_Succinctly.pdf";

    /// <summary> 
    /// Defines the list of custom commands 
    /// </summary> 

    public List<KeyboardCommand> command = new List<KeyboardCommand>() 
        new KeyboardCommand() 
            ActionName = "FitToWidth", 
            Gesture = new KeyGesture() { Key = PdfKeys.W, Modifiers = PdfModifierKeys.Shift } 
        new KeyboardCommand() 
            ActionName = "FitToPage", 
            Gesture = new KeyGesture() { Key = PdfKeys.P, Modifiers = PdfModifierKeys.Alt } 

    /// <summary> 
    /// Custom command execution. 
    /// </summary> 

    public void CommandExecute(CommandExecutedEventArgs args) 
        if(args.Modifiers == PdfModifierKeys.Shift && args.Key == PdfKeys.W) 
        else if (args.Modifiers == PdfModifierKeys.Alt && args.Key == PdfKeys.P) 

View sample in GitHub

See also