In my previous article we saw how to read PDF files in the Windows 10 universal app using the default PDF reader, but in some cases we need to read the PDF file within our application. For that we need to convert the PDF file to image and are able to see the PDF file within the application.
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 the code behind and write the following code. Create instance for StorageFile class to store the file details.
- StorageFile file = null;
-
- StorageFile file = null;
- FileOpenPickerfilePicker = 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 load the pdf file to
pdfDocument class.
- PdfDocumentpdfDocument = await PdfDocument.LoadFromFileAsync(selectedFile);
Now we can get the PDF file page count using that get the page stream and convert it to JPG image.
To convert PDF to image write the following code.
- PdfDocumentpdfDocument = await PdfDocument.LoadFromFileAsync(selectedFile);;
- if(pdfDocument != null && pdfDocument.PageCount > 0)
- {
- for(intpageIndex = 0; pageIndex < pdfDocument.PageCount; pageIndex++)
- {
- varpdfPage = pdfDocument.GetPage((uint) pageIndex);
- if(pdfPage != null)
- {
- StorageFoldertempFolder = ApplicationData.Current.TemporaryFolder;
- StorageFiledestinationFile = await KnownFolders.CameraRoll.CreateFileAsync(Guid.NewGuid().ToString() + ".jpg");
- if(destinationFile != null)
- {
- IRandomAccessStreamrandomStream = await destinationFile.OpenAsync(FileAccessMode.ReadWrite);
- PdfPageRenderOptionspdfPageRenderOptions = new PdfPageRenderOptions();
- pdfPageRenderOptions.DestinationWidth = (uint)(this.ActualWidth - 130);
- awaitpdfPage.RenderToStreamAsync(randomStream, pdfPageRenderOptions);
- awaitrandomStream.FlushAsync();
- randomStream.Dispose();
- pdfPage.Dispose();
- }
- }
- }
- }
Full source code looks like the following code:
- private async void selectFile_Click(object sender, RoutedEventArgs e)
- {
- StorageFile file = null;
- FileOpenPickerfilePicker = 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();
- LoadPdfFileAsync(file);
- }
- private async System.Threading.Tasks.TaskLoadPdfFileAsync(StorageFileselectedFile)
- {
- try
- {
- PdfDocumentpdfDocument = await PdfDocument.LoadFromFileAsync(selectedFile);;
- if(pdfDocument != null && pdfDocument.PageCount > 0)
- {
- for(intpageIndex = 0; pageIndex < pdfDocument.PageCount; pageIndex++)
- {
- varpdfPage = pdfDocument.GetPage((uint) pageIndex);
- if(pdfPage != null)
- {
- StorageFoldertempFolder = ApplicationData.Current.TemporaryFolder;
- StorageFiledestinationFile = await KnownFolders.CameraRoll.CreateFileAsync(Guid.NewGuid().ToString() + ".jpg");
- if(destinationFile != null)
- {
- IRandomAccessStreamrandomStream = await destinationFile.OpenAsync(FileAccessMode.ReadWrite);
- PdfPageRenderOptionspdfPageRenderOptions = new PdfPageRenderOptions();
- pdfPageRenderOptions.DestinationWidth = (uint)(this.ActualWidth - 130);
- awaitpdfPage.RenderToStreamAsync(randomStream, pdfPageRenderOptions);
- awaitrandomStream.FlushAsync();
- randomStream.Dispose();
- pdfPage.Dispose();
- }
- }
- }
- }
- }
- catch(Exception ex)
- {
- throw ex;
- }
- }
Now run the app and select the PDF file. You can see the PDF file converted as image like the following output.
Source
Code.
For more information on Windows 10 UWP, refer to my eBook:
Read more articles on Universal Windows App: