Opening Files
The PdfViewer provides API properties which enable you to open and load PDF files.
To render files in the PdfViewer, set the DocumentSource
property of the RadPdfViewer
. DocumentSource
is of type PdfDocumentSource
which allows you to provide a Stream
or Uri
file that is used to load the PDF document model.
Setting the Document Source in XAML
The built-in type converter allows you to provide a string path to the DocumentSource
which has to point to the PDF file. This setup will create a PdfDocumentSource
object and assign it to the source property.
Set the document source in XAML
<!-- When you use this approach to get the file, you 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.
Set the document source in code, using a Stream
// When you use this approach to get the file, you 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);
Set the document source in code by using a Uri
// When you use this approach to get the file, you 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 behavior and load the whole document at once, set the loading mode of the document to ReadAllAtOnce
. The default value is OnDemand
.
You can set the loading mode by setting the DefaultImportSettings
property of RadPdfViewer
.
Set the loading mode
this.pdfViewer.DefaultImportSettings = PdfImportSettings.ReadAllAtOnce;
PdfDocumentSource
class.
Set the loading mode by 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 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
If users open a PDF file that is protected with a password, you can show a dialog that prompts them to input the password for this document.
To use a password prompt dialog, register it first:
Register the PasswordRequiredDialog by using the ExtensibilityManager
ExtensibilityManager.RegisterPasswordRequiredDialog(new PasswordRequiredDialog());
RegisterPasswordRequiredDialog
property of the RadPdfViewerAttachedComponents
class.
Register the PasswordRequiredDialog through XAML
<telerik:RadPdfViewer telerik:RadPdfViewerAttachedComponents.RegisterPasswordRequiredDialog="True" />
Setting Passwords in Code-Behind
To open password-protected PDF files without prompting the user with a dialog but set the password in code-behind, use the UserPasswordNeeded
event of the associated PdfImportSettings
object. The event arguments allow you to provide a password for the opened document.
Subscribe to the UserPasswordNeeded event
pdfViewer.DefaultImportSettings.UserPasswordNeeded += DefaultImportSettings_UserPasswordNeeded;
Set the password
private void DefaultImportSettings_UserPasswordNeeded(object sender, Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Import.PasswordNeededEventArgs e)
{
e.Password = "password123456";
}