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 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 he code behind and write the below code. Create instance for 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,
- FileOpenPicker filePicker = new FileOpenPicker();
- filePicker.FileTypeFilter.Add(".pdf");
- filePicker.ViewMode = PickerViewMode.Thumbnail;
- filePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
- filePicker.SettingsIdentifier = "picker1";
- filePicker.CommitButtonText = "Open Pdf File";
- file = await filePicker.PickSingleFileAsync();
After selecting the file, properties of the selected file are assigned to storage file instance. Now using launcher launch the selected file by using the following code,
- Windows.System.Launcher.LaunchFileAsync(file);
Full source code looks like the following.
- private async void selectFile_Click(object sender, RoutedEventArgs e)
- {
- StorageFile file = null;
- FileOpenPicker filePicker = newFileOpenPicker();
- filePicker.FileTypeFilter.Add(".pdf");
- filePicker.ViewMode = PickerViewMode.Thumbnail;
- filePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
- filePicker.SettingsIdentifier = "picker1";
- filePicker.CommitButtonText = "Open Pdf File";
- file = await filePicker.PickSingleFileAsync();
- Windows.System.Launcher.LaunchFileAsync(file);
-
- }
In some case we need to view the file from local folder without using file picker for that write the following code.
- StorageFile file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@ "Foldername\filename.pdf");
- Windows.System.Launcher.LaunchFileAsync(file);
Now run the application and see the output looks like the following image.
For more information on Windows 10 UWP, refer to my eBook:
Please share your comments on this article.
Read more articles on Windows 10: