3.5. Encryption
A PDF document can be encrypted to prevent unauthorized access. JagPDF implements PDF's standard security handler which allows to specify
- Access permissions. It is possible to selectively disable certain operations like printing or extracting text from a document.
- User password. When a document has a user password, it must be supplied to be able to view the document and to perform operations allowed by the access permissions.
- Owner password. Supplying an owner password grants unlimited access to the document including changing the passwords and access permissions.
JagPDF uses 40-bit encryption keys for PDF 1.3 and lesser and 128-bit encryption keys since PDF 1.4. Passwords and access permissions can be set via Profile.
In the following example we will show, how to encrypt a document. First we will create a profile and specify that we want to use the standard security handler:
profile = jagpdf.create_profile() profile.set("doc.encryption", "standard")
Now we will use that profile when a new document is created
doc = jagpdf.create_file("encrypted.pdf", profile) doc.page_start(597.6, 848.68) doc.page().canvas().text(50, 800, "Encrypted Document.") doc.page_end() doc.finalize()
A document encrypted in this way has neither user nor owner password and there are no restrictions on user operations.
To create a document with passwords we need to specify them in a profile:
profile = jagpdf.create_profile() profile.set("doc.encryption", "standard") profile.set("stdsh.pwd_owner", "owner-pwd") profile.set("stdsh.pwd_user", "user-pwd")
And initialize the document with that profile:
doc = jagpdf.create_file("encrypted_pwd.pdf", profile) doc.page_start(597.6, 848.68) doc.page().canvas().text(50, 800, "Document with user and owner passwords.") doc.page_end() doc.finalize()
In the following example we will illustrate how to set user permissions.
profile = jagpdf.create_profile() profile.set("doc.encryption", "standard") profile.set("stdsh.permissions", "no_print; no_copy")
We specified that the user is not allowed to print or extract text from the document. And again, we need to supply the profile to the document:
doc = jagpdf.create_file("encrypted_perm.pdf", profile) doc.page_start(597.6, 848.68) doc.page().canvas().text(50, 800, "No printing or text copying allowed") doc.page_end() doc.finalize()
The following table lists Profile options related to encryption. All options default to an empty string.
Name |
Default |
Description |
---|---|---|
stdsh.pwd_user |
no password |
User password. At most 32 characters. |
stdsh.pwd_owner |
no password |
Owner password. At most 32 characters. |
stdsh.permissions |
no restrictions |
Specifies access permissions. The following options are available:
|
Examples |
|
PDF Reference |
the PDF Reference, chapter Syntax | Encryption |