Some test text!


Encrypt and decrypt PDF files in Java

Sample Java code for using PDFTron SDK to read encrypted (password protected) documents, secure a document with encryption, or remove encryption.

Get StartedSamplesDownload

To run this sample, get started with a free trial of PDFTron SDK.

// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.


import com.pdftron.common.PDFNetException;
import com.pdftron.filters.FilterReader;
import com.pdftron.filters.FlateEncode;
import com.pdftron.filters.MappedFile;
import com.pdftron.pdf.*;
import com.pdftron.sdf.*;

// This sample shows encryption support in PDFNet. The sample reads an encrypted document and 
// sets a new SecurityHandler. The sample also illustrates how password protection can 
// be removed from an existing PDF document.
public class EncTest {
    public static void main(String[] args) {

        // Relative path to the folder containing test files.
        String input_path = "../../TestFiles/";
        String output_path = "../../TestFiles/Output/";

        // Example 1:
        // secure a document with password protection and
        // adjust permissions

        try {
            // Open the test file
            System.out.println("Securing an existing document ...");
            PDFDoc doc = new PDFDoc((input_path + "fish.pdf"));

            // Perform some operation on the document. In this case we use low level SDF API
            // to replace the content stream of the first page with contents of file 'my_stream.txt'
            if (true)  // Optional
                System.out.println("Replacing the content stream, use flate compression...");

                // Get the page dictionary using the following path: trailer/Root/Pages/Kids/0
                Obj page_dict = doc.getTrailer().get("Root").value()

                // Embed a custom stream (file mystream.txt) using Flate compression.
                MappedFile embed_file = new MappedFile((input_path + "my_stream.txt"));
                FilterReader mystm = new FilterReader(embed_file);
                                new FlateEncode(null)));

            //encrypt the document

            // Apply a new security handler with given security settings.
            // In order to open saved PDF you will need a user password 'test'.
            SecurityHandler new_handler = new SecurityHandler();

            // Set a new password required to open a document
            String user_password = "test";

            // Set Permissions
            new_handler.setPermission(SecurityHandler.e_print, true);
            new_handler.setPermission(SecurityHandler.e_extract_content, false);

            // Note: document takes the ownership of new_handler.

            // Save the changes.
            System.out.println("Saving modified file...");
   + "secured.pdf"), SDFDoc.SaveMode.NO_FLAGS, null);
            // output PDF doc
        } catch (PDFNetException e) {

        // Example 2:
        // Opens the encrypted document and removes all of
        // its security.
        try {
            PDFDoc doc = new PDFDoc((output_path + "secured.pdf"));

            //If the document is encrypted prompt for the password
            if (!doc.initSecurityHandler()) {
                boolean success = false;
                System.out.println("The password is: test");
                for (int count = 0; count < 3; count++) {
                    BufferedReader r = new BufferedReader(new InputStreamReader(;
                    System.out.println("A password required to open the document.");
                    System.out.print("Please enter the password: ");
                    String password = r.readLine();
                    if (doc.initStdSecurityHandler(password)) {
                        success = true;
                        System.out.println("The password is correct.");
                    } else if (count < 3) {
                        System.out.println("The password is incorrect, please try again");
                if (!success) {
                    System.out.println("Document authentication error....");

            //remove all security on the document
   + "not_secured.pdf", SDFDoc.SaveMode.NO_FLAGS, null);
            // output PDF doc
        } catch (Exception e) {

        System.out.println("Test completed.");

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

PDFTron Receives USD$71 Million Growth Investment Led By Silversmith Capital Partners

Learn More