Skip to Content
NexPDF

Getting started

Activate a license and show a PDF with page tracking and zoom in a .NET MAUI app with PdfView.

English

This example shows a PDF bundled with the app, tracks the current page in the title and jumps to a page. It runs without a license key too: viewing is always allowed, and rendered pages then carry a watermark.

Activate the license at startup

The license platform comes from the device, and the app id is your bundle id or package name.

MauiProgram.cs
using Nexdevit.NexPdf;
using Nexdevit.NexPdf.Maui;

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        using (var stream = FileSystem.OpenAppPackageFileAsync("nexpdf_license.xml").GetAwaiter().GetResult())
        using (var reader = new StreamReader(stream))
        {
            try { NexPdfSdk.ActivateLicense(reader.ReadToEnd(), NexPdfEngine.Platform, AppInfo.PackageName); }
            catch (NexPdfException e) { System.Diagnostics.Debug.WriteLine($"NexPDF license: {e.Message}"); }
        }

        var builder = MauiApp.CreateBuilder();
        builder.UseMauiApp<App>();
        return builder.Build();
    }
}

Put nexpdf_license.xml in Resources/Raw. NexPdfSdk.ActivateLicense works in every version; see License for the 1.0.0 issue with NexPdfEngine.Activate.

Show the document

MainPage.xaml.cs
using Nexdevit.NexPdf.Maui;

public partial class MainPage : ContentPage
{
    private readonly PdfView _view = new();

    public MainPage()
    {
        _view.PageChanged += (_, page) => Title = $"Page {page + 1} of {_view.PageCount}";
        _view.LoadFailed += (_, e) => System.Diagnostics.Debug.WriteLine($"Cannot open: {e.Message}");
        Content = _view;
    }

    protected override async void OnAppearing()
    {
        base.OnAppearing();
        await using var stream = await FileSystem.OpenAppPackageFileAsync("sample.pdf");
        using var ms = new MemoryStream();
        await stream.CopyToAsync(ms);
        await _view.LoadAsync(ms.ToArray());
    }
}
await _view.GoToPageAsync(4);     // 0-based: the fifth page
_view.SetZoom(2);                 // twice the fit-width size

Users can also pinch, and double-tap to switch between fit width and 2.5x.

Run it

dotnet build -t:Run -f net10.0-android

Binding from XAML

With xmlns:nex="clr-namespace:Nexdevit.NexPdf.Maui;assembly=NexPdf.Maui", bind the bytes from your view model; setting DocumentBytes opens the document:

<nex:PdfView DocumentBytes="{Binding Pdf}" PageChanged="OnPageChanged" />

Next steps

On this page