Docs / Guides
Encryption and permissions
SoftZenPdf implements the PDF Standard Security Handler: password-based encryption with permissions, for writing and for reading.
Encrypting a document
Call Encrypt before saving. The user password opens the document with the permissions you
grant; the owner password opens it with full rights. If you leave the owner password out, a random one
is generated, so the user password can never grant owner rights.
using SoftZenPdf;
var document = new PdfDocument();
document.AddPage().Content.DrawText("Confidential", 72, 770, StandardFont.HelveticaBold, 24);
document.Encrypt(userPassword: "user123", ownerPassword: "owner456",
permissions: PdfPermissions.All & ~PdfPermissions.CopyContent);
document.Save("secret.pdf");
var reader = PdfReader.Open("secret.pdf", "user123");
Console.WriteLine(reader.EncryptionAlgorithm); // Aes256
Algorithms
| PdfEncryptionAlgorithm | Revision | Use it when |
|---|---|---|
Aes256 (default) | 6 (ISO 32000-2) | Always, unless you must support very old viewers |
Aes128 | 4 | Viewers that predate AES-256 support |
Rc4_128 | 3 | Only for old software; RC4 is cryptographically broken |
Rc4_40 | 2 | Only for very old software; offers almost no protection |
With Aes256, passwords can contain any Unicode text; they are normalised with SASLprep, and only the first
127 bytes of their UTF-8 form are used, as the standard specifies. The older algorithms only accept printable ASCII passwords and throw ArgumentException otherwise.
Permissions
| PdfPermissions | Allows |
|---|---|
Print | Printing, possibly at low quality |
PrintHighQuality | Printing at full quality |
ModifyContents | Changing the content, other than the operations below |
CopyContent | Copying or extracting text and graphics |
ModifyAnnotations | Adding or changing annotations, and filling forms |
FillForms | Filling existing form fields |
AssembleDocument | Inserting, rotating or deleting pages and creating bookmarks |
Combine flags with |, or start from PdfPermissions.All and remove flags with & ~.
Leave the user password empty to create a file anyone can open without a prompt, but with restricted permissions:
using SoftZenPdf;
var document = new PdfDocument();
document.AddPage().Content.DrawText("Anyone can open this; only printing is allowed.", 72, 770, StandardFont.Helvetica, 14);
// No user password: the file opens without a prompt, but the permissions still apply.
document.Encrypt(userPassword: null, ownerPassword: "owner456",
permissions: PdfPermissions.Print | PdfPermissions.PrintHighQuality);
document.Save("print-only.pdf");
Opening encrypted files
Pass either password to PdfReader.Open; a wrong password throws PdfPasswordException.
HasOwnerAccess tells you which password was used and Permissions what the file grants. With
owner rights you can copy the pages into a new, unencrypted document, which removes the password:
using SoftZenPdf;
try
{
PdfReader.Open("secret.pdf", "wrong password");
}
catch (PdfPasswordException)
{
Console.WriteLine("Wrong password");
}
var reader = PdfReader.Open("secret.pdf", "owner456");
Console.WriteLine($"Owner access: {reader.HasOwnerAccess}, permissions: {reader.Permissions}");
// With owner rights, copying the pages into a new document removes the password.
PdfDocument.Merge(reader).Save("unprotected.pdf");
SoftZenPdf's decryption is tested against files encrypted by qpdf, an independent implementation, for every algorithm.
Security notes
Permissions are advisory. Conforming viewers enforce them, but anyone who has the user password can decrypt the content with other tools. Use them to express intent, not to protect secrets from people who can open the file.
Only password security is supported: there is no certificate (public-key) encryption, and files using Adobe's deprecated AES-256 revision 5 cannot be opened. PDF/A documents cannot be encrypted.
API: PdfDocument.Encrypt, PdfPermissions, PdfReader.HasOwnerAccess.