Working with Commands in WPF Pdf Viewer
15 Nov 20228 minutes to read
Magnification operations
The PdfViewerControl provides the following set of commands to perform magnification.
NOTE
- When executing the IncreaseZoomCommand, the 25% of zoom value has been increased from the current zoom percentage.
- When executing the DecreaseZoomCommand, the 25% of zoom value has been decreased from the current zoom percentage.
The following XAML code shows how to bind the IncreaseZoomCommand to a button.
<Syncfusion:PdfViewerControl x:Name="pdfViewerControl" />
<Button Content="ZoomIn" Height="20" Width="60" Command="{Binding ElementName=pdfViewerControl, Path= IncreaseZoomCommand, Mode=OneWay}" />
The following XAML code shows how to bind the DecreaseZoomCommand to a button.
<Syncfusion:PdfViewerControl x:Name="pdfViewerControl" />
<Button Content="ZoomOut" Height="20" Width="60" Command="{Binding ElementName=pdfViewerControl, Path= DecreaseZoomCommand, Mode= OneWay }" />
Page Navigations
The below list of commands will be helpful for performing the page navigation operations.
FirstPageCommand
Navigation to the first page can be achieved by using the FirstPageCommand as shown below.
The following XAML code shows how to bind the FirstPageCommand to a button.
<Syncfusion:PdfViewerControl x:Name="pdfViewerControl" />
<Button Content="Go to first page" Height="20" Width="100" Command="{Binding ElementName=pdfViewerControl, Path=FirstPageCommand, Mode= OneWay }" />
NOTE
- If the current page is already the first page of PDF document, then FirstPageCommand does not have any effect.
LastPageCommand
Navigation to the last page can be achieved by using the LastPageCommand as shown below.
The following XAML code shows how to bind the LastPageCommand to a button.
<Syncfusion:PdfViewerControl x:Name="pdfViewerControl" />
<Button Content="Go to last page" Height="20" Width="100" Command="{Binding ElementName=pdfViewerControl, Path=LastPageCommand, Mode= OneWay }" />
NOTE
- If the current page is already the last page of PDF document, then LastPageCommand does not have any effect.
NextPageCommand
NextPageCommand displays the page next to the currently displayed page in the PDF Viewer.
The following XAML code shows how to bind the NextPageCommand to a button.
<Syncfusion:PdfViewerControl x:Name="pdfViewerControl" />
<Button Content="Go to next page" Height="20" Width="100" Command="{Binding ElementName=pdfViewerControl, Path=NextPageCommand, Mode= OneWay }" />
NOTE
- If the current page is last page of the PDF document, then NextPageCommand does not have any effect.
PreviousPageCommand
PreviousPageCommand displays the page previous to the currently displayed page in the PDF Viewer.
The following XAML code shows how to bind the PreviousPageCommand to a button.
<Syncfusion:PdfViewerControl x:Name="pdfViewerControl" />
<Button Content="Go to previous page" Height="20" Width="100" Command="{Binding ElementName=pdfViewerControl, Path= PreviousPageCommand, Mode= OneWay }" />
NOTE
- If the current page is first page of the PDF document, then PreviousPageCommand does not have any effect.
GoToPageCommand
GoToPageCommand allows you to navigate through the pages of the PDF document. The following code example illustrates the navigation to page 2 of the PDF document.
<Syncfusion:PdfViewerControl x:Name="pdfViewerControl" />
<Button Content="Go to page” Height="20" Width="60" Command="{Binding ElementName=pdfViewerControl, Path=GoToPageCommand, Mode= OneWay }" CommandParameter="2" />
NOTE
- If the command parameter is some other text or invalid page number, the GoToPageCommand does not have any effect.
Printing PDF
Printing can be performed by using the Print command.
The following XAML code shows how to bind the PrintCommand to a Button.
<Syncfusion:PdfViewerControl x:Name="pdfViewerControl" />
<Button Content="Print" Height="20" Width="60" Command="{Binding ElementName=pdfViewerControl,Path= PrintCommand, Mode= OneWay }" />
AnnotationCommand
The AnnotationCommand can be used to set the different types of annotation mode by using the AnnotationParameter in PdfViewer.
The following list of AnnotationCommandParameter values to the AnnotationCommand are used to set the annotation mode.
- Line
- Circle
- Rectangle
- Arrow
- Ink
- Strikethrough
- Highlight
- Underline
NOTE
- If the AnnotationCommandParameter value of AnnotationCommand is not equivalent to above mentioned any one of the text, then the annotation mode is set as none.
- The AnnotationCommandParameter is not a case sensitive.
Line Annotation
The following XAML code shows how to bind the annotation command for line annotation to a Button.
<Syncfusion:PdfViewerControl x:Name="pdfViewerControl" />
<Button Content="Line" Height="20" Width="60" CommandParameter="Line" Command="{Binding ElementName=pdfViewerControl, Path=AnnotationCommand, Mode= OneWay }" />
Circle Annotation
The following XAML code shows how to bind the annotation command for circle annotation to a Button.
<Syncfusion:PdfViewerControl x:Name="pdfViewerControl" />
<Button Content="Circle" Height="20" Width="60" CommandParameter="Circle " Command="{Binding ElementName=pdfViewerControl, Path=AnnotationCommand, Mode= OneWay }" />
Rectangle Annotation
The following XAML code shows how to bind the annotation command for rectangle annotation to a Button.
<Syncfusion:PdfViewerControl x:Name="pdfViewerControl" />
<Button Content="Rectangle" Height="20" Width="60" CommandParameter="Rectangle" Command="{Binding ElementName=pdfViewerControl, Path=AnnotationCommand, Mode= OneWay }" />
Arrow Annotation
The following XAML code shows how to bind the annotation command for arrow annotation to a Button.
<Syncfusion:PdfViewerControl x:Name="pdfViewerControl"/>
<Button Content="Arrow" CommandParameter="Arrow" Command="{Binding ElementName=pdfViewerControl, Path=AnnotationCommand, Mode= OneWay }" />
Ink Annotation
The following XAML code shows how to bind the annotation command for ink annotation to a Button.
<Syncfusion:PdfViewerControl x:Name="pdfViewerControl" />
<Button Content="Ink" Height="20" Width="60" CommandParameter="Ink" Command="{Binding ElementName=pdfViewerControl, Path=AnnotationCommand, Mode= OneWay }" />
Strikethrough Annotation
The following XAML code shows how to bind the annotation command for strikethrough annotation to a Button.
<Syncfusion:PdfViewerControl x:Name="pdfViewerControl" />
<Button Content="Strikethrough" Height="20" Width="60" CommandParameter="Strikethrough" Command="{Binding ElementName=pdfViewerControl, Path=AnnotationCommand, Mode= OneWay }" />
Highlight Annotation
The following XAML code shows how to bind the annotation command for highlight annotation to a Button.
<Syncfusion:PdfViewerControl x:Name="pdfViewerControl" />
<Button Content="Highlight" Height="20" Width="60" CommandParameter="Highlight" Command="{Binding ElementName=pdfViewerControl, Path=AnnotationCommand, Mode= OneWay }" />
Underline Annotation
The following XAML code shows how to bind the annotation command for underline annotation to a Button.
<Syncfusion:PdfViewerControl x:Name="pdfViewerControl" />
<Button Content="Underline" Height="20" Width="60" CommandParameter="Underline" Command="{Binding ElementName=pdfViewerControl, Path=AnnotationCommand, Mode= OneWay }" />
Save Document Command
You can save the PDF file in the given file path using the SaveDocumentCommand
. The following code shows how to save the PDF file by executing the command and to the pass the file path as command parameter.
pdfViewerControl.SaveDocumentCommand.Execute(@"C:\temp\Output.pdf");
NOTE
You can refer to our WPF PDF Viewer feature tour page for its groundbreaking feature representations. You can also explore our WPF PDF Viewer example to know how to render and configure the pdfviewer.