Getting Started with WPF PDF Viewer |
This topic contains the following sections:
This section explains how to create an application that displays a PDF file using the WPF PDF Viewer.
The following assemblies are required in your WPF application to use the PDF Viewer.
Required Assemblies | Description |
---|---|
Patagames.Pdf | The main assembly containing a class library for creating, editing, loading PDF documents, as well as PDF Rendering Engine. |
Patagames.Pdf.Wpf | This component contains the PdfViewer UserControl and other UI controls that are used in the WPF PDF Viewer. |
You can create a PDF Viewer application by simply drag the control from the Visual Studio toolbox and drop in the designer window of your application or by creating the control manually from XAML.
Follow these steps to drag and drop the PdfViewer from the toolbox.
Create a new WPF application in Visual Studio the with following XAML markup.
<Window x:Class="ExamplesWpf.MainWindow02" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="mainWindow02" > <Grid> <Grid.RowDefinitions> <RowDefinition Height="auto"></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> </Grid> </Window>
Open the Visual Studio toolbox.
Navigate to Patagames Pdf.Net SDK tab and drag the PdfViewer toolbox item to the Designer window, it automatically adds the required references to the current application.
Note |
---|
PdfViewer itself does not contain any UI elements. This allows you to quite freely decorate your interface and organically fit it into the design of your application. All necessary functionality is available through public methods. |
Navigate to Patagames Pdf.Net SDK tab and drag the PdfToolBoxMain toolbox item to the Designer window.
In the XAML editor, lay out all the components and name the added PDF viewer to 'pdfViewer1'
<ScrollViewer Grid.Row="1" CanContentScroll="True" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"> <Wpf:PdfViewer Name="pdfViewer1" /> </ScrollViewer>
Note |
---|
In order to ensure the scrollability, the Viewer should be placed inside the ScrollView container. |
In the XAML editor, bind the pdfViewer1 to the PdfViewer dependency property of the PdfToolBoxMain toolbar using the following code sample in XAML.
<ToolBarTray Grid.Row="0"> <Toolbars:PdfToolBarMain Window="{Binding ElementName= mainWindow}" PdfViewer="{Binding ElementName=pdfViewer1}"></Toolbars:PdfToolBarMain> </ToolBarTray>
This activates the toolbar to work with this instance of PdfViewer, now you can load documents by clicking the Open icon.
In the same way, add PdfToolBarClipboard and other toolbars and controls.
To add control manually in XAML, do the following steps,
Add the required assemblies as a reference to the project.
Add the following Patagames namespace in XAML to make use of the PdfViewer.
<Window xmlns:Wpf="clr-namespace:Patagames.Pdf.Net.Controls.Wpf;assembly=Patagames.Pdf.Wpf" xmlns:Toolbars="clr-namespace:Patagames.Pdf.Net.Controls.Wpf.ToolBars;assembly=Patagames.Pdf.Wpf"> </Window>
Declare the PdfViewer control and toolbars in the XAML page.
<Window xmlns:Wpf="clr-namespace:Patagames.Pdf.Net.Controls.Wpf;assembly=Patagames.Pdf.Wpf" xmlns:Toolbars="clr-namespace:Patagames.Pdf.Net.Controls.Wpf.ToolBars;assembly=Patagames.Pdf.Wpf" x:Class="ExamplesWpf.MainWindow01" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="mainWindow01" > <Grid> <Grid.RowDefinitions> <RowDefinition Height="auto"></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <ToolBarTray Grid.Row="0"> <Toolbars:PdfToolBarMain Window="{Binding ElementName= mainWindow}" PdfViewer="{Binding ElementName=pdfViewer1}" Name="toolbarMain1"></Toolbars:PdfToolBarMain> <Toolbars:PdfToolBarClipboard PdfViewer="{Binding ElementName=pdfViewer1}" Name="toolbarClipboard1"></Toolbars:PdfToolBarClipboard> </ToolBarTray> <ScrollViewer Grid.Row="1" CanContentScroll="True" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"> <Wpf:PdfViewer Name="pdfViewer1" /> </ScrollViewer> </Grid> </Window>
The following screenshots depicts different sections of the PdfViewer control and toolbars.
The height and width of the control.
Padding within the control.
The PageMargin property specifies space between page and page separator.
Page border. The PageBorderColor property allows you to get or set the color of the page border.
Page separator. You can turn off the display of the separators by seting the ShowPageSeparator property to false, or change the color using the PageSeparatorColor property.
Current page highlight. You can turn it off by seting the ShowCurrentPageHighlight property to false, or change the color using the CurrentPageHighlightColor property.
BackColor - a color that represents the background color of the control.
PageBackColor - a color that represents the background color of the page.
Open file
Print PDF
Select all text
Copy selected text to clipboard
Go to first page
Go to previous page
Current page indicator
Total number of pages in a document
Go to next page
Go to last page
Rotate the current page counterclockwise
Rotate current page clockwise
Оpen a context menu with search parameters
Text input field to search
Go to the previous found record
Go to the next found record
Set the zoom level to the page's actual size
Set the zoom level so one full page fits in the window
Set the zoom level so the page width fits the window width
Set the zoom level so the page height fits the window height
View one page at time
View pages continuously with vertical scrolling enabled
View pages continuously with horizontal scrolling enabled
View pages side-by-side with continuous scrolling enabled
View two pages side-by-side
Decrease magnification
Current magnifiction level / Text field to set magnification level to a specific value
Drop-down list with predefined magnification levels
Increase magnification
Current magnifiction level
Drop-down list with predefined magnification levels
Decrease magnification
Track bar to set magnification level
Increase magnification
One of the possible appearance of the PDF viewer
The sample project for displaying PDF files using the PDF Viewer is available in the Zip/Msi package here.