PdfView
The .NET MAUI PDF view - loading documents, XAML binding, navigation, zoom and gestures, events, how it renders, and editing the open document.
PdfView (namespace Nexdevit.NexPdf.Maui) is a ContentView that shows a PDF: the pages stacked
vertically in a scroll view, rendered in the background by the NexPDF engine, with pinch-to-zoom
and double-tap zoom. It runs on Android, iOS, Mac Catalyst and Windows.
License module
PdfView uses only the viewer module and works without a key. Without a commercial key, rendered
pages carry the "NexPDF • Unlicensed" or "NexPDF • Trial" watermark; see
Running without a key.
Show a document
Create the view, then pass the bytes of a document to LoadAsync:
using Nexdevit.NexPdf.Maui;
var view = new PdfView();
view.PageChanged += (_, page) => Title = $"Page {page + 1} of {view.PageCount}";
view.LoadFailed += (_, e) => System.Diagnostics.Debug.WriteLine($"Cannot open: {e.Message}");
Content = view;
await view.LoadAsync(await File.ReadAllBytesAsync(path));LoadAsync(byte[]) opens the bytes on a background thread with NexPdfDocument.OpenBytes, so it
shows what OpenBytes opens: PDF, JPEG, PNG and TIFF pictures and, with the conversion module,
Word, Excel, PowerPoint, EPUB, FictionBook, XPS and CBZ files. A previous document is disposed
first. When opening fails, LoadAsync does not throw: it raises LoadFailed with the exception.
Encrypted PDFs
Since 1.1 the view opens encrypted PDFs (RC4, AES-128 and AES-256) with the user or owner password:
| Member | What it does |
|---|---|
LoadAsync(bytes, password) | Opens the bytes with password (null for none) |
Password | Bindable property. LoadAsync(bytes) and DocumentBytes use it. Set it after a PasswordRequired to retry the open |
PasswordRequired | Raised when the document is encrypted and the password is missing (e.WrongPassword is false) or wrong (true). LoadFailed is raised too |
PdfView.IsPasswordError(exception) | true when an open failed because the password was missing or wrong |
view.PasswordRequired += async (_, e) =>
{
var prompt = e.WrongPassword ? "Wrong password. Try again:" : "This PDF is protected. Password:";
var password = await DisplayPromptAsync("Password", prompt);
if (password is not null) view.Password = password; // retries the open
};
view.LoadFailed += (_, e) =>
{
if (!PdfView.IsPasswordError(e)) System.Diagnostics.Debug.WriteLine($"Cannot open: {e.Message}");
};
view.DocumentBytes = bytes; // or await view.LoadAsync(bytes, password)Setting Password to a new value retries the open only when no document is open and the bytes came
from DocumentBytes. After LoadAsync(bytes), call LoadAsync(bytes, password) yourself with the
password the user typed. Office files, e-books and comics carry no PDF password: they are converted
and the password is ignored.
Bind from XAML
DocumentBytes is a bindable property; setting it to a byte[] calls LoadAsync with
Password, so bind or set Password first for an encrypted file:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:nex="clr-namespace:Nexdevit.NexPdf.Maui;assembly=NexPdf.Maui"
x:Class="MyApp.MainPage">
<nex:PdfView x:Name="Viewer" DocumentBytes="{Binding Pdf}" PageChanged="OnPageChanged" />
</ContentPage>private void OnPageChanged(object? sender, int page) =>
Title = $"Page {page + 1} of {Viewer.PageCount}";DocumentBytes keeps the value you set. A document opened with LoadAsync does not change it.
Navigation
| Member | What it does |
|---|---|
PageCount | Number of pages of the open document (0 before one is loaded) |
CurrentPage | 0-based index of the page near the top of the view |
GoToPageAsync(page, animated = true) | Scrolls so page starts at the top; out-of-range pages are ignored |
PageChanged | Raised with the new CurrentPage when it changes: after a document loads and as the user scrolls |
await view.GoToPageAsync(view.PageCount - 1); // the last page
await view.GoToPageAsync(0, animated: false);Zoom and gestures
| Member | What it does |
|---|---|
Zoom | The current zoom; 1 fits the page width to the view |
SetZoom(zoom) | Sets the zoom, clamped to PdfView.MinZoom (0.5) and PdfView.MaxZoom (5.0) |
| Pinch | Scales the pages live and renders them sharp when the pinch ends |
| Double tap | Zooms to 2.5x at the tapped point, or back to 1 when already zoomed in |
view.SetZoom(2); // twice the fit-width size
ZoomLabel.Text = $"{view.Zoom:P0}";How it renders
- Pages are laid out at their displayed size (crop box and rotation applied), scaled to the view width times the zoom.
- After scrolling, pinching or resizing settles, the view renders the current page, the page before it and the two after it, on a background thread, at a resolution that matches the zoom and the screen density (between 36 and 600 dpi).
- Rendered pages near the current one are kept as PNG bytes and reused; pages further than four pages away are dropped from that cache.
- Rendering errors are not reported: a page that fails to render stays blank. Render it yourself
with
view.Document!.RenderPagePng(page, dpi)to see the engine's message.
The open document
Document is the open NexPdfDocument (or null), with the whole engine API: text, search,
annotations, forms, editing, conversion and the rest, as the guides show. After you change it, call
ReloadAsync(): it clears the rendered pages, re-reads the page count and sizes, and renders again.
view.Document!.AddWatermark("DRAFT"); // needs the security module
await view.ReloadAsync();PdfView renders on a background thread with a lock of its own, which your calls on Document do
not take. A call you make while the view is rendering can overlap it, and a document is used by one
thread at a time. For anything more than a short edit, work on your own document and load the
result:
using Nexdevit.NexPdf;
using var doc = NexPdfDocument.OpenBytes(originalBytes);
doc.RotatePage(0, 90); // needs the editor module
await view.LoadAsync(doc.SaveBytes());Dispose
Dispose() cancels rendering and frees the document. MAUI does not call it for you: call it when
the view will not be shown again, for example after you remove the page that holds it from the
navigation stack.
What PdfView does not have yet
PdfView is a viewer with zoom. It has no annotation tools, form filling UI, signing UI, search
bar, thumbnails or text selection (still true in 1.1). Those features are available in code through Document; for a
ready-made editing UI, embed the web viewer in a WebView.
Related
- Getting started
- Rendering: render pages yourself and map taps to PDF points
- Platform notes: UI thread, background work and memory
- API reference