How to: Replace a Standard PDF Viewer Control Command with a Custom Command

  • 3 min to read

This example shows how to modify the existing PDF Viewer command functionality.

  1. Create a custom command provider class inherited from the PdfCommandProvider class and override its members to create a new DelegateCommand.

    In this example the CustomPdfCommandProvider class overrides ZoomInCommandInternal and ZoomOutCommandInternal properties to zoom in/out to a custom zoom factor.

    The delegate command constructor accepts ZoomIn and CanZoomIn (ZoomOut and CanZoomOut) delegates.

    public class CustomPdfCommandProvider : PdfCommandProvider {
    
            ICommand zoomInCommandInternal;
            protected override ICommand ZoomInCommandInternal {
                get { return zoomInCommandInternal ?? (zoomInCommandInternal = new DelegateCommand(ZoomIn, CanZoomIn)); }
            }
    
            ICommand zoomOutCommandInternal;
            protected override ICommand ZoomOutCommandInternal {
                get { return zoomOutCommandInternal ?? (zoomOutCommandInternal = new DelegateCommand(ZoomOut, CanZoomOut)); }
            }
    
            void ZoomIn() {
                //...           
            }
    
            bool CanZoomIn() {
                //...      
            }
    
            void ZoomOut() {
                //...           
            }
    
            bool CanZoomOut() {
                //...      
            }
    }
    
  2. Implement ZoomIn and CanZoomIn (ZoomOut and CanZoomOut) methods that the corresponding delegate command will call when the command is executed.

    //...
            readonly List<double> factors = new List<double> { 0.15, 0.3, 0.45, 1, 1.25, 1.5, 2, 5 };
            public PdfViewerControl Control { get { return DocumentViewer as PdfViewerControl; } }
    
            void ZoomIn() {
                if (Control == null) return;
    
                foreach (var zoomFactor in factors) {
                    if (Control.ZoomFactor < zoomFactor) {
                        Control.ZoomFactor = zoomFactor;
                        break;
                    }
                }
            }
    
            bool CanZoomIn() {
                return Control != null && Control.Document != null && Control.ZoomFactor < factors[factors.Count - 1];
            }
    
            void ZoomOut() {
                if (Control == null) return;
    
                for (int i = factors.Count - 1; i >= 0; i--) {
                    if (factors[i] < Control.ZoomFactor) {
                        Control.ZoomFactor = factors[i];
                        break;
                    }
                }
            }
    
            bool CanZoomOut() {
                return Control != null && Control.Document != null && Control.ZoomFactor > factors[0];
            }
    //...
    
  3. Use the created CustomPdfCommandProvider to substitute the default command provider.

    xmlns:local="clr-namespace:DXSample"
    xmlns:pdfViewer="http://schemas.devexpress.com/winfx/2008/xaml/pdf"
    
        <pdfViewer:PdfViewerControl.CommandProvider>
             <local:CustomPdfCommandProvider/>            
        </pdfViewer:PdfViewerControl.CommandProvider>