Key Features

The purpose of this help article is to show you the key features of the RadPdfViewer control.

Pdf Document Visualization

RadPdfViewer control enables you to visualize Pdf documents through the Source property of type Telerik.XamarinForms.PdfViewer.DocumentSource.

The Pdf Document could be loaded from:

  • RadFixedDocument - it is used to load the pdf document from a stream.

Using this approach you have more control over the loading process, for example, you could modify the document after it is imported and before it is assigned as a Source to the PdfViewer control. For more details check RadFixedDocument topic from RadPdfProcessing documentation.

You could use it in two ways:

private void ImportFixedDocument()
{
    Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider provider = new Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider();
    Assembly assembly = typeof(KeyFeatures).Assembly;
    string fileName = assembly.GetManifestResourceNames().FirstOrDefault(n => n.Contains("pdfviewer-overview.pdf"));
    using (Stream stream = assembly.GetManifestResourceStream(fileName))
    {
        RadFixedDocument document = provider.Import(stream);
        this.pdfViewer.Source = document;
    }
}

or

private void ImportFixedDocument()
{
    Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider provider = new Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider();
    Assembly assembly = typeof(KeyFeatures).Assembly;
    string fileName = assembly.GetManifestResourceNames().FirstOrDefault(n => n.Contains("pdfviewer-overview.pdf"));
    using (Stream stream = assembly.GetManifestResourceStream(fileName))
    {
        RadFixedDocument document = provider.Import(stream);
        this.pdfViewer.Source = new FixedDocumentSource(document);
    }
}
  • Uri
Uri uri = this.GetUri();
this.pdfViewer.Source = uri;

or

Uri uri = this.GetUri();
this.pdfViewer.Source = new UriDocumentSource(uri);

where GetUri() method returns a valid and accessible URL.

  • File - you can visualize the pdf document from a file located on a device (available since R1 2019 SP). You just need to pass the file path to the Source property of the PdfViewer control:
this.pdfViewer.Source = filePath;

where the filePath variable is a string that contains the path to the file location.

In order to make sure that the file exists on the device you could use the following code:

System.IO.File.OpenRead(filePath);

Please make sure that you have granted the app all the permissions needed before the resources are used. Otherwise, an error will be raised.

  • Byte Array
byte[] bytes = this.GetBytes();
this.pdfViewer.Source = bytes;

or

byte[] bytes = this.GetBytes();
this.pdfViewer.Source = new ByteArrayDocumentSource(bytes, true);          
  • Stream

There are two ways:

Func<CancellationToken, Task<Stream>> streamFunc = ct => Task.Run(() =>
{
    Assembly assembly = typeof(KeyFeatures).Assembly;
    string fileName = assembly.GetManifestResourceNames().FirstOrDefault(n => n.Contains("pdfviewer-overview.pdf"));
    Stream stream = assembly.GetManifestResourceStream(fileName);
    return stream;
});
this.pdfViewer.Source = streamFunc;

or

Assembly assembly = typeof(KeyFeatures).Assembly;
string fileName = assembly.GetManifestResourceNames().FirstOrDefault(n => n.Contains("pdfviewer-overview.pdf"));
Stream stream = assembly.GetManifestResourceStream(fileName);
var streamDocumentSource = new StreamDocumentSource();
streamDocumentSource.Import(stream);
this.pdfViewer.Source = streamDocumentSource;

If you choose the second approach with StreamDocumentSource, please keep in mind the stream must stay open while the PdfViewer is in use, because the pdf import is ReadOnDemand. This means that you'd need to manually close the stream when no longer using the PdfViewer.

ReadOnDemand Loading

RadPdfViewer control provides ReadOnDemand loading of the Pdf document, which means that each page of the document is loaded dynamically when necessary (when needed to be shown in the PdfViewer) and it is unloaded once it becomes invisible. The stream that holds the document stays opened while the document is used in PdfViewer.

Document Reference

Through the Document property of RadPdfViewer you can get a reference to the RadFixedDocument imported by the DocumentSource. For more details check RadFixedDocument topic from RadPdfProcessing documentation.

Zoom Level Support

RadPdfViewer exposes properties for applying min and max zoom values.

  • MaxZoomLevel(double): Defines the maximum magnification factor at which content could be maximized. The default value is 3.0
  • MinZoomLevel(double): Defines the minimum magnification factor at which content could be minimized. The default value is 0.3

In order to check how these properties works you should set the ZoomIn and ZoomOut Commmands of the control. For more details please check the Commands article.

Viewing Modes

You could easily set one of the two layout modes that the control provides through its LayoutMode property.

The available options are:

  • ContinuousScroll: Displays pages in a continuous vertical column.
  • SinglePage: Displays one page at a time.

By default the PdfViewer LayoutMode property is set to ContinuousScroll.

The RadPdfViewer LayoutMode could be triggered through the ToggleLayoutModeCommand and the ToggleLayoutModeToolbarItem.

Here is how the PdfViewer looks when LayoutMode is set to ContinuousScroll:

PdfViewer ContinuousScroll

And when the LayoutMode property is set to SinglePage:

PdfViewer SinglePage

Page Spacing

  • PageSpacing(double): Defines the space between the pages of the Pdf Document. The default value is 20.0

PagesStart Index

  • VisiblePagesStartIndex(int): Defines the index at which the document will be displayed. The default value is 0.

BusyIndicator Template

If the default busy template does not suit your needs, you could easily define a custom template through the following property:

  • BusyIndicatorTemplate(DataTemplate): Specifies the template visualized while the Pdf Document is loading.

Here is an example how the custom BusyIndicatorTemplate could be defined:

<telerikPdfViewer:RadPdfViewer x:Name="pdfViewer">
    <telerikPdfViewer:RadPdfViewer.BusyIndicatorTemplate>
        <DataTemplate>
            <telerikPrimitives:RadBusyIndicator AnimationType="Animation10"
                                                AnimationContentHeightRequest="100"
                                                AnimationContentWidthRequest="100"
                                                IsBusy="True" />
        </DataTemplate>
    </telerikPdfViewer:RadPdfViewer.BusyIndicatorTemplate>
</telerikPdfViewer:RadPdfViewer>

Here is how the BusyIndicator Template looks:

PdfViewer BusyIndicator Template

A sample BusyIndicatorTemplate example can be found in the PdfViewer/Features folder of the SDK Samples Browser application.

Example

Here is an example how the above RadPdfViewer features could be applied:

For the example we will visualize a pdf document from file embedded in the application with a BuildAction:EmbeddedResource.

Then add the following code to load the pdf document from Stream:

Func<CancellationToken, Task<Stream>> streamFunc = ct => Task.Run(() =>
{
    Assembly assembly = typeof(KeyFeatures).Assembly;
    string fileName = assembly.GetManifestResourceNames().FirstOrDefault(n => n.Contains("pdfviewer-overview.pdf"));
    Stream stream = assembly.GetManifestResourceStream(fileName);
    return stream;
});
this.pdfViewer.Source = streamFunc;

Finally, use the following snippet to declare a RadPdfViewer in XAML:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition />
    </Grid.RowDefinitions>
    <telerikPdfViewer:RadPdfViewerToolbar PdfViewer="{Binding Source={x:Reference pdfViewer}}">
        <telerikPdfViewer:ZoomInToolbarItem />
        <telerikPdfViewer:ZoomOutToolbarItem />
        <telerikPdfViewer:ToggleLayoutModeToolbarItem />
    </telerikPdfViewer:RadPdfViewerToolbar>
    <telerikPdfViewer:RadPdfViewer x:Name="pdfViewer"
                                   Grid.Row="1"
                                   PageSpacing="15"
                                   MinZoomLevel="0.2"
                                   MaxZoomLevel="5" />
</Grid>

and add the following namespace:

xmlns:telerikPrimitives="clr-namespace:Telerik.XamarinForms.Primitives;assembly=Telerik.XamarinForms.Primitives"

Where the telerikPdfViewer namespace is the following:

xmlns:telerikPdfViewer="clr-namespace:Telerik.XamarinForms.PdfViewer;assembly=Telerik.XamarinForms.PdfViewer"

A sample Key Features example can be found in the PdfViewer/Features folder of the SDK Samples Browser application.

See Also

In this article
Not finding the help you need? Improve this article