Windows应用商店的应用程序 - 显示PDF

Windows应用商店的应用程序 - 显示PDF

问题描述:

我创建一个Windows商店应用程序(以前称为地铁的应用程序),它能够读取并显示多种不同类型的文件(JPG格式,WMV,PDF等)。每个文件类型使用适当的XAML控件显示(如JPG利用图像和WMV使用的MediaElement)。我所遇到的一个问题是显示的PDF文件。看来我必须把它转换为图像来显示。使用Magick.NET我已经调查但针对.NETFramework而非.NETCore。我已经找到了其他框架需要许可证。有没有显示我的应用程序中的PDF解决方案?

I am creating a Windows store application (formerly called Metro app) that is able to read in and display several different file types (jpg, wmv, pdf, etc). Each file type is displayed using the appropriate XAML control (eg. jpg uses Image and wmv uses MediaElement). A problem I have come across is displaying PDFs. It seems I will have to convert it to an image to display. I have investigated using Magick.NET but that targets .NETFramework rather than .NETCore. Other frameworks I have sought out require a license. Is there a solution to display a PDF within my application?

看第10分钟由内特钻石提供的视频后,渲染PDF是一个简单的任务。这是Windows 8.1的 PdfDocument PdfPage 一>类是新的版本。下面呈现一个 StorageFile (这是一个.pdf文件)为图像,并将它们放入一个垂直滚动堆叠面板( imagePanel )。

After watching the first 10 minutes of the video provided by Nate Diamond, rendering a PDF is a simple task. This is a solution for Windows 8.1 as the PdfDocument and PdfPage classes are new to the version. Below renders a StorageFile (which is a .pdf file) into images and puts them into a vertically scrolling stack panel (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);

        }
    } 



异步方法也可以跑。因为如果任务等待是不可取的。

The asynchronous methods can also be ran as tasks if awaiting is undesirable.

 PdfDocument pdf = PdfDocument.LoadFromFileAsync(file).AsTask().Result;