Universal Windows Platform – PdfView App

Step 1

If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu locate and select Visual Studio 2017.


Step 2

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…


Step 3

From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in a Name and select a Location and then select Ok to create the Project


Step 4

Then in New Universal Windows Project you need to select the Target Version to be Windows 10 Creators Update (10.0; Build 15063) and the Minimum Version to be at least Windows 10 Anniversary Update (10.0; Build 14393) or Windows 10 Creators Update (10.0; Build 15063)


Step 5

From the Menu choose Project, then Add New Item…


Step 6

From the Add New Item choose Visual C# from Installed then choose Code then select Code File and then in the Name as Library.cs and then select Add to add the file to the Project


Step 7

Then in the Code View for Library.cs the following should be entered:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.Data.Pdf;
using Windows.Foundation;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.UI.Popups;
using Windows.UI.Xaml.Media.Imaging;

public class Library
    private PdfDocument document = null;

    public void Show(string content, string title)
        IAsyncOperation<IUICommand> command = new MessageDialog(content, title).ShowAsync();

    public async Task<uint> OpenAsync()
        uint pages = 0;
            document = null;
            FileOpenPicker picker = new FileOpenPicker()
                SuggestedStartLocation = PickerLocationId.DocumentsLibrary
            StorageFile open = await picker.PickSingleFileAsync();
            if (open != null)
                document = await PdfDocument.LoadFromFileAsync(open);
            if (document != null)
                if (document.IsPasswordProtected)
                    Show("Password Protected PDF Document", "PdfView App");
                pages = document.PageCount;
        catch (Exception ex)
            if (ex.HResult == unchecked((int)0x80004005))
                Show("Invalid PDF Document", "PdfView App");
                Show(ex.Message, "PdfView App");
        return pages;

    public async Task<BitmapImage> ViewAsync(uint number)
        BitmapImage source = new BitmapImage();
        if (!(number < 1 || number > document.PageCount))
            uint index = number - 1;
            using (PdfPage page = document.GetPage(index))
                using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
                    await page.RenderToStreamAsync(stream);
                    await source.SetSourceAsync(stream);
        return source;

    public List<int> Numbers(int total)
        return Enumerable.Range(1, total).ToList();

Step 8

Then in the Solution Explorer select MainPage.xaml


Step 9

From the Menu choose View and then Designer


Step 10

The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:

<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Name="Open" Icon="OpenFile" Label="Open" Click="Open_Click"/>
		<RowDefinition Height="Auto"/>
		<RowDefinition Height="*"/>
	<ComboBox Name="Page" Grid.Row="0" HorizontalAlignment="Stretch" SelectionChanged="Page_SelectionChanged"/>
	<ScrollViewer Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center">
		<Image Name="Display" Stretch="None"/>

It should appear as such:


Step 11

From the Menu choose View and then Code


Step 12

Once in the Code View, below the public MainPage() { … } the following Code should be entered:

Library library = new Library();

private async void Open_Click(object sender, RoutedEventArgs e)
	uint pages = await library.OpenAsync();
	if (pages > 0)
		Page.ItemsSource = library.Numbers(Convert.ToInt32(pages));
		Page.SelectedIndex = 0;

private async void Page_SelectionChanged(object sender, SelectionChangedEventArgs e)
	uint.TryParse(Page.SelectedItem.ToString(), out uint page);
	Display.Source = await library.ViewAsync(page);

It should then appear as such:


Step 13

That completes the Universal Windows Application so Save the Project then in Visual Studio select the Local Machine to run the Application


Step 14

Once started the Application should then appear


Step 15

After the Application has started running you can then select Open launch a FileOpenPicker then you need to select a pdf document to open. Once done then you can select the ComboBox to choose which page of the document to display but it will start with the first.


Step 16

To Exit the application select Stop in Visual Studio


Creative Commons License

One thought on “Universal Windows Platform – PdfView App

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s