How to Convert PDF Files To Images In Windows 10 Universal App

How to Convert PDF Files To Images In Windows 10 Universal App

In this article you will learn how to convert PDF file in to the JPG without any use of third party library. 


1: Add a button in application

2: Now on button click event past following code lines 


  private async void Button_Click(object sender, RoutedEventArgs e)
        {
            StorageFile file = null;
            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();

          await  LoadPdfFileAsync(file);
        }


3: Create a function name like ConvertPDFtoImages and past following code lines 

  private async System.Threading.Tasks.Task LoadPdfFileAsync(StorageFile selectedFile)
        {
            try
            {
                PdfDocument pdfDocument = await PdfDocument.LoadFromFileAsync(selectedFile); ;
                if (pdfDocument != null && pdfDocument.PageCount > 0)
                {
                    for (int pageIndex = 0; pageIndex < pdfDocument.PageCount; pageIndex++)
                    {
                        var pdfPage = pdfDocument.GetPage((uint)pageIndex);
                        if (pdfPage != null)
                        {
                            StorageFolder tempFolder = ApplicationData.Current.TemporaryFolder;
                            StorageFile destinationFile = await KnownFolders.CameraRoll.CreateFileAsync(Guid.NewGuid().ToString() + ".jpg");
                            if (destinationFile != null)
                            {
                                IRandomAccessStream randomStream = await destinationFile.OpenAsync(FileAccessMode.ReadWrite);
                                PdfPageRenderOptions pdfPageRenderOptions = new PdfPageRenderOptions();
                                pdfPageRenderOptions.DestinationWidth = (uint)(this.ActualWidth - 130);
                                await pdfPage.RenderToStreamAsync(randomStream, pdfPageRenderOptions);
                                await randomStream.FlushAsync();
                                randomStream.Dispose();
                                pdfPage.Dispose();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }        

    

4: Open project manifest file and got to "Capabilities" tab set "Picture Library" check box checked 

Run your application and convert PDF file to JPG

Please send your thoughts and quires in bellow comment box 


Thanks, for reading the blog, I hope it helps you. Please share this link on your social media accounts so that others can read our valuable content. Share your queries with our expert team and get Free Expert Advice for Your Business today.


About Writer

Ravinder Singh

Full Stack Developer
I have 12+ years of experience in commercial software development. I write this blog as a kind of knowledge base for myself. When I read about something interesting or learn anything I will write about it. I think when writing about a topic you concentrate more and therefore have better study results. The second reason why I write this blog is, that I love teaching and I hope that people find their way on here and can benefit from my content.

Hire me on Linkedin

My portfolio

Ravinder Singh Full Stack Developer