How to Encrypt PDF Documents in Java
The goal of this tutorial is to enable you with the capabilities to protect your information and important documents with the appropriate tools.
Join the DZone community and get the full member experience.Join For Free
Proper documentation, intensive contracts, and extensive manuals form the backbone of the business, though, in modern business, much of this is retained digitally using document file formats such as PDF. Because your organization relies on so many of these forms of documentation, it is integral that you can protect the contents within from errors or outside threats. For the proper precautions to be put in place, utilizing encryption and permissions settings will ensure your PDF documents are only used in ways you deem fit, and cannot be accidentally or maliciously altered by other entities.
Setting these parameters on each document, however, is a daunting and time-consuming task, and, if it is mistakenly forgotten, it can lead to major issues for you and your organization. By using the following two APIs, you can cut this risk as each document will be automatically encrypted with password protection. This password protection includes an owner password to control editor/creator permissions and a user password to control who can view the PDF.
Furthermore, our second API shown below will allow you to set additional permissions on the document including the ability to restrict or allow printing, copying content, document assembly, editing (read-only), form filling, modification of annotations, and degraded printing through document Digital Rights Management (DRM).
The goal of this tutorial is to enable you with the capabilities to protect your information and important documents with the appropriate tools. This will help your organization to run more smoothly and provide added security to your operations.
For the first API, we have a few parameters we need to input for the function to work. These parameters include the user password, owner password, encryption key length, and the input PDF file. For encryption key length, the possible values are “128” (128-bit RC4 encryption) and “256” (256-bit AES encryption), with a default of 256.
To run the function, we first need to install our library with Maven by adding a Jitpack reference to the repository in pom.xml:
Then, we can add a reference to the dependency:
To install with Gradle, you can add the reference in your root build.gradle at the end of repositories:
Then, add the dependency in build.gradle:
The imports need to be pasted at the top of the file, and then we can call our function:
This will return a downloadable file containing the predetermined encryption settings. To ensure that this and our other API function properly, you will need to verify that:
- Your input file is valid
- You have correctly set the parameters
- Your API Key has been properly inserted into the code block. This can be retrieved at no cost on the Cloudmersive website and will provide access to 800 monthly calls across our library of APIs.
For our second API, because you can perform more actions, there is also a longer list of parameters to set before we can use the function. Like with our previous API, you must input the input PDF file, owner password, user password, and encryption key length. However, you also need to add permissions for whether to allow printing, document assembly, content extraction, form filling, editing, annotations, and degraded printing.
To run the function, install the library using the same steps as shown previously, and add the imports to the top of your file. Then, call the function:
Once again, this will return a downloadable file with the chosen permissions set.
Upon the completion of this tutorial, you will be able to password protect and specify permissions on any PDF to add improved security for your documents. If you have any questions about using these APIs or inquiries concerning other API solutions, you can visit the Cloudmersive website where our team is happy to help with anything you might need.
Opinions expressed by DZone contributors are their own.