Edit this page

Showing a File

This help article discusses different ways of loading a document in RadPdfViewer and their customization options.

Showing a PDF file

RadPdfViewer exposes a DocumentSource property of type FixedDocumentStreamSource that can be used to pass a PDF file to the control. There are several ways to set the property.

Setting the DocumentSource in XAML

First, you need to declare the Telerik namespace as shown in Example 1.

[XAML] Example 1: Define Telerik namespace


After that, you can proceed to add the PdfViewer control to the user control as shown in Example 2:

[XAML] Example 2: Create RadPdfViewer

    <telerik:RadPdfViewer x:Name="pdfViewer" DocumentSource="PdfViewerDemo;component/SampleData/Sample.pdf"/>

In Example 2, a file named "Sample.pdf" embedded in a "Sample Data" folder in the WPF project will be shown when the page is loaded. PdfViewerDemo is the name of the WPF project.

PDF files can be opened as long as you can obtain a stream with their content that supports Read and Seek operations. If the stream supports only Read, its content should be copied to a MemoryStream, which will enable the Seek operation as well and will facilitate RadPdfViewer to show the document. Notice that the document is loaded asynchronously and the stream shouldn't be closed.

Setting DocumentSource in Code-Behind

In code-behind, you can set the DocumentSource to either a URI or a stream.

Example 3 shows how a PDF file can be loaded from a file embedded as a resource through a stream:

[C#] Example 3: Load PDF from a stream

private void LoadFromStream(object sender, System.Windows.RoutedEventArgs e)
    Stream str = App.GetResourceStream(new System.Uri("PdfViewerDemo;component/SampleData/Sample.pdf", System.UriKind.Relative)).Stream;
    this.pdfViewer.DocumentSource = new PdfDocumentSource(str);

Example 4 shows how a PDF can be loaded from a file embedded as a resource by passing its URI:

[C#] Example 4: Load PDF from a URI

private void LoadFromUri(object sender, System.Windows.RoutedEventArgs e)
    this.pdfViewer.DocumentSource = new PdfDocumentSource(new System.Uri("PdfViewerDemo;component/SampleData/Sample.pdf", System.UriKind.Relative)); 

In Example 3 and Example 4, PdfViewerDemo is the name of the project and the PDF file is embedded as a resource in a folder called SampleData.

Setting the Document in Code-Behind

PdfDocumentSource internally uses the PdfFormatProvider class to create a document for RadPdfViewer. This allows you to easily import your documents directly using PdfFormatProvider instead of PdfDocumentSource.

[C#] Example 5: Set RadFixedDocument through PdfFormatProvider

string pdfFilePath = "Sample.pdf";
MemoryStream stream = new MemoryStream();

using (Stream input = File.OpenRead(pdfFilePath))

FormatProviderSettings settings = new FormatProviderSettings(ReadingMode.OnDemand);
PdfFormatProvider provider = new PdfFormatProvider(stream, settings);
RadFixedDocument doc = provider.Import();
this.pdfViewer.Document = doc;

Binding the DocumentSource of RadPdfViewer

The DocumentSource of RadPdfViewer is implemented as a dependency property and can be bound to a URI, string representing a URI, or a stream. This is done with the help of PdfDocumentSourceValueConverter, which caters to creating a PdfDocumentSource and presenting it to the viewer.

Example 6 and Example 7 show how to declare the converter:

[XAML] Example 6: Define a Fixed namespace


[XAML] Example 7: Define a PdfDocumentSourceValueConverter

    <fixed:PdfDocumentSourceValueConverter x:Key="PdfDocumentSourceValueConverter" />

The bound property can be implemented as shown in Example 8 and Example 9.:

[C#] Example 8: Create the property

public class Context
    public [string/Uri/Stream] Source { get; set; }

Notice that regardless of the type of the property you choose to bind the DocumentSource of the PdfViewer to, the XAML is identical.

[XAML] Example 9: Bind the DocumentSource property of RadPdfViewer

<telerik:RadPdfViewer x:Name="viewer" DocumentSource="{Binding Source, Converter={StaticResource PdfDocumentSourceValueConverter}}" />

Specifying Reading Mode

Starting with the release of R2 2016, you can control the way the documents are loaded in RadPdfViewer. You can choose between loading the whole document at one time (as done in versions prior R2 2016) or loading it page by page. The second option means that you no longer needed to parse the entire document in order to show it.

You can control how the document is loaded by setting the PdfViewer.DefaultFormatProviderSettings property. This property is used when the OpenPdfDocumentCommand is triggered to open a PDF file and could be set to one of the settings exposed by the FormatProviderSettings class:

  • ReadAllAtOnce: This value is used by default. When ReadAllAtOnce is applied, the document stream will be copied into a memory stream, which is used by PdfViewer. When you are loading a document through an URI in combination with ReadAllAtOnce, the stream to the resource will be closed as soon as it is copied to memory.

  • ReadOnDemand: Each page of the document is loaded dynamically when necessary (to be shown in the PdfViewer, printed, etc.) and it is unloaded once it becomes invisible. The stream that holds the document stays opened while the document is used in PdfViewer.

[C#] Example 10: Setting DefaultFormatProviderSettings

this.pdfViewer.DefaultFormatProviderSettings = FormatProviderSettings.ReadOnDemand;

Another option to set the reading mode is through the constructors that accept FormatProviderSettings when creating a new instance of the PdfDocumentSource class. The FormatProviderSettings.ReadAllAtOnce predefined settings are used by default. The PdfDocumentSource used in combination with ReadAllAtOnce will throw the Loaded event after applying the document to the PdfViewer.

If you want to change this behavior to read the document pages when needed, you can create a new PdfDocumentSource instance with FormatProviderSettings.ReadOnDemand predefined settings.

[C#] Example 11: Setting FormatProviderSettings

PdfDocumentSource source = new PdfDocumentSource(stream/uri, FormatProviderSettings.ReadOnDemand);      

When you bind the DocumentSource property of RadPdfViewer and want to control the way bound documents are loaded, you can pass the desired FormatProviderSettings as a ConverterParameter.

[XAML] Example 11: Setting FormatProviderSettings

<telerik:RadPdfViewer x:Name="viewer" DocumentSource="{Binding Uri, Converter={StaticResource PdfDocumentSourceValueConverter}, ConverterParameter=ReadOnDemand}" />

ReadAllAtOnce” and “ReadOnDemand” are the valid values you can specify.

See Also