Read PDF Files In Windows 10 Universal App Using Default PDF Reader

Introduction 

 
Reading PDF files has involved third party libraries previously, but now in Windows 10, you can easily implement this using Launcher API.
 

Let’s see the steps

 
Create a new Windows 10 universal app. For creating a new Windows 10 universal project, refer to the following:
Now create one button to select the file. Go to the code behind and write the below code. Create an instance for a StorageFile class to store the file details.
 
StorageFile file = null;
 
Next, use FilePicker to select the file, here I have added filters to only view the PDF file,
  1. FileOpenPicker filePicker = new FileOpenPicker();    
  2. filePicker.FileTypeFilter.Add(".pdf");    
  3. filePicker.ViewMode = PickerViewMode.Thumbnail;    
  4. filePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;    
  5. filePicker.SettingsIdentifier = "picker1";    
  6. filePicker.CommitButtonText = "Open Pdf File";    
  7. file = await filePicker.PickSingleFileAsync();    
After selecting the file, the properties of the selected file are assigned to the storage file instance. Now using launcher launch the selected file by using the following code,
  1. Windows.System.Launcher.LaunchFileAsync(file);     
The full source code looks like the following.
  1. private async void selectFile_Click(object sender, RoutedEventArgs e)    
  2. {    
  3.     StorageFile file = null;    
  4.     FileOpenPicker filePicker = newFileOpenPicker();    
  5.     filePicker.FileTypeFilter.Add(".pdf");    
  6.     filePicker.ViewMode = PickerViewMode.Thumbnail;    
  7.     filePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;    
  8.     filePicker.SettingsIdentifier = "picker1";    
  9.     filePicker.CommitButtonText = "Open Pdf File";    
  10.     file = await filePicker.PickSingleFileAsync();    
  11.     Windows.System.Launcher.LaunchFileAsync(file);    
  12.     
  13. }    
In some cases, we need to view the file from the local folder without using file picker for that write the following code.
  1. StorageFile file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@ "Foldername\filename.pdf");    
  2. Windows.System.Launcher.LaunchFileAsync(file);    
Now run the application and see the output looks like the following image.
 
output
 
For more information on Windows 10 UWP, refer to my eBook:
Please share your comments on this article.
 
Read more articles on Windows 10

Summary

 
In this article, we learned about reading PDF Files In Windows 10 Universal App Using Default PDF Reader. 


Similar Articles