# 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**

```XAML

<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**

```XAML
// 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**

```XAML
// 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**

```C#
this.pdfViewer.DefaultImportSettings = PdfImportSettings.ReadAllAtOnce;
```

Additionally, you can determine the mode in the constructor of the `PdfDocumentSource` class.

#### **Set the loading mode by using the PdfDocumentSource constructor**

```C#
this.pdfViewer.DocumentSource = new PdfDocumentSource(new Uri("ms-appx:///Sample.pdf", UriKind.Absolute), PdfImportSettings.ReadOnDemand);
```

The `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**

```C#
ExtensibilityManager.RegisterPasswordRequiredDialog(new PasswordRequiredDialog());
```

To add the dialog through XAML, use the `RegisterPasswordRequiredDialog` property of the `RadPdfViewerAttachedComponents` class.

#### **Register the PasswordRequiredDialog through XAML**

```XAML
<telerik:RadPdfViewer telerik:RadPdfViewerAttachedComponents.RegisterPasswordRequiredDialog="True" />
```

As a result, when users open a password-protected file, the PdfViewer will show the following dialog.

![WinUI RadPdfViewer PdfViewer Password Dialog](https://www.telerik.com/winui/documentation/assets/77cbd81230a9c4da1a58e95fcf573dc4/radpdfviewer-open-file-0.png)

### 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**

```C#
pdfViewer.DefaultImportSettings.UserPasswordNeeded += DefaultImportSettings_UserPasswordNeeded;
```

#### **Set the password**

```C#
private void DefaultImportSettings_UserPasswordNeeded(object sender, Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Import.PasswordNeededEventArgs e)
{
	e.Password = "password123456";
}
```

## See Also

* [Getting Started with the Telerik UI for WinUI PdfViewer](https://www.telerik.com/winui/documentation/controls/radpdfviewer/getting-started)
* [Saving Files](https://www.telerik.com/winui/documentation/controls/radpdfviewer/file-operations/save-as)
