public class Encryption extends NativeObject
PDF document can be encrypted to protect content from unauthorized access. The encryption process applies encryption to all streams (e.g. images) and strings, but not to other items in the PDF document. This means the structure of the PDF document is accessible, but the content of its pages is encrypted.
The standard security handler allows access permissions and up to two passwords to be specified for a document:
A user password (see getUserPassword()
) and an owner password (see getOwnerPassword()
).
The following list shows the four possible combinations of passwords and how an application processing such a PDF document behaves:
No user password, no owner password (no encryption):
Everyone can read, i.e. no password required to open the document. Everyone can change security settings.
No user password, owner password:
Everyone can read, i.e. no password required to open the document. Access permissions are restricted (unless the owner password is provided). Owner password required to change security settings.
User password, no owner password:
User password required to read. All access permissions are granted.
User password, owner password:
User or owner password required to read. Access permissions are restricted (unless the owner password is provided). Owner password required to change security settings.
Since encryption is not allowed by the PDF/A ISO standards, PDF/A documents must not be encrypted.
Constructor | Description |
---|---|
Encryption(java.lang.String userPassword,
java.lang.String ownerPassword,
java.util.EnumSet<Permission> permissions) |
Modifier and Type | Method | Description |
---|---|---|
java.lang.String |
getOwnerPassword() |
The owner password (Getter)
|
java.util.EnumSet<Permission> |
getPermissions() |
The access permissions granted when opening the document (Getter)
|
java.lang.String |
getUserPassword() |
The user password (Getter)
|
void |
setPermissions(java.lang.String ownerPassword,
java.util.EnumSet<Permission> permissions) |
Set the owner password and access permissions.
|
void |
setUserPassword(java.lang.String value) |
The user password (Setter)
|
equals, hashCode
public Encryption(java.lang.String userPassword, java.lang.String ownerPassword, java.util.EnumSet<Permission> permissions)
userPassword
- Set the user password of the output document (see getUserPassword()
).
If null
or empty, no user password is set.ownerPassword
- Set the owner password and permissions of the output document (see getOwnerPassword()
).
If null
or empty, no owner password is set.permissions
- The permissions to be set on the PDF document.
If no owner password is set, the permissions must not be restricted, i.e. the permissions
must be All
.java.lang.IllegalArgumentException
- if permissions
is null
public void setPermissions(java.lang.String ownerPassword, java.util.EnumSet<Permission> permissions)
getOwnerPassword()
).ownerPassword
- The owner password to be set on the PDF document (see getOwnerPassword()
).permissions
- The permissions to be set on the PDF document.java.lang.IllegalArgumentException
- If restricted permissions
(i.e. not All
) are specified without ownerPassword
.java.lang.IllegalArgumentException
- if ownerPassword
is null
java.lang.IllegalArgumentException
- if permissions
is null
public java.lang.String getUserPassword()
This password protects the document against unauthorized opening and reading.
If a PDF document is protected by a user password, it cannot be opened without a password. The user or, if set, owner password must be provided to open and read the document.
If a document is not protected by a user password, it can be opened by without a password, even if an owner password is set.
If the password contains characters that are not in the Windows ANSI encoding (Windows Code Page 1252), the output document's compliance level is automatically upgraded to PDF version 1.7. This is because older PDF versions do not support Unicode passwords.
public void setUserPassword(java.lang.String value)
This password protects the document against unauthorized opening and reading.
If a PDF document is protected by a user password, it cannot be opened without a password. The user or, if set, owner password must be provided to open and read the document.
If a document is not protected by a user password, it can be opened by without a password, even if an owner password is set.
If the password contains characters that are not in the Windows ANSI encoding (Windows Code Page 1252), the output document's compliance level is automatically upgraded to PDF version 1.7. This is because older PDF versions do not support Unicode passwords.
public java.lang.String getOwnerPassword()
This password is sometimes also referred to as the author’s password. This password grants full access to the document. Not only can the document be opened and read, it also allows the document's security settings, access permissions, and passwords to be changed.
If the password contains characters that are not in the Windows ANSI encoding (Windows Code Page 1252), the output document's compliance level is automatically upgraded to PDF version 1.7. This is because older PDF versions do not support Unicode passwords.
public java.util.EnumSet<Permission> getPermissions()
The operations granted in a PDF document are controlled using permission flags. In order to set permission flags, the PDF document must be encrypted and have an owner password.
The restricted permissions apply whenever the document is opened with a password other than the owner password. The owner password is required to initially set or later change the permission flags.
When opening an encrypted document, the access permissions for the document can be read using Document.getPermissions()
.
Note that the permissions might be different from the "Document Restrictions Summary" displayed in Adobe Acrobat.