PDFOne (for Java™)
Powerful all-in-one PDF library for Java
Compatibility
J2SE J2EE

How To Create a Java PDF Viewer In A Snap

Display PDF documents in a Java GUI application for end-users.
By Santhanam L.

Last month, we released Version 2.00 of PDFOne Java. In this major update of our Java PDF library, we introduced two new components - PdfViewer and PdfPrinter. In this article, we will see how to create a Java PDF viewer application using NetBeans IDE. This application will your end-users to view PDF documents.

Here we go!

  1. Create a new Java Application project.
  2. Add a JFrame Form to Source Packages.
  3. Add a JToolBar to the top of the form.
  4. Add the following components to the toolbar (see screenshot).
    • JTextField - 1 No.
      • In Properties window, change name of the field to txtFilePath
    • JButton - 1 No.
      • Change the name of the button to btnLoad and specify an icon image for the button.
    • JButton - 4 Nos.
      • Change their Text property to First Page, Previous Page, Next Page, and Last Page.
      • Change their names to btnFirstpage, btnPreviousPage, btnNextPage and btnLastPage.
  5. Resize the JToolBar component so that it looks like the address bar of a browser.
  6. Add PdfViewer component to Palette Window:
    • On Palette Window, right-click Beans, and select Palette Manager... on the shortcut menu.
    • On the Palette Manager dialog, click on Add from JAR..., select the file PDFOne.jar, and click on Next >.
    • Select PdfViewer and PdfPrinter from Marked JavaBeans list and click Next >.
    • Select Beans as the palette catagory for the selected "marked JavaBeans" and click Finish.
  7. Drag and drop GnosticePDFViewer bean to the JFrame, and resize it so that it occupies the whole of the JFrame minus the toolbar.
  8. Add the following declarations to the class declaration.
    PdfDocument d;
    PdfReader r;
    
  9. Add the following code for the actionPerformed event of the btnLoad JButton.
    try {
     // Read PDF document specified by the user
     // in the text field
     r = PdfReader.fileReader(txtFilePath.getText());
     d = new PdfDocument(r);
    
     // Display the document in viewer
     viewer.loadDocument(d);
    
     setTitle(txtFilePath.getText() 
              + " - Gnostice PDF Viewer");
    } catch(PdfException pdfEx) {
     JOptionPane.showMessageDialog(
             this,
    	 pdfEx.getMessage(),
    	 "Gnostice PDF Viewer",
            JOptionPane.ERROR_MESSAGE);
    
     setTitle("Gnostice PDF Viewer");
    } catch(IOException ioEx) {
     JOptionPane.showMessageDialog(
             this,
    	 ioEx.getMessage(),
    	 "Gnostice PDF Viewer",
             JOptionPane.ERROR_MESSAGE);
             setTitle("Gnostice PDF Viewer");
    }
    
  10. Set the above event handler to take care of the actionPerformed event of txtFilePath JButton.
  11. Add the following code for btnFirstPage JButton's actionPerformed event.
    try {
     // Display first page of the document
     viewer.firstPage();
    } catch(PdfException pdfe) {
     JOptionPane.showMessageDialog(
             this,
    	 pdfe.getMessage(),
    	 "Gnostice PDF Viewer",
             JOptionPane.ERROR_MESSAGE);
    } catch(IOException ioe) {
     JOptionPane.showMessageDialog(
             this,
    	 ioe.getMessage(),
    	 "Gnostice PDF Viewer",
             JOptionPane.ERROR_MESSAGE);
    }
    
    Add similar code for the actionPerformed events of btnPreviousPage, btnNextPage, and btnLastPage JButtons.

You can further improve the viewer by adding support for

  1. zoom (page magnification)
  2. page number display
  3. viewer resizing - in sync with window resizing
---o0O0o---

Downloads:

---o0O0o---

Our Developer Tools
eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

XtremePDFConverter VCL

A Delphi/C++Builder component to intelligently convert PDF to user-friendly Word RTF documents.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

XtremeDocumentStudio .NET

Multi-format document-processing component suite for .NET developers

PDFOne (for Java™)

A Java™ PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java™ applications.

XtremeFontEngine (for Java)

Java font engine to render glyphs from Type 1, Type 2 (CFF), and TrueType fonts

Our Office Productivity Applications
Free PDF Reader

A free, fast, and portable application for viewing, printing and converting PDF documents.

Privacy | Legal | Feedback | Newsletter | Resellers © 2002-2013 Gnostice Information Technologies Private Limited. All rights reserved.

This site is best viewed on a screen with minimum resolution of 1152 x 864 pixels. Windows XP users are advised to use Microsoft ClearType Tuning for optimal experience. Also, please use the latest version of a standards-compliant browser such as Firefox, Opera, or Dragon (Chromium).