How Do I Programmatically Create A PdfView?

Q: How Do I Programmatically Create A PdfView?

  1. Initialize the SDK
  2. Create a PdfView object in the OnNavigatedTo handler of the Page
  3. Add a handler for PdfView.InitializationCompletedHandler
  4. Add the PdfView to the children of a UI element on the Page
  5. One initialization has been completed you can display a PDF.
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
namespace App1
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            PSPDFKit.Sdk.Initialize("YOUR_LICENSE_GOES_HERE");
        }

        private PdfView _pdfView;

        protected override void OnNavigatedTo(NavigationEventArgs e) 
        {
            _pdfView = new PdfView();
            _pdfView.InitializationCompletedHandler += PdfView_InitializationCompletedHandler;
            Grid.Children.Add(_pdfView);
        }

        private async void PdfView_InitializationCompletedHandler(PdfView sender, PSPDFKit.Pdf.Document args)
        {
            var storageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/A.pdf"));
            _pdfView.PdfFileSource = storageFile;
        }
    }
}
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid Name="Grid">

    </Grid>
</Page>