Skip to Content
NexPDF

Encryption and permissions

Open password-protected PDFs in a .NET MAUI PdfView, encrypt documents and add watermarks with the NexPDF engine.

English

License module

Encrypting, removing passwords, watermarks, headers, footers and Bates numbering need the security module (Startup tier and above). Opening encrypted files works without a key.

Open an encrypted PDF

Since 1.1, PdfView takes the password itself: call LoadAsync(bytes, password) or set the bindable Password property (before DocumentBytes, or after a failed open to retry). When the password is missing or wrong the view raises PasswordRequired, with WrongPassword telling the two apart, and then LoadFailed:

using Nexdevit.NexPdf.Maui;

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) view.Password = password;   // retries the DocumentBytes open
};
view.DocumentBytes = pdfBytes;

PdfView.IsPasswordError(exception) tells a password failure from any other in a LoadFailed handler. See PdfView.

In 1.0.x PdfView took no password. There, open the file yourself with NexPdfDocument.OpenBytes(pdfBytes, password), remove the protection with RemovePassword() (security module) and pass doc.SaveBytes() to LoadAsync.

Encrypt before sharing

var doc = view.Document!;
doc.EncryptAes256("user-password", "owner-password");
doc.Save(Path.Combine(FileSystem.CacheDirectory, "protected.pdf"));

EncryptWithPermissions(userPw, ownerPw, permissions) takes a bit mask: print 1, modify 2, copy 4, annotate 8, fill forms 16, assemble 32.

Watermarks, headers and footers

doc.AddWatermark("CONFIDENTIAL");
doc.AddHeaderFooter("Acme, Lda", "Page {page} of {total}", 9);
await view.ReloadAsync();

See the .NET security guide for permissions and Bates numbering.

On this page