How to use Windows Universal PDFViewCtrl in Xamarin.Forms?

Note: This tutorial is based on Windows Universal platform (Windows 10 apps), but the same should apply for Windows 8.1.

Step 1: Setup your Xamarin.Forms project to include a Universal Windows Platform (UWP) app by following the official documentation:
https://developer.xamarin.com/guides/xamarin-forms/platform-features/windows/installation/universal/

You can find the shared project under: Add Reference → Shared Projects

Step 2: Download PDFNet SDK for Windows Universal Apps, Windows 10 version. Install the .vsix library found in Lib folder.

Step 3: Add PDFNet for Windows 10 Universal Apps extension to the UWP project via Add Reference. (Add Reference → Universal Windows → Extensions)

Step 4: In the common shared project, create a Page as follows:

public class ViewerPage : ContentPage { public ViewerPage() { } }

Step 5: Add a sample PDF file in the UWP project. We will refer to the file as “sample.pdf”. Right click the file and select Properties. Set the Build Action to “Content”.

Step 6: In the UWP project, create a PageRenderer as follows:

`
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms.Platform.UWP;
using Xamarin.Forms;
using Windows.Foundation;

[assembly: ExportRenderer(typeof(PDFViewCtrlDemo3.ViewerPage), typeof(PDFViewCtrlDemo3.UWP.ViewerPageRenderer))]

namespace PDFViewCtrlDemo3.UWP
{
public class ViewerPageRenderer : PageRenderer
{
Windows.UI.Xaml.Controls.Border _border;
public ViewerPageRenderer()
{
pdftron.PDFNet.Initialize();
_border = new Windows.UI.Xaml.Controls.Border();
var container = this.ContainerElement as Windows.UI.Xaml.Controls.Panel;
container.Children.Add(_border);
_border.Background = new Windows.UI.Xaml.Media.SolidColorBrush(Windows.UI.Colors.Orange);

pdftron.PDF.PDFViewCtrl pdfviewctrl = new pdftron.PDF.PDFViewCtrl();
_border.Child = pdfviewctrl;
string path = System.IO.Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, “sample.pdf”);
pdftron.PDF.PDFDoc doc = new pdftron.PDF.PDFDoc(path);
pdfviewctrl.SetDoc(doc);
}

// REQUIRED: Xamarin will not measure and arrange children by default
protected override Windows.Foundation.Size ArrangeOverride(Windows.Foundation.Size finalSize)
{
_border.Arrange(new Rect(0, 0, finalSize.Width, finalSize.Height));
return finalSize;
}

// REQUIRED: Xamarin will not measure and arrange children by default
protected override Windows.Foundation.Size MeasureOverride(Windows.Foundation.Size availableSize)
{
_border.Measure(availableSize);
return new Windows.Foundation.Size(_border.DesiredSize.Width, _border.DesiredSize.Height);
}
}
}
`

Step 7: In the common shared project’s App.cs file, make sure MainPage is set to ViewerPage:

public App () { // The root page of your application MainPage = new ViewerPage(); }

Step 8: Build the project and run, you should see something like the following: