Encryption and permissions
Open password-protected PDFs, encrypt with AES-256 and permissions, remove passwords, and stamp watermarks, headers and Bates numbers in the browser with NexPDF for the web.
License module
Encrypting, removing passwords, watermarks, headers, footers and Bates numbering need the security
module (Startup tier and above). Opening encrypted files and reading their security settings work
without a key.
Open an encrypted PDF
try {
const doc = await nex.PdfDocument.fromFile(file, { password: 'secret' });
} catch (e) {
console.log((e as Error).message); // "password required" or "incorrect password"
}The user (open) password or the owner password both work. RC4, AES-128 and AES-256 files are supported.
const info = JSON.parse(doc.raw.securityJson());
console.log(info.encrypted, info.owner, info.print, info.copy);securityJson() reports encrypted, revision, owner (opened with owner access) and the
permissions print, modify, copy, annotate, fill_forms and assemble.
Encrypt with AES-256
doc.encrypt('user-password', 'owner-password');
const encrypted = doc.toBlob();An empty user password lets anyone open the file while the owner password still protects the permissions.
Set permissions
doc.raw.encryptWithPermissions(userPw, ownerPw, permissions) takes a bit mask of what users without
the owner password may do:
| Flag | Value |
|---|---|
| 1 | |
| Modify | 2 |
| Copy text and images | 4 |
| Annotate | 8 |
| Fill forms | 16 |
| Assemble (insert, rotate, delete pages) | 32 |
const PRINT = 1, FILL_FORMS = 16;
doc.raw.encryptWithPermissions('', 'owner-password', PRINT | FILL_FORMS);Remove a password
const doc = await nex.PdfDocument.fromFile(file, { password: 'owner-password' });
doc.raw.removePassword(null); // or pass the owner password here
const decrypted = doc.toBlob();Watermarks, headers, footers and Bates numbers
doc.raw.addWatermark('CONFIDENTIAL');
doc.raw.addHeaderFooter('Acme, Lda', 'Page {page} of {total}', 9); // font size 0 = default
doc.raw.addBates('ACME-', 1, 6); // ACME-000001, ACME-000002, ...
doc.raw.removeWatermarks(); // removes NexPDF watermarks