locked
How to render more that 1 page PDF using Windows.Data.PDF

    Question

  • I used below PDF Viewer sample :

    https://code.msdn.microsoft.com/windowsapps/PDF-viewer-sample-85a4bb30/view/SourceCode#content

    This sample only show how to render a single page PDF. What if I have more than 1 page, how this problem can be solved? or can this app handle this case?

    Any help is much appreciated.

    private async Task RenderPDFPage(string pdfFileName,RENDEROPTIONS renderOptions)
            {
                try
                {        
                     StorageFile pdfFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(pdfFileName);
                     //Load Pdf File
                    
                     _pdfDocument = await PdfDocument.LoadFromFileAsync(pdfFile); ;

                    if (_pdfDocument != null && _pdfDocument.PageCount > 0)
                    {
                        //Get Pdf page
                        var pdfPage = _pdfDocument.GetPage(PDF_PAGE_INDEX);

                        var Count = _pdfDocument.PageCount;


                        if (pdfPage != null)
                        {
                            // next, generate a bitmap of the page
                            StorageFolder tempFolder = ApplicationData.Current.TemporaryFolder;
                                            
                            StorageFile jpgFile = await tempFolder.CreateFileAsync(Guid.NewGuid().ToString() + ".png", CreationCollisionOption.ReplaceExisting);
                           
                            if (jpgFile != null)
                            {
                                IRandomAccessStream randomStream = await jpgFile.OpenAsync(FileAccessMode.ReadWrite);
                               
                                PdfPageRenderOptions pdfPageRenderOptions = new PdfPageRenderOptions();
                                switch (renderOptions)
                                {
                                    case RENDEROPTIONS.NORMAL:
                                        //Render Pdf page with default options
                                        await pdfPage.RenderToStreamAsync(randomStream);                                   
                                        break;
                                    case RENDEROPTIONS.ZOOM:
                                        //set PDFPageRenderOptions.DestinationWidth or DestinationHeight with expected zoom value
                                        Size pdfPageSize = pdfPage.Size;
                                        pdfPageRenderOptions.DestinationHeight = (uint)pdfPageSize.Height * ZOOM_FACTOR;
                                        //Render pdf page at a zoom level by passing pdfpageRenderOptions with DestinationLength set to the zoomed in length
                                        await pdfPage.RenderToStreamAsync(randomStream, pdfPageRenderOptions);
                                        break;
                                    case RENDEROPTIONS.PORTION:
                                        //Set PDFPageRenderOptions.SourceRect to render portion of a page
                                        pdfPageRenderOptions.SourceRect = PDF_PORTION_RECT;                                                                    
                                        //Render portion of a page
                                        await pdfPage.RenderToStreamAsync(randomStream, pdfPageRenderOptions);
                                        break;
                                }
                                await randomStream.FlushAsync();
                               
                                randomStream.Dispose();
                                pdfPage.Dispose();

                                await DisplayImageFileAsync(jpgFile);
                            }
                        }
                    }
                }
                catch (Exception err)
                {
                    rootPage.NotifyUser("Error: " + err.Message, NotifyType.ErrorMessage);

                }
            }
            /// <summary>
            /// This is the click handler for the 'RenderPage' button. 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private  async void RenderPage_Click(object sender, RoutedEventArgs e)
            {

                try
                {
                    rootPage.NotifyUser("Rendering page...", NotifyType.StatusMessage);
                  
                    await RenderPDFPage(PDFFILENAME, RENDEROPTIONS.NORMAL);
                  
                    rootPage.NotifyUser("Rendered page ", NotifyType.StatusMessage);

                }
                catch (Exception err)
                {
                    rootPage.NotifyUser("Error: " + err.Message, NotifyType.ErrorMessage);               
                }
            }

    Monday, December 8, 2014 2:28 PM

Answers

  • Hi Machine7,

    Passing different pageIndex to GetPage method can get the other page of PDF, please try and let me know the result.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Tuesday, December 9, 2014 8:14 AM
    Moderator