Convert PDF Files To Images In Windows 10 Universal App

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.

  1. StorageFile file = null;  
  2.   
  3. StorageFile file = null;  
  4. FileOpenPickerfilePicker = new FileOpenPicker();  
  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();  
After selecting the file, properties of the selected file are assigned to storage file instance. Now load the pdf file to pdfDocument class.
  1. 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.
  1. PdfDocumentpdfDocument = await PdfDocument.LoadFromFileAsync(selectedFile);;  
  2. if(pdfDocument != null && pdfDocument.PageCount > 0)  
  3. {  
  4.     for(intpageIndex = 0; pageIndex < pdfDocument.PageCount; pageIndex++)  
  5.     {  
  6.         varpdfPage = pdfDocument.GetPage((uint) pageIndex);  
  7.         if(pdfPage != null)  
  8.         {  
  9.             StorageFoldertempFolder = ApplicationData.Current.TemporaryFolder;  
  10.             StorageFiledestinationFile = await KnownFolders.CameraRoll.CreateFileAsync(Guid.NewGuid().ToString() + ".jpg");  
  11.             if(destinationFile != null)  
  12.             {  
  13.                 IRandomAccessStreamrandomStream = await destinationFile.OpenAsync(FileAccessMode.ReadWrite);  
  14.                 PdfPageRenderOptionspdfPageRenderOptions = new PdfPageRenderOptions();  
  15.                 pdfPageRenderOptions.DestinationWidth = (uint)(this.ActualWidth - 130);  
  16.                 awaitpdfPage.RenderToStreamAsync(randomStream, pdfPageRenderOptions);  
  17.                 awaitrandomStream.FlushAsync();  
  18.                 randomStream.Dispose();  
  19.                 pdfPage.Dispose();  
  20.             }  
  21.         }  
  22.     }  
  23. }  
Full source code looks like the following code:
  1. private async void selectFile_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     StorageFile file = null;  
  4.     FileOpenPickerfilePicker = new FileOpenPicker();  
  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.     LoadPdfFileAsync(file);  
  12. }  
  13. private async System.Threading.Tasks.TaskLoadPdfFileAsync(StorageFileselectedFile)  
  14. {  
  15.     try  
  16.     {  
  17.         PdfDocumentpdfDocument = await PdfDocument.LoadFromFileAsync(selectedFile);;  
  18.         if(pdfDocument != null && pdfDocument.PageCount > 0)  
  19.         {  
  20.             for(intpageIndex = 0; pageIndex < pdfDocument.PageCount; pageIndex++)  
  21.             {  
  22.                 varpdfPage = pdfDocument.GetPage((uint) pageIndex);  
  23.                 if(pdfPage != null)  
  24.                 {  
  25.                     StorageFoldertempFolder = ApplicationData.Current.TemporaryFolder;  
  26.                     StorageFiledestinationFile = await KnownFolders.CameraRoll.CreateFileAsync(Guid.NewGuid().ToString() + ".jpg");  
  27.                     if(destinationFile != null)  
  28.                     {  
  29.                         IRandomAccessStreamrandomStream = await destinationFile.OpenAsync(FileAccessMode.ReadWrite);  
  30.                         PdfPageRenderOptionspdfPageRenderOptions = new PdfPageRenderOptions();  
  31.                         pdfPageRenderOptions.DestinationWidth = (uint)(this.ActualWidth - 130);  
  32.                         awaitpdfPage.RenderToStreamAsync(randomStream, pdfPageRenderOptions);  
  33.                         awaitrandomStream.FlushAsync();  
  34.                         randomStream.Dispose();  
  35.                         pdfPage.Dispose();  
  36.                     }  
  37.                 }  
  38.             }  
  39.         }  
  40.     }  
  41.     catch(Exception ex)  
  42.     {  
  43.         throw ex;  
  44.     }  
  45. }  
Now run the app and select the PDF file. You can see the PDF file converted as image like the following output.

run

Source Code.

For more information on Windows 10 UWP, refer to my eBook:
Read more articles on Universal Windows App: