Nathan Howard Nathan is a developer at IDRSolutions, focusing mainly on the development of HTML5 and Form support for the online PDF converter. He was a speaker and exhibitor at JavaOne 2013, co-presenting a session titled 'Lessons Learned from Using GlassFish with NetBeans". Nathan also enjoys writing technical blog-articles.

Writing a JavaFX PDF Viewer plugin for Netbeans – 8. Opening Encrypted PDFs

3 min read


In part 6&7 we finished linking the PDF file type with our plugin. Currently when an encrypted PDF is opened, it will not open up within NetBeans’ own text editor. In this article we will get encrypted PDFs to open using our JavaFX PDF Viewer plugin.

Because our plugin relies upon our JPedal Java FX Viewer to view PDF’s, the implementation to read encrypted files was instantiated in the JPedal Library. We’ll be showing you how we went about adding this functionality to our JavaFX Viewer which in turn allows us to open encrypted PDF’s in our NetBeans plugin!

 Getting Started

Firstly, we must ensure we have the bouncy-castle jar files added to our plugins class-path. This can be done by right-clicking our NetBeans plugin project and navigating to Properties, then we click Libraries on the left hand side menu, then we press Wrapped JARs on the tabbed middle menu. When you download our plugin all of these jars are already included, this is an example of how you would do it yourself from a development point of view. Adding bouncy-castle is important as our library relies on bouncy-castle to deal with encrypted PDFs, without the bcprov.jar and bcmail.jar our plugin will not be able to open encrypted PDF’s.


Currently when we open an encrypted file with our NetBeans JavaFX Plugin this is what we see :


We will now implement the Encryption Feature into our Plugin & JavaFX Viewer.

Coding the Encryption Feature in JavaFX

The first thing to-do was to open our Java class “BaseViewerFX” and to navigate to our method which allows us to load PDF files “loadPDF(File)”. We then wrote a block of code which will open an encrypted PDF if the user has added it as a JVM option (example of PDF password as a JVM option), if they haven’t then our JavaFX Viewer will ask them to enter a password.

    public PDFDisplayTopComponent(String file){
try {
   //Open the pdf file so we can check for encryption
   * This code block deals with user input and JVM passwords in Encrypted PDF documents.
      int passwordCount = 0;        //Monitors how many attempts there have been to the password
      closePasswordPrompt = false;  //Do not close the prompt box
      //While the PDF content is not viewable, repeat until the correct password is found
      while(!pdf.isFileViewable() && !closePasswordPrompt) {
        * See if there's a JVM flag for the password & Use it if there is
        * Otherwise prompt the user to enter a password
    password = System.getProperty("org.jpedal.password");
}else if(!closePasswordPrompt){
//If we have a password, try and open the PdfFile again with the password
if (password != null) {
           pdf.openPdfFile(input.getAbsolutePath(), password);
passwordCount=passwordCount+1; //Increment he password attempt
} catch (PdfException ex) {
// If the pdf failed to open, don't decode it.

The next step was to write a method which will display a JavaFX window when an encrypted PDF requires a password to view the content. We called this method “showPasswordPrompt(int)”, it takes an int as a parameter which keeps track of the users attempts to enter a password.

public PDFDisplayTopComponent(String file){
    * This method will show a popup box and request for a password.
    * If the user does not enter the correct password it will ask them to try again.
    * If the user presses the Cross button, the password prompt will close.
    * @param passwordCount is an int which represents the current input attempt
   private void showPasswordPrompt(final int passwordCount){
       //Setup password prompt content
       final Stage enterPasswordStage = new Stage();
       Button okButton = new Button("Ok");
       Text titleText = new Text("Password Request");
       final TextField inputPasswordField = new TextField("Please Enter Password");
       //If the user has attempted to enter the password more than once, change the text
       if(passwordCount >= 1){
           titleText.setText("Incorrect Password");
           inputPasswordField.setText("Please Try Again");
       //Setup the password prompt & add children
       enterPasswordStage.setScene(new Scene(VBoxBuilder.create().children(titleText, inputPasswordField, okButton).
       alignment(Pos.CENTER).padding(new Insets(10)).build()));
       //If the Ok button is pressed, store the user input as the password
       okButton.setOnAction(new EventHandler() {
           @Override public void handle(ActionEvent e) {
               password = inputPasswordField.getText();
       //Check whether stage is closed with the cross button, if it is stop requesting password.
       enterPasswordStage.setOnCloseRequest(new EventHandler() {
         public void handle(WindowEvent we) {
             closePasswordPrompt = true;
       //Halt everything until the Ok button / Cross button is pressed

See It In Action

Now the code has been implemented in our JavaFX BaseViewer class, you can see, when running our JavaFX NetBeans plugin, when we attempt to open an encrypted PDF, we are asked to enter a password.


If the user enters a wrong password, then the content cannot be decrypted and the user is asked to try again. The user can close the password prompt box at any-time by pressing the “Cross-Button” in the top-right, this allows the user to continue to use the Viewer functionality to possibly load in a new PDF.


When the correct password is entered, the PDF content is displayed.


Thank you for reading, hopefully you have found this part of our series interesting, keep an eye on the blog for future updates for the plugin and hopefully a few other surprises… You can also download our plugin from here.

This post is part of our “NetBeans article Index” series. In these articles, we aim to explore NetBeans in different ways, from useful hint and tips, to our how-to’s, experiences and usage of the NetBeans IDE.

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (TwitterFacebook and Google+) or the Blog RSS.

IDRsolutions develop a Java PDF Viewer and SDK, an Adobe forms to HTML5 forms converter, a PDF to HTML5 converter and a Java ImageIO replacement. On the blog our team post anything interesting they learn about.

Nathan Howard Nathan is a developer at IDRSolutions, focusing mainly on the development of HTML5 and Form support for the online PDF converter. He was a speaker and exhibitor at JavaOne 2013, co-presenting a session titled 'Lessons Learned from Using GlassFish with NetBeans". Nathan also enjoys writing technical blog-articles.

2 Replies to “Writing a JavaFX PDF Viewer plugin for Netbeans –…”

  1. Thank you very much for this very good tutorial series. it was very informative and useful. Hoping for future updates of the plugins. Thanks very much once again.

  2. I have been getting emails for recruiters looking to hire you. Apparently you are my alter ego. 🙂 Just thought it was funny.

Leave a Reply

Your email address will not be published. Required fields are marked *