Windows Presentation Foundation (WPF)
In this tutorial, you will configure a C# or VB.NET project for a Windows Presentation Foundation (WPF) application and use ImageGear PDF capabilities. You will also learn how to open a PDF or PS file, display it on the screen, and navigate through the different pages of the PDF.
For local development, run the IGPDFSetup.exe application located at the root level of the ImageGear PDF .zip file. This will ensure that licensing is set up properly for local development. Once the setup application is complete, projects can use NuGet for building and deployment.
The following tutorial refers specifically to 64-bit installations; for 32-bit installations:
- Your project should already be set to compile to target Debug and x86, and you should have the directory: $YOURLOCALPROJ\bin\x86\Debug\.
- Throughout these instructions, replace x64 with x86.
- The 32-bit ImageGear binaries are found in $INSTALLDIR\Bin\x86
Using the desired version of Visual Studio (2010 or later):
- Create a new "WPF Application" project, using C# or VB.NET, and name the project: my_first_PDF_WPF_project
- If you installed ImageGear 64-bit, using the Configuration Manager, create a new project platform (if you don’t have one already) for x64. Make sure your project is set to compile targeting Debug and x64. Make sure you now have $YOURLOCALPROJ\bin\x64\Debug\, and if is not there, create it.
- Add references and required resources into your projects in one of the following ways:
- Recommended: use our NuGet Packages. For this project, you need the following packages:
- Manually:
- Copy all files (and folders) inside $INSTALLDIR\Bin\x64 to your local output bin directory in your project (i.e., $YOURLOCALPROJ\bin\x64\Debug\ ).
- Add the following references to your project from $YOURLOCALPROJ\bin\x64\Debug\:
- ImageGear.Core.dll
- ImageGear.Evaluation.dll
- ImageGear.Formats.Common.dll
- ImageGear.Formats.Pdf.dll
- ImageGear.Formats.Vector.dll
- ImageGear.Presentation.dll
- ImageGear.Windows.Controls.dll
- ImageGear.Wpf.dll
Your output target directory should be set to $YOURLOCALPROJ\bin\x64\Debug\
- Open the MainWindow.xaml screen with both the Design and XAML panels for the window visible. You will use the XAML panel to create the graphical user interface. First, in the XAML panel, create a custom alias namespace. So, add some code to the header so it looks like this:
XAML |
Copy Code |
<Window x:Class="my_first_PDF_WPF_project.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:IGWC="clr-namespace:ImageGear.Windows.Controls;assembly=ImageGear.Windows.Controls"
Title="MainWindow" Height="714" Width="1035" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
</Window> |
- For organizational purposes we will use a Grid. Inside of that Grid, add a Menu. That Menu will have a File > Open … item that will be responsible for opening a PDF file. In the Grid, we will also add a StatusBar to offer feedback to the user. The XML code to add the mentioned controls is the following:
XAML |
Copy Code |
<Window x:Class="my_first_PDF_WPF_project.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:IGWC="clr-namespace:ImageGear.Windows.Controls;assembly=ImageGear.Windows.Controls"
Title="MainWindow" Height="714" Width="1035" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="91*" />
<ColumnDefinition Width="4*" />
<ColumnDefinition Width="44*" />
<ColumnDefinition Width="888*" />
</Grid.ColumnDefinitions>
<Menu Height="20" HorizontalAlignment="Stretch" Name="menu" VerticalAlignment="Top" Grid.ColumnSpan="4">
<MenuItem Name="menuItemFileOpen" Header="Open PDF ..." Click="menuItemFileOpen_Click"/>
</Menu>
<StatusBar Height="25" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" VerticalContentAlignment="Center" HorizontalContentAlignment="Stretch" Margin="0,0,0,-1" Grid.ColumnSpan="4">
<StatusBarItem Name="statusLabel" DataContext="{Binding}" BorderBrush="Black" BorderThickness="0" VerticalAlignment="Center" />
</StatusBar>
</Grid>
</Window> |
- To finish the GUI, we need to add a PageView control, which will be used to display the PDF document, and the ImGearThumbnailList that will be used to navigate through the document. The entire XAML file should now look like this:
XAML |
Copy Code |
<Window x:Class="my_first_PDF_WPF_project.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:IGWC="clr-namespace:ImageGear.Windows.Controls;assembly=ImageGear.Windows.Controls"
Title="MainWindow" Height="714" Width="1035" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="91*" />
<ColumnDefinition Width="4*" />
<ColumnDefinition Width="44*" />
<ColumnDefinition Width="888*" />
</Grid.ColumnDefinitions>
<Menu Height="20" HorizontalAlignment="Stretch" Name="menu" VerticalAlignment="Top" Grid.ColumnSpan="4">
<MenuItem Name="menuItemFileOpen" Header="Open Document ..." Click="menuItemFileOpen_Click"/>
</Menu>
<StatusBar Height="25" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" VerticalContentAlignment="Center" HorizontalContentAlignment="Stretch" Margin="0,0,0,-1" Grid.ColumnSpan="4">
<StatusBarItem Name="statusLabel" DataContext="{Binding}" BorderBrush="Black" BorderThickness="0" VerticalAlignment="Center" />
</StatusBar>
<IGWC:ImGearThumbnailList Name="igThumbnailList" Margin="12,26,0,30" HorizontalAlignment="Left" VerticalAlignment="Stretch" Width="90" ItemHeight="80" ItemWidth="60" SelectionChanged="igThumbnailList_SelectionChanged" Grid.ColumnSpan="3" />
<IGWC:PageView Name="igPageView" Margin="20,26,12,30" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="LightGray" Grid.Column="2" Grid.ColumnSpan="2" />
</Grid>
</Window> |
- Next, let's add the functionality to the controls in the MainWindow.xaml.cs file. First add some using statements and declare the some members required for this project in the .xaml.cs file. Open MainWindow.xaml.cs and add code similar to the following within the body of your MainWindow class:
- First, we need to initialize and support processing of PDF and PS files:
- Next, add the code for opening a PDF or PS file.
- Also, the code to navigate across the pages in the PDF document using the Thumbnail list:
- And finally, the code to properly dispose of all the used controls:
This sample project was created as a simple introduction to using the PDF functionality in ImageGear. For systems designed for production environments, you need to configure your projects more efficiently by only adding the resources that you need. For more information, refer to Deploying Your Product.