Contents
Introduction
jPDFSecure
is a Java library that integrates seamlessly into your application or applet to update security settings of your PDF documents. jPDFSecure provides the following functions:
- Encrypt / Decrypt PDF documents
- Set / Remove Permissions
- Permission to Print
At High Resolution
- Permission to Print
- Permission to Copy or Extract Content
- Permission to Extract Content in Support of Accessibiliy for Disabled Users
- Permission to Modify
- Permission to Assemble: Insert, Rotate, Delete Pages
- Permission to Add / Modify Annotations
- Permissions to Fill Form Fields and Sign
- Set / Remove Passwords
- Password to Open PDF Documents
- Master Password to Change Permissions
Like all of our libraries, jPDFSecure is built on top of Qoppa's proprietary format and doesn't require any third party programs or drivers.
Getting Started
The starting point for using jPDFSecure is the com.qoppa.pdfSecure.PDFSecure. This class is used to load a pdf document and to update security settings of the pdf document. The class provides three constructors to load PDF files from the file system, a URL or an InputStream. All constructors take an additional parameter, an object that implements IPasswordHandler, that will be queried if the PDF file requires a password to open (called Open or User password). For PDF files that are not currently encrypted, this second parameter can be null:
PDFSecure pdfSecure = new PDFSecure(new URL(http://www.qoppa.com/content.pdf"), null);
Getting Current Password / Encryption Info
Once a
PDFSecure object has been created, the host application
can call simple method to get current security settings of the loaded PDF document.
// PDF document is encrypted
System.out.println(pdfSecure.isEncrypted());
// PDF document has an Open (or User) password (password to open the document)
System.out.println(pdfSecure.hasOpenPassword());
// PDF document has a Permissions (or Owner) password (password to update permissions)
System.out.println(pdfSecure.hasPermissionsPassword());
Getting Current Permissions Info
PDFSecure.getPermissions will return a permission object. From this object, it is easy to get all the distinct permissions.
PDFPermissions perms = pdfSecure.getPermissions();
// permission to print high resolution
System.out.println(
perms.isPrintHighResAllowed());
// permission to print
// automatically on if the permission to print high res is on
System.out.println(
perms.isPrintAllowed());
// permission to copy text and graphics
System.out.println(perms.isExtractTextGraphicsAllowed());
// permission to copy text and graphics for disabled users
// automatically on of the permissions to copy text and graphics is on
System.out.println(perms.isExtractTextGraphicsForAccessibilityAllowed());
// permission to change the document
System.out.println(perms.isChangeDocumentAllowed());
// permission to assemble document: insert, rotate, delete pages
// automatically on if the permission to change document is on
System.out.println(perms.isAssembleDocumentAllowed());
// permission to add/modify annotations
// automatically on if the permission to change document is on
System.out.println(perms.isModifyAnnotsAllowed());
// permission to fill form fields and sign
// automatically on if the permission to change document is on
// also automatically on if the permission to modify annotations is on
System.out.println(perms.isFillFormFieldsAllowed());
Changing Security Settings (Passwords, Encryption & Permissions)
PDFSecure.setSecurity is the method to call to set / update the security settings for this document.
// set new security settings
pdfSecure.setSecurity (newPermPwd, newOpenPwd, permissions, currentPermPwd);
// save pdf document (outputPDFFile is a Java File Object)
pdfSecure.saveDocument(outputPDFFile.getAbsoluteFileName());
- newPermPwd = document's new Permissions password. If null, there is no Permissions (or Owner) password and any user can change the permissions on the document.
- newOpenPwd = document's new Open password. If null, user won't have to enter a password to open the document.
- permissions = a PDFPermissions object. This object can not be null.
To grant all permissions, use new PDFPermissions(true)
To remove all permissions, use new PDFPermissions(false)
To grant only some permissions
PDFPermissions permissons = new PDFPermissions(false);
permissons.setPrintAllowed(true);
permissons.setAssembleDocumentAllowed(true);
permissons.setFillFormsAllowed(true);
Note that there is some hierarchy in permissions:
- If the permissions to Print High Res is on then the permission to print is automatically on.
- If the permissions to Extract Text and Graphics is on then the permission to Extract Text and Graphics for Accessibility is automaticaly on.
- If the permission to Change Document is on then the following 3 permissions are on: Assemble Document, Modify Annotations, Fill Form Fields.
- If the permission to Modify Annotations is on then the permissions to Fill Form Fields is automatically on.
- currentPermPwd = current permissions password. This parameter is needed if the document has a permissions password.
Clearing Security (Passwords, Encryption & Permissions)
Calling PDFSecure.clearSecurity will clear all security for this document: it will clear open/user password, owner/permissions password, encryption. This means that all permissions will be reset to true. If the PDF document has a permissions password, it will have to be passed on as a parameter (currentPermissionsPwd below), else it can be left null:
// clear all security settings
pdfSecure.clearSecurity(currentPermissionsPwd);
// save pdf document (outputPDFFile is a Java File Object)
pdfSecure.saveDocument(outputPDFFile.getAbsoluteFileName());
Getting Basic Information about the PDF Document (Title, Author, etc...)
To get basic information about the loaded PDF document, you need to get the DocumentInfo class accessible through PDFSecure.getDocumentInfo. From this class, you can get information about the document such as title, author, subject, keywords, etc...
System.out.println(pdfSecure.getDocumentInfo().getTitle());
System.out.println(pdfSecure.getDocumentInfo().getAuthor());
System.out.println(pdfSecure.getDocumentInfo().getKeywords());
Distribution and JAR Files
jPDFSecure is packaged in a single jar file, jPDFSecure.jar that gets installed with the evaluation sample.
When distributing an application that contains jPDFSecure, the
jPDFSecure.jar file needs to be distributed along with it and needs
to be included in the class path when running the application.