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 

Loading..

User Comment

Loading comments..