PDFOne (for Java)
Create, edit, view, print & enhance PDF documents and forms in Java SE/EE
Compatibility
J2SE J2EE Windows Linux Mac (OS X)

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;
    
  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
     d = new PdfDocument();
     d.load(txtFilePath.getText());
    
     // 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 .NET Developer Tools
XtremeDocumentStudio .NET

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

PDFOne .NET

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

Our Delphi/C++Builder developer tools
XtremeDocumentStudio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

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.

Our Java developer tools
XtremeDocumentStudio (for Java)

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

PDFOne (for Java)

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

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

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