-
Notifications
You must be signed in to change notification settings - Fork 24
Implementing a SecurityCallback
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 packageorg.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
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.