jPDFViewer Developer Guide
jPDFViewer Developer Guide
jPDFViewer is a Java bean that can be embedded in Java applications to deliver PDF content to your users without the need to install any third party programs or drivers. jPDFViewer integrates seamlessly into your application and delivers the most features, performance and reliability of any Java PDF viewer.
By using jPDFViewer to display your documents, your application has greater control over your PDF content. For instance, the PDF content can be created runtime and can be delivered directly to the viewer through a URL or an input stream without using any temporary files on the local computer. This can be useful when you need to prevent your users from being able to make copies of the files. Additionally, jPDFViewer gives you greater control over the navigation toolbar and its functions, such as printing.
jPDFViewer provides a standard Java bean that can be added to any GUI container, the bean class is com.qoppa.pdfViewer.PDFViewerBean. The bean can be added to a container either programmatically or using any standard GUI editor in your development environment. Once the bean has been added, your application can start showing PDF documents by either loading PDF documents programmatically, based on its own logic, or by letting users open their own PDF documents by clicking on the open button in the toolbar.
The host application can also customize the toolbar by calling the getToolbar() method and removing or adding buttons, override the button handlers, or even hiding the entire toolbar. Action handlers on the toolbar buttons can be overridden to provide custom code to handle the functions. For example, your application can override the open function to show the user a limited list of PDF documents that can be opened.
Opening a Document
Loading and displaying a document programmatically is very simple using jPDFViewer. Once a PDFViewer bean has been added to a container, the host application simply needs to call one of the loadPDF methods in PDFViewerBean and the library will load and display the first page of the document. The three versions of the loadPDF method take a file name, a URL or an InputStream as parameters
PDFViewerBean bean = getViewerBean (); bean.loadPDF ("c:\\test.pdf");
Customizing the toolbar
The toolbar in jPDFViewer can be shown or hidden according to the needs of host application. Additionally, all the buttons in the toolbar are accessible, so they can be enabled/disabled or hidden. The PDFViewerBean class provides a method, getToolbar that returns a reference to the toolbar, a PDFToolbar object. PDFToolbar extends JToolBar and provides methods to access each of the buttons individually. Please refer to the API for more detail.
The following examples assume that viewerBean is a reference to a PDFViewerBean:
To hide the toolbar:
To hide the open button:
viewerBean.getToolbar().getjbOpen ().setVisible (false);
To disable the print button:
Working With Form Fields
Fields in a PDF file are contained in what Adobe calls an AcroForm. jPDFViewer provides access to an AcroForm object from the PDFViewerBean class and through this object, the host application can get the fields present in the document and get or set their data.
To get a reference to the AcroForm, the host application needs to call PDFViewerBean.getAcroForm. The method returns an object of type com.qoppa.pdf.AcroForm. The host application can then use this object to get the list of fields in the PDF document and to get individual fields by name. The field objects in turn have methods to get or set their current value.
jPDFViewer provides a number of additional functions to work with and manipulate PDF documents. The library provides these functions by giving access to an interface class that represents the underlying PDF document. The PDFViewerBean.getDocument method returns an object that implements com.qoppa.pdf.dom.IPDFDocument. Some of the information and functions this object provides are:
- Get information about the PDF document such as the author, creation date and more – IPDFDocument.getDocumentInfo()
- Print the PDF document programmatically by calling – IPDFDocument.print()
- Get the bookmark tree (the table of contents) – IPDFDocument.getRootBookmark()
- Get information about each page – IPDFDocument.getIPage(). This method returns an object that implements IPDFPage
- Get each page’s size, crop box, trim box and bleed box – IPDFPage.getMediaBox(), IPDFPage.getCropBox(), IPDFPage.getTrimBox(), IPDFPage.getBleedBox()
- Export each page to an image – IPDFPage.getImage()
- Get a list of annotations in a page – IPDFPage.getAnnotations()
Distribution and JAR Files
Required and optional jar files for jPDFViewer can be found on the jPDFViewer Download page.