在UWP应用程序中并排查看PDF文件

时间:2017-11-30 10:06:21

标签: xaml uwp uwp-xaml

我需要与我的应用程序并排的pdf查看器,以便我能够查看在应用程序中进行任何更改时生成的pdf。在Windows 8.1应用程序中,PDF是使用Reader应用程序打开的,默认情况下它会并排打开PDF。

但Win10中的情况并不相同。在第10场胜利中,读者应用程序单独打开推回应用程序屏幕。在胜利8.1中有任何类似的查看器

下图来自Windows 8.1 app

1 个答案:

答案 0 :(得分:1)

以下是在应用程序中打开PDF文件的解决方案

像往常一样,我从文件中打开PDF作为存储文件。

composer install

现在,PDF文档逐页读取为BitmapImage并添加到列表中。

StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/pdffile.pdf"));
Windows.Data.Pdf.PdfDocument doc = await Windows.Data.Pdf.PdfDocument.LoadFromFileAsync(file);
Load(doc);

将ObservableCollection绑定到ItemsControl,我们可以将PDF视为图像。

public async void Load(PdfDocument pdfDoc)
    {
        PdfPages.Clear();

        for (uint i = 0; i < pdfDoc.PageCount; i++)
        {
            BitmapImage image = new BitmapImage();

            var page = pdfDoc.GetPage(i);

            using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
            {
                await page.RenderToStreamAsync(stream);
                await image.SetSourceAsync(stream);
            }

            PdfPages.Add(image);
        }
    }

    public ObservableCollection<BitmapImage> PdfDocPages
    {
        get;
        set;
    } = new ObservableCollection<BitmapImage>();