JagPDF
Prev Up Home Next

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:

  • no_print - Disables printing the document.
  • no_modify - Disables modification by operations other than specified by no_modify_ex, no_forms and no_assemble.
  • no_copy
    • (PDF 1.3 and lesser) - Disables copying or extraction of text and graphics (including for accessibility purposes).
    • (PDF 1.4) - Disables copying or extraction of text and graphics by operations other than no_extract_accessibility.
  • no_modify_ex - Disables modification of text annotations, filling in interactive form fields. If no_modify is specified then it also disables creation or modification of interactive form fields (including signature fields).
  • no_forms - (PDF 1.4) Disables filling-in of existing interactive form fields (including signature fields).
  • no_extract_accessibility - (PDF 1.4) Disables copying or extraction for accessibility purposes.
  • no_assemble - (PDF 1.4) Disables insertion, rotation or deletion of pages and creation of bookmarks and thumbnail images.
  • no_hires_print - (PDF 1.4) The document can be printed only in 150 DPI.

Examples

pdficon_small encrypted, pdficon_small passwords, pdficon_small user permissions

PDF Reference

the PDF Reference, chapter Syntax | Encryption

Prev Up Home Next