Display a PDF in PDFViewCtrl for your UWP viewer

In this tutorial you will be able to display a PDF file in PDFViewCtrl. To begin working with PDFViewCtrl, you must have first added it to your project and initialized the library (get started with UWP).

  1. Now that the project is set up, let us add a PDFViewCtrl as the main feature of the project. First, we need to set up the page to contain the PDFViewCtrl. We want to add a main viewing area for the PDFViewCtrl, and an area at the bottom where we can later add in some options. Open up “MainPage.xaml” and find the Grid element. Add some row definitions as follows:The second row will be used for our options bar, while the first row will use whatever space is left to display the PDFViewCtrl.To this Grid, let's add a Border which will host the PDFViewCtrl and then a stack panel with a button to open a document.

XML

1<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
2 <Grid.RowDefinitions>
3 <RowDefinition Height="*"/>
4 <RowDefinition Height="Auto"/>
5 </Grid.RowDefinitions>
6 </Grid>

XML

1<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
2 <Grid.RowDefinitions>
3 <RowDefinition Height="*"/>
4 <RowDefinition Height="Auto"/>
5 </Grid.RowDefinitions>
6 <Border x:Name="PDFViewBorder" Grid.Row="0"/>
7 <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right" Margin="5">
8 <Button x:Name="OpenButton">Open</Button>
9 </StackPanel>
10 </Grid>
  1. Now, we want to add the PDFViewCtrl to the app. We do this in “MainPage.xaml.cs”. Open up “MainPage.xaml.cs” and add a member variable that references a PDFViewCtrl.Next, we need to create a PDFViewCtrl, so let's do this in the constructor of the MainPage. We also need to make the PDFViewCtrl the child of the PDFViewBorder.

C#

1public sealed partial class MainPage : Page
2 {
3 pdftron.PDF.PDFViewCtrl MyPDFViewCtrl;
4 ...
5 }

C#

1public MainPage()
2 {
3 ...
4 MyPDFViewCtrl = new pdftron.PDF.PDFViewCtrl();
5 PDFViewBorder.Child = MyPDFViewCtrl;
6 }
  1. We also have to add a handler for the Open button. Let's do that in MainPage's constructor after we have created a PDFViewCtrl.Inside this click function, we need to open a Document. First, let's include two new namespaces that we need for the file picker and for the storage file.Also, OpenButton_Click needs to be async in order to handle some of the asynchronous API's needed here. So, replace the old OpenButton_Click with the following:

C#

1public MainPage()
2 {
3 ...
4 MyPDFViewCtrl = new pdftron.PDF.PDFViewCtrl();
5 PDFViewBorder.Child = MyPDFViewCtrl;
6 OpenButton.Click += OpenButton_Click;
7 }
8
9 void OpenButton_Click(object sender, RoutedEventArgs e)
10 {
11 throw new NotImplementedException();
12 }

C#

1using Windows.Storage;
2using Windows.Storage.Pickers;

C#

1async void OpenButton_Click(object sender, RoutedEventArgs e)
2 {
3 // Get a file from the file picker.
4 FileOpenPicker fileOpenPicker = new FileOpenPicker();
5 fileOpenPicker.ViewMode = PickerViewMode.List;
6 fileOpenPicker.FileTypeFilter.Add(".pdf");
7 StorageFile file = await fileOpenPicker.PickSingleFileAsync();
8
9 // Create a PDFDocument and use it as the source for the PDFViewCtrl
10 if (file != null)
11 {
12 Windows.Storage.Streams.IRandomAccessStream stream = await
13 file.OpenAsync(FileAccessMode.ReadWrite);
14 pdftron.PDF.PDFDoc doc = new pdftron.PDF.PDFDoc(stream);
15 MyPDFViewCtrl.SetDoc(doc);
16 }
17 }

Build and launch the app and we should now have a very basic PDF document viewer.

Do note that in Windows 8.1, the file picker works differently. Please see this guide for how to use file pickers in Windows Phone 8.1: https://msdn.microsoft.com/en-us/library/windows/apps/dn642086(v=vs.105).aspx

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales