Basic viewer for Xamarin.Forms (view & annotate)

If you are looking for a quick start on displaying documents in your application, please first take a look at the document viewer guide as it contains all out of box controls to provide you with the best viewing and annotating experience. Continue reading this article if you are looking for embedding viewer component alone.

Before you get started, we recommend you have a look at Microsoft's comprehensive guide on Customizing a ContentPage. It is important that you understand how Xamarin renders the same page on different platforms using ContentPage, ExportRenderer and PageRenderer. In this guide, you will learn how to create the PDFViewCtrl page renderer on each platform.

Complete sample code can be found here:

The rendering process can be taken advantage of to implement platform-specific customization by creating a custom renderer for a ContentPage on each platform. The process for doing this is as follows:

  1. Create a Xamarin.Forms page.
  2. Consume the page from Xamarin.Forms.
  3. Create the custom renderer for the page on each platform.

Create the Xamarin.Forms Page

An unaltered ContentPage can be added to the shared Xamarin.Forms project, as shown in the following XAML code example:

XAML

1<?xml version="1.0" encoding="utf-8" ?>
2<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="CustomRenderer.ViewerPage" Title="Viewer Page">
3 <ContentPage.Content>
4 </ContentPage.Content>
5</ContentPage>

Similarly, the code-behind file for the ContentPage should also remain unaltered, as shown in the following code example:

C#

1public partial class ViewerPage : ContentPage
2{
3 public ViewerPage()
4 {
5 InitializeComponent();
6 }
7}

The following code example shows how the page can be created in C#:

C#

1public class ViewerPageCS : ContentPage
2{
3 public ViewerPageCS()
4 {
5 }
6}

An instance of the ViewerPage will be used to display PDF file on each platform. Customization of the control will be carried out in the custom renderer, so no additional implementation is required in the ViewerPage class.

Consume the Xamarin.Forms Page

The empty ViewerPage must be displayed by the Xamarin.Forms application. This occurs when a button on the MainPage instance is tapped, which in turn executes the OnOpenViewerButtonClicked method, as shown in the following code example:

C#

1async void OnOpenViewerButtonClicked(object sender, EventArgs e)
2{
3 await Navigation.PushAsync(new ViewerPage());
4}

This code simply navigates to the ViewerPage, on which custom renderers will customize the page's appearance on each platform.

Create the Page Renderer on each Platform

The ViewerPage instance is rendered by platform-specific ViewerPageRenderer classes, which all derive from the PageRenderer class for that platform. This results in each ViewerPage instance being rendered with a PDF file, as shown in the following screenshots:

Android

iOS

UWP

Apryse Docs Image
Apryse Docs Image
Apryse Docs Image

Create the Page Renderer on iOS

The following code example shows the page renderer for the iOS platform:

C#

1[assembly: ExportRenderer(typeof(ViewerPage), typeof(ViewerPageRenderer))]
2namespace CustomRenderer.iOS
3{
4 public class ViewerPageRenderer : PageRenderer
5 {
6 ...
7
8 protected override void OnElementChanged(VisualElementChangedEventArgs e)
9 {
10 base.OnElementChanged(e);
11
12 if (e.OldElement != null || Element == null)
13 {
14 return;
15 }
16
17 try
18 {
19 SetupUserInterface();
20 SetupEventHandlers();
21 }
22 catch (Exception ex)
23 {
24 System.Diagnostics.Debug.WriteLine(@" ERROR: ", ex.Message);
25 }
26 }
27
28 ...
29 }
30}

Create the Page Renderer on Android

The following code example shows the page renderer for the Android platform:

C#

1[assembly: ExportRenderer(typeof(ViewerPage), typeof(ViewerPageRenderer))]
2namespace CustomRenderer.Droid
3{
4 public class ViewerPageRenderer : PageRenderer
5 {
6 ...
7
8 public ViewerPageRenderer(Context context) : base(context)
9 {
10 }
11
12 protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
13 {
14 base.OnElementChanged(e);
15
16 if (e.OldElement != null || Element == null)
17 {
18 return;
19 }
20
21 try
22 {
23 SetupUserInterface();
24 SetupEventHandlers();
25 AddView(view);
26 }
27 catch (Exception ex)
28 {
29 System.Diagnostics.Debug.WriteLine(@" ERROR: ", ex.Message);
30 }
31 }
32
33 ...
34 }
35}

Create the Page Renderer on UWP

The following code example shows the page renderer for UWP:

C#

1[assembly: ExportRenderer(typeof(ViewerPage), typeof(ViewerPageRenderer))]
2namespace CustomRenderer.UWP
3{
4 public class ViewerPageRenderer : PageRenderer
5 {
6 Page page;
7 ...
8
9 protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Page> e)
10 {
11 base.OnElementChanged(e);
12
13 if (e.OldElement != null || Element == null)
14 {
15 return;
16 }
17
18 try
19 {
20 ...
21 SetupUserInterface();
22
23 this.Children.Add(page);
24 }
25 catch (Exception ex)
26 {
27 Debug.WriteLine(@" ERROR: ", ex.Message);
28 }
29 }
30
31 protected override Size ArrangeOverride(Size finalSize)
32 {
33 page.Arrange(new Windows.Foundation.Rect(0, 0, finalSize.Width, finalSize.Height));
34 return finalSize;
35 }
36
37 ...
38 }
39}

Try out the complete sample here: https://github.com/ApryseSDK/xamarin-forms-sample.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales