Encryption and permissions
Open password-protected PDFs, encrypt with AES-256 and permissions, remove passwords, and stamp watermarks, headers and Bates numbers with NexPDF for .NET.
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
using System.Text.Json.Nodes;
using Nexdevit.NexPdf;
try
{
using var doc = NexPdfDocument.Open("protected.pdf", "secret");
var info = JsonNode.Parse(doc.SecurityJson())!;
Console.WriteLine($"{info["encrypted"]} {info["owner"]} {info["print"]} {info["copy"]}");
}
catch (NexPdfException e)
{
Console.WriteLine(e.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.
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.EncryptAes256("user-password", "owner-password");
doc.Save("encrypted.pdf");An empty user password lets anyone open the file while the owner password still protects the permissions.
Set permissions
EncryptWithPermissions(userPw, ownerPw, permissions) takes a bit mask (uint) 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 uint Print = 1, FillForms = 16;
doc.EncryptWithPermissions("", "owner-password", Print | FillForms);
doc.Save("print-and-fill-only.pdf");Remove a password
using var doc = NexPdfDocument.Open("encrypted.pdf", "owner-password");
doc.RemovePassword(); // or pass the owner password here
doc.Save("decrypted.pdf");Watermarks, headers, footers and Bates numbers
doc.AddWatermark("CONFIDENTIAL");
doc.AddHeaderFooter("Acme, Lda", "Page {page} of {total}", 9); // font size 0 = default
doc.AddBates("ACME-", 1, 6); // ACME-000001, ACME-000002, ...
doc.RemoveWatermarks(); // removes NexPDF watermarks