Click or drag to resize

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.

Assemblies required

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.

Create a simple PDF Viewer application

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.

Drag and drop the PdfViewer from the toolbox

Follow these steps to drag and drop the PdfViewer from the toolbox.

  1. Create a new WPF application in Visual Studio the with following XAML markup.

    XAML
    <Window x:Class="ExamplesWpf.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Name="mainWindow"
            >
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
        </Grid>
    </Window>
  2. Open the Visual Studio toolbox.

  3. 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.

    Getting Started Wpf 01
    Note 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.
  4. Navigate to Patagames Pdf.Net SDK tab and drag the PdfToolBoxMain toolbox item to the Designer window.

  5. In the XAML editor, lay out all the components and name the added PDF viewer to 'pdfViewer1'

    XAML
    <ScrollViewer Grid.Row="1" CanContentScroll="True"  VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
        <Wpf:PdfViewer Name="pdfViewer1" />
    </ScrollViewer>
    Note Note
    In order to ensure the scrollability, the Viewer should be placed inside the ScrollView container.
  6. In the XAML editor, bind the pdfViewer1 to the PdfViewer dependency property of the PdfToolBoxMain toolbar using the following code sample in XAML.

    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.

  7. In the same way, add PdfToolBarClipboard and other toolbars and controls.

Adding control manually in XAML

To add control manually in XAML, do the following steps,

  1. Add the required assemblies as a reference to the project.

  2. Add the following Patagames namespace in XAML to make use of the PdfViewer.

    XAML
    <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>
  3. Declare the PdfViewer control and toolbars in the XAML page.

    XAML
    <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.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Name="mainWindow"
            >
        <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>
    Getting Started Wpf 02
Appearance and Structure of the Toolbars and Controls

The following screenshots depicts different sections of the PdfViewer control and toolbars.

PdfViewer

Pdf Viewer Legend
  1. The height and width of the control.

  2. Padding within the control.

  3. The PageMargin property specifies space between page and page separator.

  4. Page border. The PageBorderColor property allows you to get or set the color of the page border.

  5. 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.

  6. Current page highlight. You can turn it off by seting the ShowCurrentPageHighlight property to false, or change the color using the CurrentPageHighlightColor property.

  7. BackColor - a color that represents the background color of the control.

  8. PageBackColor - a color that represents the background color of the page.

PdfToolBarMain

Pdf Tool Strip Main Legend
  1. Open file

  2. Print PDF

PdfToolBarClipboard

Pdf Tool Strip Clipboard Legend
  1. Select all text

  2. Copy selected text to clipboard

PdfToolBarPages

Pdf Tool Strip Pages Legend
  1. Go to first page

  2. Go to previous page

  3. Current page indicator

  4. Total number of pages in a document

  5. Go to next page

  6. Go to last page

PdfToolBarRotate

Pdf Tool Strip Rotate Legend
  1. Rotate the current page counterclockwise

  2. Rotate current page clockwise

Pdf Tool Strip Search Legend
  1. Оpen a context menu with search parameters

  2. Text input field to search

  3. Go to the previous found record

  4. Go to the next found record

PdfToolBarSizes

Pdf Tool Strip Sizes Legend
  1. Set the zoom level to the page's actual size

  2. Set the zoom level so one full page fits in the window

  3. Set the zoom level so the page width fits the window width

  4. Set the zoom level so the page height fits the window height

PdfToolBarViewModes

Pdf Tool Strip View Modes Legend
  1. View one page at time

  2. View pages continuously with vertical scrolling enabled

  3. View pages continuously with horizontal scrolling enabled

  4. View pages side-by-side with continuous scrolling enabled

  5. View two pages side-by-side

PdfToolBarZoom

Pdf Tool Strip Zoom Legend
  1. Decrease magnification

  2. Current magnifiction level / Text field to set magnification level to a specific value

  3. Drop-down list with predefined magnification levels

  4. Increase magnification

PdfToolBarZoomEx

Pdf Tool Strip Zoom Ex Legend
  1. Current magnifiction level

  2. Drop-down list with predefined magnification levels

  3. Decrease magnification

  4. Track bar to set magnification level

  5. Increase magnification

Pdf viewer in assembled form

One of the possible appearance of the PDF viewer

Pdf Viewer Complete

The sample project for displaying PDF files using the PDF Viewer is available in the Zip/Msi package here.

See Also