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.

Example 1: Define Telerik namespace

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

Make sure to set the Build Action of the file to Resource so it can be accessed through URI.

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 Silverlight project will be shown when the page is loaded. PdfViewerDemo is the name of the project.

You can also load files that are not on the client machine. For example, if you embed a Sample.pdf file in the Web project, you can load it like this:

<telerik:RadPdfViewer x:Name="pdfViewer" DocumentSource="/Sample.pdf"/> 

PDF files can be opened as long as you can obtain a stream with their content that supports Read and Seek operations. Note that due to Silverlight's security policy, only files from a domain with permissive cross-domain policy can be loaded. Furthermore, 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:

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:

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.

The PdfDocumentSource class exposes the Loaded event. This event is not related to the parsing of the content but is fired just after the document is being imported.

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.

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:

Example 6: Define a Fixed namespace


Example 7: Define a PdfDocumentSourceValueConverter

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

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

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.

Example 9: Bind the DocumentSource property of RadPdfViewer

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

Specifying Reading Mode

You can control the way the documents are loaded in RadPdfViewer. You can choose between loading the whole document at one time 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. However, you will need to keep the stream to the document file open.

You can control how the document is loaded by setting the PdfViewer.DefaultFormatProviderSettings property.

  • 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.

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 ReadOnDemand predefined settings are used by default.

If you want to change this behavior to read all the document pages at the beginning of the import, you can create a new PdfDocumentSource instance with FormatProviderSettings.ReadAllAtOnce predefined settings.

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 settings as a ConverterParameter.

Example 12: Applying settings in binding scenario

<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

In this article