Opening Files
This article describes the API used to open and load PDF files into the PdfViewer.
To render files, set the DocumentSource
property of RadPdfViewer
. The property is of type PdfDocumentSource
which allows you to provide a file Stream
or Uri
that is used to load the PDF document model.
Setting Document Source in XAML
The built-in type converter allows you to provide a string path to the DocumentSource
which should point to the PDF file. This will create a PdfDocumentSource
object and assign it to the source property.
Setting the document source in XAML
<!-- to use this approach to get the file, you will need to include it in the project and set its BuildAction to Content -->
<telerik:RadPdfViewer DocumentSource="ms-appx:///Sample.pdf" />
Setting the Document Source in Code
To set the DocumentSource
in code, use the PdfDocumentSource
class.
Setting the document source in code, using a Stream
// to use this approach to get the file, you will need to include it in the project and set its BuildAction to EmbeddedResource
Assembly executingAssembly = typeof(App).GetTypeInfo().Assembly;
Stream inputStream = executingAssembly.GetManifestResourceStream(executingAssembly.GetName().Name + ".Sample.pdf");
this.pdfViewer.DocumentSource = new PdfDocumentSource(inputStream);
Setting the document source in code, using a Uri
// to use this approach to get the file, you will need to include it in the project and set its BuildAction to Content
this.pdfViewer.DocumentSource = new PdfDocumentSource(new Uri("ms-appx:///Sample.pdf", UriKind.Absolute));
Document Loading Mode
By default the document is read on demand - page by page. To change this and load the whole document at once, you can set the loading mode of the document to ReadAllAtOnce
. The default value is OnDemand
.
The loading mode can be set by setting the DefaultImportSettings
property of RadPdfViewer
.
Setting the loading mode
this.pdfViewer.DefaultImportSettings = PdfImportSettings.ReadAllAtOnce;
PdfDocumentSource
class.
Setting the loading mode using the PdfDocumentSource constructor
this.pdfViewer.DocumentSource = new PdfDocumentSource(new Uri("ms-appx:///Sample.pdf", UriKind.Absolute), PdfImportSettings.ReadOnDemand);
ReadAllAtOnce
option will load and parse the entire document. Once the document is loaded completely the stream to the associated file will be closed.
The ReadOnDemand
option will load pages from the document when the viewport changes, and unload them when no longer needed. This functionality requires for the control to create a copy of the opened file stream and keep it open while the document is used by the PdfViewer.
Password Protected Files
To open PDF files protected with password, use the UserPasswordNeeded
event of the associated PdfImportSettings
object. The event arguments allow you to provide a password for the opened document.
Subscribing to the UserPasswordNeeded event
pdfViewer.DefaultImportSettings.UserPasswordNeeded += DefaultImportSettings_UserPasswordNeeded;
Setting the password
private void DefaultImportSettings_UserPasswordNeeded(object sender, Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Import.PasswordNeededEventArgs e)
{
e.Password = "password123456";
}