Windows Store Application - Show PDF

I am creating a Windows storage application (formerly called the Metro application) that can read and display several different types of files (jpg, wmv, pdf, etc.). Each file type is displayed using an appropriate XAML control (for example, jpg uses Image and wmv uses MediaElement). The problem I encountered is displaying PDF files. It seems I will have to convert it to an image to be displayed. I researched using Magick.NET, but this is about .NETFramework, not .NETCore. The other framework I was looking for requires a license. Is there a solution to display pdf in my application?

+3
source share
2 answers

After watching the first 10 minutes of the video provided by Nate Diamond, PDF rendering is an easy task. This solution is for Windows 8.1 as PdfDocument and PdfPage are new to the version. Below it displays StorageFile(which is a .pdf file) into images and places them in the stack pane with vertical scrolling ( imagePanel).

private async void renderPdf(StorageFile file)
    {
        imagePanel.Children.Clear();

        PdfDocument pdf = await PdfDocument.LoadFromFileAsync(file);

        for (uint pageNum = 0; pageNum < pdf.PageCount; pageNum++)
        {
            PdfPage page = pdf.GetPage(pageNum);

            InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();
            await page.RenderToStreamAsync(stream);

            BitmapImage source = new BitmapImage();
            source.SetSource(stream);

            Image pdfPage = new Image();

            pdfPage.HorizontalAlignment = HorizontalAlignment.Center;
            pdfPage.VerticalAlignment = VerticalAlignment.Center;
            pdfPage.Height = page.Size.Height;
            pdfPage.Width = page.Size.Width;
            pdfPage.Margin = new Thickness(0, 0, 0, 5);
            pdfPage.Source = source;

            imagePanel.Children.Add(pdfPage);

        }
    } 

Asynchronous methods can also run as tasks if waiting is undesirable.

 PdfDocument pdf = PdfDocument.LoadFromFileAsync(file).AsTask().Result;
+3
source

Checkout this video from the 2013 edition of PDF content rendering in Windows Store apps. If there is a way to do this, this is it.

+3
source

All Articles