Commands
The PdfViewer commands implement the interactions with the document in the viewer.
The command descriptors are wrappers of predefined commands, such as rotate, open file, page up/down, and others, which are represented by implementations of the CommandDescriptorBase
class. The following list contains part of the available descriptors. Check the CommandDescriptors
collection property of RadPdfViewer
for a complete list of the available commands.
OpenCommandDescriptor
SaveAsCommandDescriptor
PageUpCommandDescriptor
PageDownCommandDescriptor
SelectAllCommandDescriptor
ZoomInCommandDescriptor
ZoomOutCommandDescriptor
MoveCaretToLineStartCommandDescriptor
MoveCaretToLineEndCommandDescriptor
ShowFindDialogCommandDescriptor
PrintCommandDescriptor
To access and execute the descriptors, use the CommandDescriptors
property of the RadPdfViewer
. To get the associated command, use the Command
property of the descriptor.
Execute the command of a command descriptor
this.pdfViewer.CommandDescriptors.ShowFindDialogCommandDescriptor.Command.Execute(null);
IsEnabled
property of the associated descriptor to false
.
Binding Commands to the UI
You can bind the descriptor commands to data through the Command
property of the UI elements like buttons.
Bind the page down command to a button
<telerik:RadButton Content="Page Down"
Command="{Binding ElementName=pdfViewer, Path=CommandDescriptors.PageDownCommandDescriptor.Command}"/>
Overriding Default Commands
The following example demonstrates how to create a custom command and override the command of an existing descriptor. The example creates a custom command by inheriting from the FixedDocumentViewerCommandBase
class. However, you can use any ICommand
implementation.
Create a custom command
public class FitToWidthCommand : FixedDocumentViewerCommandBase
{
private const double PageMargin = 20;
public FitToWidthCommand(FixedDocumentViewerBase fixedDocumentViewerBase)
: base(fixedDocumentViewerBase)
{
}
public override void Execute(object parameter)
{
double width = this.Viewer.ActualWidth - 2 * PageMargin;
double pageWidth = this.Viewer.CurrentPage.Size.Width;
this.Viewer.ScaleFactor = width / pageWidth;
}
}
Create a custom CommandDescriptors class to override the default commands
public class CustomCommandDescriptors : DefaultCommandDescriptors
{
private CommandDescriptor fitToWidthCommandDescriptor;
public override CommandDescriptor FitToWidthCommandDescriptor
{
get
{
if (this.fitToWidthCommandDescriptor == null)
{
this.fitToWidthCommandDescriptor = new CommandDescriptor(new FitToWidthCommand(FixedDocumentViewer));
}
return fitToWidthCommandDescriptor;
}
}
public CustomCommandDescriptors(FixedDocumentViewerBase fixedDocumentViewerBase)
: base(fixedDocumentViewerBase)
{
}
}
Replace the default CommandDescriptors object
this.pdfViewer.CommandDescriptors = new CustomCommandDescriptors(this.pdfViewer);