Implementing a SecurityCallback

Table of Contents


When the Document class encounters a PDF document that is encrypted with Acrobat standard security, it first tries to open the PDF file with an empty password string. If the Document class fails to validate the empty password, the application must have a mechanism to request the password. You can use the org.icepdf.core.SecurityCallback interface to do this.

The interface has one method, which is called by the Document class to retrieve a document's password. You can implement the SecurityCallback interface in numerous ways to meet the needs of your application. For example, the package org.icepdf.core.ri.common contains reference code for the SecurityCallback in the class MyGUISecurityCallback.

Consider the following code which would allow a user to type in the documents password if needed.

// new document instance
Document document = new Document();
// setup a security callback before opening an encrypted document.
document.setSecurityCallback(new SecurityCallback(){
    public String requestPassword(Document document) {
        System.out.println(
            "This document is Encrypted please type the document password:");
        String input = "";
        // get users  password
        try {
            BufferedReader stdin =
                new BufferedReader(new InputStreamReader(System.in));
            input = stdin.readLine();
        } catch (IOException e) {}
        return input;
    }
});
// finally open the document.
document.setFile(filePath);

The anonymous inner class allows a developer to handle how they want to ask a user for the password. In this case the command line is used, but alternatively a dialog or some other input form could have been use.

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

© Copyright 2017 ICEsoft Technologies Canada Corp.