Open PDFs from In-Memory Data in UWP

PSPDFKit for Windows supports a number of different ways of opening documents. This guide explains how to use IBuffer to open a document.

Note that UWP introduces very specific rules around file access permissions, and it’s worth reading about them here if you’re not familiar.

Microsoft also has several guide articles that cover topics such as working with file pickers, tracking recently used files, and much more.

Introduction

For opening a document, you must have added a PdfView to your desired XAML. You’ll also need to assign a Name for interacting with it through code. In this example, thePdfView is simply named PDFView:

<pspdfkit:PdfView License="{StaticResource PSPDFKitLicense}" Name="PDFView"/>

The rest of the process needs to be done after the PdfView Control is loaded. The easiest way to ensure that is the case is by using the PdfView.InitializationCompletedHandler event:

PDFView.InitializationCompletedHandler += (pdfView, document) =>
{
    // ...
};

If you don’t intend to open a document straight away, and you know that the PdfView will always be initialized, you can skip this step and open the document whenever it’s convenient by referring to the following sections.

Opening the Document

First, you’ll need to get your buffer containing the document from somewhere. In this example, you’ll read the buffer from a StorageFile, but it can originate from wherever is more convenient for your use case, as long as it’s an IBuffer:

var buffer = await FileIO.ReadBufferAsync(file);

Then, to display the document, you must use the relevant creator method from DocumentSource. These creators instantiate a DocumentSource, which is used by the PdfView like so:

// Open and display the PDF in the PSPDFKit `PdfView`.
var documentSource = DocumentSource.CreateFromBuffer(buffer);
await PDFView.Controller.ShowDocumentAsync(documentSource);