Search code examples
xamlpdf.net-corewinui-3winui

How display PDF file in WinUI 3 Desktop App


I have Desktop aplication in C# Net Core WinUI 3 that encrypt files. I success to decrypt PDF file and now I want to Display the file in my application. I have decrypt PDF file in stream and from this stream I want to display the file in my application without use with another program and without install external program. I need only Display read-only PDF file without edit option. In Addition my App need run in offline mode.

I tried WebView2 but need install WebView2 in computer. I also try convert PDF to Image but i need exteral program. What can I do?


Solution

  • You can use the PdfDocument class to load a PDF file.

    Let me show you an example with Drag and Drop feature.

    Shell.xaml

    <Page
        x:Class="PdfDemo.Shell"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:local="using:PdfDemo"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
        mc:Ignorable="d">
    
        <Grid RowDefinitions="Auto,*">
            <Grid
                x:Name="DroppingSpaceGrid"
                Grid.Row="0"
                AllowDrop="True"
                Background="Transparent"
                DragOver="DroppingSpaceGrid_DragOver"
                Drop="DroppingSpaceGrid_Drop">
                <TextBlock
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    Text="Drop your PDF here." />
            </Grid>
            <GridView
                Grid.Row="1"
                ItemsSource="{x:Bind Pages, Mode=OneWay}">
                <GridView.ItemTemplate>
                    <DataTemplate x:DataType="BitmapImage">
                        <Image Source="{x:Bind}" />
                    </DataTemplate>
                </GridView.ItemTemplate>
            </GridView>
        </Grid>
    
    </Page>
    

    Shell.xaml.cs

    using Microsoft.UI.Xaml;
    using Microsoft.UI.Xaml.Controls;
    using Microsoft.UI.Xaml.Media.Imaging;
    using System;
    using System.Collections.ObjectModel;
    using System.Linq;
    using Windows.ApplicationModel.DataTransfer;
    using Windows.Data.Pdf;
    using Windows.Storage;
    using Windows.Storage.Streams;
    
    namespace PdfDemo;
    
    public sealed partial class Shell : Page
    {
        public Shell()
        {
            InitializeComponent();
        }
    
        private void DroppingSpaceGrid_DragOver(object sender, DragEventArgs e)
        {
            e.AcceptedOperation = DataPackageOperation.Copy;
            e.DragUIOverride.IsCaptionVisible = false;
            e.DragUIOverride.IsGlyphVisible = false;
        }
    
        private async void DroppingSpaceGrid_Drop(object sender, DragEventArgs e)
        {
            if ((await e.DataView.GetStorageItemsAsync()).FirstOrDefault() is not StorageFile file ||
                file.FileType is not ".pdf")
            {
                return;
            }
    
            PdfDocument pdfDocument = await PdfDocument.LoadFromFileAsync(file);
    
            Pages.Clear();
    
            for (int i = 0; i < pdfDocument.PageCount; i++)
            {
                PdfPage pdfPage = pdfDocument.GetPage((uint)i);
                using var stream = new InMemoryRandomAccessStream();
                await pdfPage.RenderToStreamAsync(stream);
                var bitmapImage = new BitmapImage();
                await bitmapImage.SetSourceAsync(stream);
                Pages.Add(bitmapImage);
            }
        }
    
        public ObservableCollection<BitmapImage> Pages { get; } = [];
    }