Back to Devexpress

Commands

wpf-120550-controls-and-libraries-pdf-viewer-commands.md

latest3.3 KB
Original Source

Commands

  • Apr 01, 2021
  • 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:

csharp
pdfViewer.SetZoomModeCommand.Execute(ZoomMode.FitToWidth);
vb
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.

csharp
if (pdfViewer.SetPageNumberCommand.CanExecute(2)) {
    pdfViewer.SetPageNumberCommand.Execute(2);
}
vb
If pdfViewer.SetPageNumberCommand.CanExecute(2) Then
    pdfViewer.SetPageNumberCommand.Execute(2)
End If

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.

  • Use the created CustomPdfCommandProvider to substitute the default command provider.

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