Syncfusion AI Assistant

How can I help you?

Viewing PDF in UWP PDF Viewer (SfPdfViewer)

23 May 20268 minutes to read

The SfPdfViewer allows you to load the PDF documents from the stream, storageFile, and PdfLoadedDocument object.

Loading a PDF using PdfLoadedDocument object

The SfPdfViewer allows you to load the PDF document synchronously and asynchronously from the specified pdfLoadedDocument object using the LoadDocument and LoadDocumentAsync methods respectively.

The following code explains how to load the document synchronously using a PdfLoadedDocument object that was created from the PDF in the Assets folder of the application.

Assembly assembly = typeof(MainPage).GetTypeInfo().Assembly;
//Loads the stream from the embedded resource.
fileStream = assembly.GetManifestResourceStream("ApplicationNameSpace.Assets.PDFFileName.pdf");
byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, buffer.Length);
//Create a new PDFLoadedDocument object.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(buffer);
pdfViewer.LoadDocument(loadedDocument);
'Create a new PDFLoadedDocument object.
Dim loadedDocument As New PdfLoadedDocument(Buffer)
pdfViewer.LoadDocument(loadedDocument)

The following code explains how to load the document asynchronously using the PdfLoadedDocument object that was created from the PDF in the Assets folder of the application.

//Create a new PDFLoadedDocument object.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(buffer);
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
await pdfViewer.LoadDocumentAsync(loadedDocument, cancellationTokenSource.Token);
pdfViewer.LoadDocument(loadedDocument);
'Create a new PDFLoadedDocument object.
Dim loadedDocument As New PdfLoadedDocument(Buffer)
Dim cancellationTokenSource As New CancellationTokenSource()
Await pdfViewer.LoadDocumentAsync(loadedDocument, cancellationTokenSource.Token)

In the above code sample, the CancellationToken enables you to cancel the asynchronous loading of a PDF document when it is in progress.

Loading a PDF using Stream object

The SfPdfViewer allows you to load the PDF document synchronously and asynchronously from the specified stream using the LoadDocument and LoadDocumentAsync methods respectively.

The following code explains how to load the document synchronously using a stream object that was created from the PDF in the Assets folder of the application.

Assembly assembly = typeof(MainPage).GetTypeInfo().Assembly; 
//Loads the stream from the embedded resource.
fileStream = assembly.GetManifestResourceStream("ApplicationNameSpace.Assets.PDFFileName.pdf"); 
pdfViewer.LoadDocument(fileStream);
'Loads the stream from the embedded resource. 
Dim assembly As Assembly = GetType(MainPage).GetTypeInfo().Assembly 
docStream = assembly.GetManifestResourceStream("ApplicationNameSpace. Assets.PDFFileName.pdf")
pdfViewer.LoadDocument(docStream)

The following code explains how to load the document asynchronously using a stream object that was created from the PDF in the Assets folder of the application.

Assembly assembly = typeof(MainPage).GetTypeInfo().Assembly; 
//Loads the stream from the embedded resource.
fileStream = assembly.GetManifestResourceStream("ApplicationNameSpace.Assets.PDFFileName.pdf"); 
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
await pdfViewer.LoadDocumentAsync(fileStream, cancellationTokenSource.Token);
Assembly assembly = typeof(MainPage).GetTypeInfo().Assembly; 
//Loads the stream from the embedded resource.
fileStream = assembly.GetManifestResourceStream("ApplicationNameSpace.Assets.PDFFileName.pdf"); 
Dim cancellationTokenSource As New CancellationTokenSource()
Await pdfViewer.LoadDocumentAsync(fileStream, cancellationTokenSource.Token)

In the above code sample, the CancellationToken enables you to cancel the asynchronous loading of a PDF document when it is in progress.

Loading a PDF using the StorageFile object

The SfPdfViewer allows you to load the PDF document synchronously and asynchronously from the specified storage file using the LoadDocument and LoadDocumentAsync methods respectively.

The following code explains how to load the document synchronously using a StorageFile object that was created from the PDF in the Assets folder of the application.

//Opens a file picker.
var picker = new FileOpenPicker();
picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
picker.ViewMode = PickerViewMode.List;
//Filters the PDF files in the documents library.
picker.FileTypeFilter.Add(".pdf");
StorageFile file = await picker.PickSingleFileAsync();
pdfViewer.LoadDocument(file);
'Opens a file picker. Dim picker = New FileOpenPicker()
picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary 
picker.ViewMode = PickerViewMode.List 
'Filters the PDF files in the documents library. 
picker.FileTypeFilter.Add(".pdf") 
Dim file = Await picker.PickSingleFileAsync()
pdfViewer.LoadDocument(file)

The following code explains how to load the document asynchronously using a StorageFile object that was created from the PDF in the Assets folder of the application.

//Opens a file picker.
var picker = new FileOpenPicker();
picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
picker.ViewMode = PickerViewMode.List;
//Filters the PDF files in the documents library.
picker.FileTypeFilter.Add(".pdf");
StorageFile file = await picker.PickSingleFileAsync();
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
await pdfViewer.LoadDocumentAsync(file, cancellationTokenSource.Token);
'Opens a file picker. Dim picker = New FileOpenPicker()
picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary 
picker.ViewMode = PickerViewMode.List 
'Filters the PDF files in the documents library. 
picker.FileTypeFilter.Add(".pdf") 
Dim file = Await picker.PickSingleFileAsync()
Dim cancellationTokenSource As New CancellationTokenSource()
Await pdfViewer.LoadDocument(file, cancellationTokenSource.Token);

In the above code sample, the CancellationToken enables you to cancel the asynchronous loading of a PDF document when it is in progress.

Unloading PDF document from the viewer

The SfPdfViewer control allows you to unload the PDF document from the viewer when the control is no longer in use. This releases the PDF document and all its associated resources.

private void Unload_Click(object sender, RoutedEventArgs e)
{
	//Unloads the PDF document displayed in the PDF Viewer Control.
	pdfViewer.Unload();
}

Saving a PDF document

The Save method returns the PDF document along with the changes made (annotations addition and modification) as a stream, which can be saved as a file.

//Save the PDF document.
Stream pdfDocumentStream = pdfViewerControl.Save();

NOTE

The Save method does not save stamp annotations in the PDF document. Use the SaveAsync method to save them.

Saving a PDF document asynchronously

The PDF Viewer also allows you to save and return the PDF document with the changes as a stream asynchronously using the SaveAsync method.

//Save the PDF document asynchronously.
Task<Stream> pdfDocumentStream = pdfViewerControl.SaveAsync();

Events to track PDF loading

The PDF Viewer control allows you to track PDF loading by using the DocumentLoaded event. This event is triggered after the document is properly loaded in the SfPdfViewer.

SfPdfViewerControl pdfViewer = new SfPdfViewerControl();

pdfViewer.DocumentLoaded += PdfViewer_DocumentLoaded;

private void PdfViewer_DocumentLoaded(object sender, DocumentLoadedEventArgs args)
{
	//Gets the total page count of the loaded PDF document.
	int pageCount = pdfViewer.PageCount;
}

Disposing the managed resources of PDF Viewer

The PDF Viewer control allows you to dispose the managed resources associated with the viewer. To do this, call the Dispose method of the PDF Viewer control as shown in the following code sample.

//Disposes all managed resources of SfPdfViewer.
pdfViewer.Dispose();

See Also