Skip to Content
NexPDF

Opening and saving

Open PDFs from the app package, the file picker and bytes, open encrypted files, create PDFs from pictures, HTML, Office and e-book files, and save, share and copy documents in a .NET MAUI app.

English

License module

Opening PDFs and pictures, saving and copying are part of the viewer module and work without a key. Creating a PDF from HTML, Office or e-book files needs the conversion module (Startup tier and above).

Open in the view

PdfView.LoadAsync(bytes) opens a document on a background thread and shows it. Read the bytes from wherever the file comes from:

// A file bundled in Resources/Raw
await using (var s = await FileSystem.OpenAppPackageFileAsync("sample.pdf"))
using (var ms = new MemoryStream())
{
    await s.CopyToAsync(ms);
    await view.LoadAsync(ms.ToArray());
}

// A file the user picks
var file = await FilePicker.Default.PickAsync();
if (file is not null)
{
    await using var s = await file.OpenReadAsync();
    using var ms = new MemoryStream();
    await s.CopyToAsync(ms);
    await view.LoadAsync(ms.ToArray());
}

The view accepts JPEG, PNG and TIFF pictures too (one page per picture). With the conversion module, Word, Excel and PowerPoint files, EPUB and zipped FictionBook books, XPS files and CBZ comics are detected and converted on open. When a file cannot be opened, the view raises LoadFailed; see PdfView.

Open without a view

NexPdfDocument.OpenBytes(data, password = null) opens bytes; NexPdfDocument.Open(path, password = null) reads a file path. Use them for work that does not need to be shown, on a background thread:

using Nexdevit.NexPdf;

var pages = await Task.Run(() =>
{
    using var doc = NexPdfDocument.OpenBytes(data);
    return doc.PageCount;
});

A document holds native memory. Dispose it with using; after that any call throws ObjectDisposedException. The view's own document is disposed when another one is loaded or the view is disposed.

Encrypted documents

Since 1.1, PdfView opens encrypted PDFs (RC4, AES-128 and AES-256) with the user or owner password: pass it to LoadAsync(bytes, password) or set the bindable Password. A missing or wrong password raises PasswordRequired (and LoadFailed):

view.PasswordRequired += async (_, e) =>
{
    var password = await DisplayPromptAsync("Password",
        e.WrongPassword ? "Wrong password. Try again:" : "This PDF is protected. Password:");
    if (password is not null) await view.LoadAsync(data, password);
};
await view.LoadAsync(data);

Without a view, NexPdfDocument.OpenBytes(data, password) throws NexPdfException with the message password required or incorrect password. Office files, e-books and comics carry no PDF password: since 1.1 they are converted as usual and a password given with them is ignored. See PdfView and Security.

Create a PDF from other formats

SourceMethodModule
JPEG, PNG, TIFFNexPdfDocument.OpenImage(data)none
HTMLNexPdfDocument.FromHtml(html)conversion
Word, Excel, PowerPointNexPdfDocument.OpenOffice(data)conversion
EPUB, FictionBook, XPS/OpenXPS, CBZNexPdfDocument.OpenEbook(data)conversion
using (var pdf = NexPdfDocument.FromHtml("<h1>Invoice 42</h1><p>Total: 120 EUR</p>"))
    await view.LoadAsync(pdf.SaveBytes());

To add photos to the open document, use InsertImagePages(index, data); see Editing.

Save and share

Save(path) writes the document with every change as a complete new file; SaveBytes() returns the same bytes. Write to the app's own folders:

var path = Path.Combine(FileSystem.AppDataDirectory, "contract.pdf");
view.Document!.Save(path);

var shared = Path.Combine(FileSystem.CacheDirectory, "contract.pdf");
await File.WriteAllBytesAsync(shared, view.Document.SaveBytes());
await Share.Default.RequestAsync(new ShareFileRequest("Contract", new ShareFile(shared)));

Save(path, linearized: true) and SaveLinearized() write a "Fast Web View" file, which shows its first page before a download ends; useful when you upload the PDF to a web server.

Signed files

Signing returns the finished signed file. Write those bytes as they are: saving the document object again rewrites the file and breaks the signature. See Signatures.

Copy a document

Copy() returns an independent document with the same content and changes, for an undo snapshot or for work on another thread. Take the copy when the view is idle, since it reads the whole document:

var snapshot = view.Document!.Copy();
view.Document.DeletePage(0);
await view.ReloadAsync();
// ... undo:
await view.LoadAsync(snapshot.SaveBytes());
snapshot.Dispose();

On this page