Encrypt PDF Files - Java

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

Required Knowledge

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.

Create the Project and Add LEADTOOLS References

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>\LEADTOOLS22\Bin\Java.

For this project, the following references are needed:

For a complete list of which JAR files are required for your application, refer to Files to be Included With Your Java Application.

Set the License File

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.

Add the PDF Encryption Code

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.

Java
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 Program class and call it in the run() method, as shown below. Also, add two String values to the run() method, String inFileName = @"C:\LEADTOOLS22\Resources\Images\Leadtools.pdf"; and String outFileName = @"C:\LEADTOOLS22\Resources\Images\LeadtoolsEncrypted.pdf";.

Java
private void run(String[] args) { 
    try { 
        Platform.setLibPath("C:\\LEADTOOLS22\\Bin\\CDLL\\x64"); 
        Platform.loadLibrary(LTLibrary.LEADTOOLS); 
        Platform.loadLibrary(LTLibrary.PDF); 
 
        SetLicense(); 
 
        String inFileName = "C:\\LEADTOOLS22\\Resources\\Images\\Leadtools.pdf"; 
        String outFileName = "C:\\LEADTOOLS22\\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.

Java
// Convert PDF to encrypted and disable printing 
void EncryptPdf(String inputFileName, String outputFileName) { 
    PDFFile inputFile = new PDFFile(inputFileName); 
    System.out.println("Original PDF File Loaded"); 
    inputFile.setSecurityOptions(new PDFSecurityOptions()); 
 
    // Set a password to open the file 
    inputFile.getSecurityOptions().setUserPassword("PasswordToOpen"); 
 
    // Set a password that will be needed when changing the file permissions in the 
    // future 
    inputFile.getSecurityOptions().setOwnerPassword("PermissionsPassword"); 
    // Disable printing 
    inputFile.getSecurityOptions().setPrintEnabled(false); 
    inputFile.getSecurityOptions().setHighQualityPrintEnabled(false); 
    inputFile.getSecurityOptions().setEncryptionMode(PDFEncryptionMode.RC128_BIT); 
     
    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

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.

Wrap-up

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.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.