This tutorial shows how to encrypts PDF files in a Java application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to encrypt PDF files in a Java application |
Completion Time | 20 minutes |
Visual Studio Project | Download tutorial project (1 KB) |
Platform | Java Application |
IDE | Eclipse |
Development License | Download LEADTOOLS |
Try it in another language |
|
Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Encrypt PDF Files - Java tutorial.
Start with a copy of the project created in the Add References and Set a License tutorial. If the project is not available, follow the steps in that tutorial to create it.
The references needed depend upon the purpose of the project. References can be added by local .jar
files located at <INSTALL_DIR>\LEADTOOLS23\Bin\Java
.
For this project, the following references are needed:
leadtools.codecs.jar
leadtools.jar
leadtools.pdf.jar
For a complete list of which JAR files are required for your application, refer to Files to be Included With Your Java Application.
The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.
There are two types of runtime licenses:
Note: Adding LEADTOOLS references and setting a license are covered in more detail in the Add References and Set a License tutorial.
With the project created, the references added, and the license set, coding can begin.
In the Project Explorer, open _Main.java
. Add the following statements to the import
block at the top.
import java.io.IOException;
import java.nio.file.*;
import leadtools.*;
import leadtools.pdf.*;
Add a new method named EncryptPdf(String inputFileName, String outputFileName)
to the _Main_
class and call it in the run()
method, as shown below. Also, add two String
values to the method, one for the input file and one for the output file.
private void run(String[] args) {
try {
Platform.setLibPath("C:\\LEADTOOLS23\\Bin\\CDLL\\x64");
Platform.loadLibrary(LTLibrary.LEADTOOLS);
Platform.loadLibrary(LTLibrary.PDF);
SetLicense();
String inFileName = "C:\\LEADTOOLS23\\Resources\\Images\\Leadtools.pdf";
String outFileName = "C:\\LEADTOOLS23\\Resources\\Images\\LeadtoolsEncrypted.pdf";
EncryptPdf(inFileName, outFileName);
} catch (Exception ex) {
System.err.println(ex.getMessage());
ex.printStackTrace();
}
}
Add the code below to the new method to set the PDFSecurityOptions
, encrypt the PDF file, and output the new encrypted PDF to the file path set above.
void EncryptPdf(String inputFileName, String outputFileName) {
PDFFile inputFile = new PDFFile(inputFileName);
System.out.println("Original PDF File Loaded");
// Set Security Options
PDFSecurityOptions securityOptions = new PDFSecurityOptions();
securityOptions.setUserPassword("PasswordToOpen");
securityOptions.setOwnerPassword("PermissionsPassword");
// Disable Printing
securityOptions.setPrintEnabled(false);
securityOptions.setHighQualityPrintEnabled(false);
securityOptions.setEncryptionMode(PDFEncryptionMode.RC128_BIT);
inputFile.setSecurityOptions(securityOptions);
System.out.println("Security Options Set.");
inputFile.convert(1, -1, outputFileName);
System.out.println("Encrypted PDF Produced.");
System.out.println("Checking if output PDF is encrypted: " + PDFFile.isEncrypted(outputFileName));
}
Run the project by pressing Ctrl + F11, or by selecting Run -> Run.
If the steps were followed correctly, the application runs and converts the input PDF file to a new encrypted PDF file, using the specified PDFSecurityOptions
.
This tutorial showed how to save PDF files with encryption and how to control their security permissions to disallow printing. We also covered how to use the PDFFile
and PDFSecurityOptions
classes.