JasperReports Ultimate Guide - Sample Reference - Schema Reference - Configuration Reference - API (Javadoc)

JasperReports - PDF Encrypt Sample (version 4.6.0)


Shows how reports could be encrypted when exported to PDF.

Download All Sample Source Files
Browse Sample Source Files on SVN


Main Features in This Sample

Encrypted PDF


top

Encrypted PDFDocumented by Sanda Zaharia


Description / Goal
How to produce encrypted PDF reports.

Since
0.5.1


PDF Encryption

There are situations when documents are supposed to be accessed by authorized users only.
Using the file encryption represents a method of protecting a document from unauthorized access. There are two categories of document users: the document owner (which provides full document access granted by default) and regular document users (with limited access permissions. User permissions and document restrictions can be set by the document owner only.
To distinguish between these user categories, the document is allowed to store within up to two passwords: an owner password and a user password. The application decides whether to encrypt a document based on the presence of any of these passwords or access restrictions. If they are set, the document will be encrypted, making all its content unreadable to unauthorized users.

User permissions and all other information required to validate the passwords will be stored in the document's encryption dictionary.

When a user attempts to open an encrypted document protected with a password, a dialog will open prompting for the appropriate password. If the password matches, then all user permissions are activated and the document can be open, decrypted, and displayed in a readable form on the screen.
If no password is set, when open the document no password is requested and full access is granted by default.
Opening the document with the owner password enables the full access to the document, including the possibility to change passwords and access permissions.
Opening the document with the user password enables some additional operations to be performed, with respect to the user access permissions stored in the encryption dictionary.

Encrypted PDF in JasperReports

When exporting to the PDF format the engine has to know five additional things about the generated document and uses five dedicated exporter parameters for this:
  • Is the document encrypted? A flag parameter called IS_ENCRYPTED answers to this question. If set to Boolean.TRUE, this instructs the exporter to encrypt the resulting PDF document. By default PDF files are not encrypted.
    One can use the related net.sf.jasperreports.export.pdf.encrypted hint property for the same purpose.
  • Which key to use for the encrypted document? By default the PDF exporter uses a 40-bit key for encryption, but if needed, it can use a 128-bit one. Setting the IS_128_BIT_KEY flag to true will instruct the engine to use a 128-bit key.
    One can use the related net.sf.jasperreports.export.pdf.128.bit.key hint property for the same purpose.
  • The user password. This can be set using the USER_PASSWORD or the related net.sf.jasperreports.export.pdf.user.password hint property.
  • The owner password. This can be set using the OWNER_PASSWORD or the related net.sf.jasperreports.export.pdf.owner.password hint property.
  • User permissions. They can be set in the PERMISSIONS export parameter. This exporter parameter accepts java.lang.Integer values representing the PDF permissions for the generated document. The open permissions for the document can be AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations, AllowFillIn, AllowScreenReaders, AllowAssembly, and AllowDegradedPrinting (these are all in the PdfWriter class of the iText library). Permissions can be combined by applying bitwise OR to them.
Encrypted PDF Example

This sample makes use of the above export parameters in order to generate an encrypted document. Taking a look at the pdf() method in the src/PdfEncryptApp.java file, one can see how to set them all:
  exporter.setParameter(JRPdfExporterParameter.IS_ENCRYPTED, Boolean.TRUE);
  exporter.setParameter(JRPdfExporterParameter.IS_128_BIT_KEY, Boolean.TRUE);
  exporter.setParameter(JRPdfExporterParameter.USER_PASSWORD, "jasper");
  exporter.setParameter(JRPdfExporterParameter.OWNER_PASSWORD, "reports");
  exporter.setParameter(
    JRPdfExporterParameter.PERMISSIONS, 
    new Integer(PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING)
    );
Running the Sample

Running the sample requires the Apache Ant library. Make sure that ant is already installed on your system (version 1.5 or later).
In a command prompt/terminal window set the current folder to demo/samples/pdfencrypt within the JasperReports source project and run the > ant test view command.
It will generate all supported document types containing the sample report in the demo/samples/pdfencrypt/build/reports directory.
Then the report will open in the JasperReports internal viewer.



© 2001- Jaspersoft Corporation www.jaspersoft.com