Commands

  • 3 minutes to read

This document describes how to invoke and customize commands.

Any action than an end-user can perform in the PDF Viewer (for example, opening a document, text selection, page rotation) corresponds to a specific command.

The commands in the PDF Viewer are represented by corresponding PdfViewerControl's properties (e.g., the DocumentViewerControl.OpenDocumentCommand property).

NOTE

The PdfViewerControl class inherits some command properties from the DocumentViewerControl class.

Each command property returns a corresponding object that implements the ICommand interface. This interface exposes two methods and an event.

  • bool CanExecute(object parameter);
  • void Execute(object parameter);
  • event EventHandler CanExecuteChanged.

Execute Commands

The command's main method is Execute. It is used to invoke a command.

The code snippet below shows how to invoke the DocumentViewerControl.SetZoomModeCommand command:

pdfViewer.SetZoomModeCommand.Execute(ZoomMode.FitToWidth);

The CanExecute method is called before command execution. It indicates whether a command can be executed. The Execute method is only invoked when CanExecute returns true.

The following code snippet shows how to invoke the DocumentViewerControl.SetPageNumberCommand command whose parameter is a required page number. The CanExecute method checks if the second page exists in a document.

if (pdfViewer.SetPageNumberCommand.CanExecute(2)) {
    pdfViewer.SetPageNumberCommand.Execute(2);
}

The CanExecuteChanged event is raised when the CanExecute value has been changed.

Replace Built-in Command with a Custom Command

You can modify the functionality of the existing PdfViewerControl command. All commands in the PDF Viewer are created using the command provider represented by the PdfCommandProvider class.

To create a custom command:

  • Create a custom command provider class inherited from the PdfCommandProvider class. Override its members (for example, ZoomInCommandInternal) to create an instance of the DelegateCommand class.

    The delegate command constructor accepts Execute and CanExecute delegates (in this example ZoomIn and CanZoomIn delegates). The delegate command calls these delegates when the command's logic is invoked.

    public class CustomPdfCommandProvider : PdfCommandProvider {       
            ICommand zoomInCommandInternal;
            protected override ICommand ZoomInCommandInternal {
                get { return zoomInCommandInternal ?? (zoomInCommandInternal = new DelegateCommand(ZoomIn, CanZoomIn)); }
            } 
    
            void ZoomIn() {
                //...           
            }
    
            bool CanZoomIn() {
                //...      
            }
    }
    
  • Use the created CustomPdfCommandProvider to substitute the default command provider.

    xmlns:pdfViewer="http://schemas.devexpress.com/winfx/2008/xaml/pdf"
    
    <pdfViewer:PdfViewerControl x:Name="pdfViewer" DocumentSource="{Binding Path}" >         
        <pdfViewer:PdfViewerControl.CommandProvider>
            <local:CustomPdfCommandProvider />
        </pdfViewer:PdfViewerControl.CommandProvider>
    </pdfViewer:PdfViewerControl>
    

See the How to: Replace a Standard PDF Viewer Control Command with a Custom Command example for more information.