Swing Integration with ViewerPanel

This tutorial assumes you already have a basic knowledge of Swing.

Introduction

As with any state-of-the-art PDF renderer, jPDF allows application authors to integrate a PDF rendering panel directly into their Swing GUI applications. The integration can be done through the use of the ViewerPanel class.

In its simplest form the ViewerPanel shows a user interface like the one below. jPDF Viewer can display any type of PDF document. It provides tools to zoom in and out, paginate, rotate and print.

Its panel supports drag-and-drop and has extended usability features ... Try to double click the page index display area.

jPDF Viewer can be integrated with other modules of the suite to provide access to form fields, digital signature, encryption, etc., ...

The code

The code to generate the above GUI is quite simple and won't surprise anyone used to Swing programming. We just instantiate a ViewerPanel and add it to a JFrame. If you need to run this sample you will find that it is part of the distribution.

package com.crionics.jpdf.samples.viewer;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import javax.swing.JFrame;
import com.crionics.jpdf.PdfException;
import com.crionics.jpdf.viewer.ViewerPanel; 

/**
* Sample demo which bundles the ViewerPanel in a Frame.
*/

public class SampleViewerFrame extends JFrame
{
  private ViewerPanel viewer;

  public SampleViewerFrame() throws IOException, PdfException
  { 

    setTitle("jPDF Viewer Sample"); 

    // End the application when the window closes:
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) { System.exit(0);}
    });

    // Create the pdf viewer panel:
    viewer = new ViewerPanel(); <brsetContentPane(viewer);

    // Uncomment this line to preload a document:
    //viewer.loadDocument(new File("mydocument.pdf"));

    pack();

  }

  public static void main(String arg[]) throws Exception
  {
    SampleViewerFrame f = new SampleViewerFrame(); 
    f.setVisible(true);
  }

}

Using jPDF Viewer in an applet

Since jPDF Viewer is a 100% Java Swing component extending from JPanel, it can easily be integrated with an applet.

The code below shows how to display the viewer inside an applet. It is important to note that in order to be fully functional, the applet will have to be signed. Thanks to Java security features, only signed applets can have free access to the file system and network resources.

package com.crionics.jpdf.samples.viewer;
import javax.swing.JApplet;
import com.crionics.jpdf.viewer.ViewerPanel;

/**
* Sample implementation to show jPDF Viewer in an applet
*/
public class SampleViewerApplet extends JApplet {

    private ViewerPanel panel;

    // instanciates jPDF Viewer
    public void init()
    {
      panel = new ViewerPanel();
      setContentPane(panel);
    }

    public void start() { }

    public void stop() { }

    /** close document and force memory release */
    public void destroy() {
      panel.closeDocument();
      panel = null;
      System.gc();
    }

}