How to add a PDF viewer to your Java application
JPedal includes a very comprehensive PDF viewer which makes use of both Swing and optionally JavaFX for the best possible viewing solution. The Viewer is customizable allowing its appearance to be altered to best suite your needs.
The Viewer is written in Java and can be embedded in an existing Swing application.
JavaFX is an optional plugin that you can use to enable the page flow mode.
If you do not have JPedal, you can download a trial copy:
View PDF files in existing Java Application - add Viewer to another Swing Component
// If you want to set your a look and feel
System.setProperty("org.jpedal.userControlledLAF", "javax.swing.plaf.metal.MetalLookAndFeel");
//Create display JFrame
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new BorderLayout());
//This could be any JPanel from your application
final JPanel rootContainer = frame.getContentPane();
Viewer viewer = new Viewer(rootContainer, null);
viewer.setupViewer();
viewer.executeCommand(ViewerCommands.OPENFILE, "inputFile.pdf");
//add viewer to your application
frame.add(rootContainer, BorderLayout.CENTER);
//Display the example frame with embedded PDF viewer
frame.setTitle("Viewer in External Frame");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
As well as adding the Viewer to your own application you can also customize the Viewer to control which options are present using the Viewers properties file. Once you have created a properties file that displays the options you require it can be loaded by the Viewers constructor shown above. To do this you just need to pass in the file’s path as the second parameter.
Proper termination of the Viewer
The Viewer writes files (such as cached images) to a temporary location on the disk. These are cleared once the Viewer is closed, but only if the Viewer is disposed of properly. For example, if you are using System.exit(), or a custom close button as a way to terminate your program, you should ensure that you call Viewer.dispose() first.