Universal Windows Platform – Tutorial Insider – PdfView App

Standard

comentsys.wordpress.com today features a Universal Windows Platform tutorial showing how to create a PdfView App which will allow you to Open and Page through PDF Documents.

Details

The Library Class defines the PdfDocument that can be opened later then there’s a Show method for displaying messages on-screen. The OpenAsync method uses the FileOpenPicker set to open a “.pdf” file and then sets the PdfDocument to be the opened file using PickSingleFileAsync with the LoadFromFileAsync method – if the document has been opened if it is password protected a message will be displayed to say that the example won’t open those, otherwise there’s an Exception handler to detect a PDF document that has failed to open due to being invalid or some other error during opening and this method also returns the number of pages in the PDF document as uint. There’s a ViewAsync which takes a uint as a parameter and then gets a PdfPage from the document using this and then this is rendered to a BitmapImage using the RenderToStreamAsync to set an InMemoryRandomAccessStream which is used to set the source of the BitmapImage with that page’s contents using SetSourceAsync. Theres also a Numbers method which helps generate a list of page numbers to be used.

The MainPage contains the Open button to allow a PDF Document to be opened and there’s a ComboBox that is populated with all possible page numbers based on the number of pages in the document – when a number is selected that uses the ViewAsync method to get that page. Finally there’s an Image control which is used to display the page and this is contained within a ScrollViewer so if the page is too wide or long it can be scrolled around to view all that page’s contents.

Behind the Scenes

This example was inspired from the Microsoft Universal Windows Platform (UWP) app samples and in particular the PDF document example, the main purpose of this example was to show how easy it is to open and navigate through a PDF Document – although password protected documents aren’t fully supported in the example it would be possible to open these if the correct password was provided to the LoadFromFileAsync method from a TextBox or value. It’s quite easy to support PDF documents this way and could be a useful way of integrating PDF documents into any application using this example as the start of that – you could even have it scroll through all the pages by using multiple Image controls and setting their sources accordingly to each page in the PDF document.

Advertisements

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