com.qoppa.pdfViewer
Class PDFViewerBean

java.lang.Object
  extended byjava.awt.Component
      extended byjava.awt.Container
          extended byjavax.swing.JComponent
              extended byjavax.swing.JPanel
                  extended bycom.qoppa.pdfViewer.PDFViewerBean
All Implemented Interfaces:
Accessible, ActionListener, EventListener, IDocumentListener, ImageObserver, IPassword, IPDFActionHandler, MenuContainer, Serializable
Direct Known Subclasses:
PDFNotesBean

public class PDFViewerBean
extends JPanel
implements ActionListener, IPassword, IDocumentListener, IPDFActionHandler

PDFViewer bean is a component that can be used to display PDF documents within an application or an applet. The bean takes a PDF document as a file name, a URL or an InputStream, reads the document and then displays the document, allowing the user to navigage through the document.

Author:
Qoppa Software
See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Field Summary
static int SPLITPOLICY_NEVER_VISIBLE
           
static int SPLITPOLICY_USEDOCUMENTPAGEMODE
           
static int ZOOMMODE_FITPAGE
          FitPage zoom mode changes the scale when the window is resized so that the page is displayed using the maximum available space.
static int ZOOMMODE_FITWIDTH
          FitWidth zoom mode changes the scale when the window is resized so that the full width of the page is displayed using the maximum available space.
static int ZOOMMODE_NORMAL
          Normal zoom mode means that the scale remains the same when the window is resized.
 
Fields inherited from class javax.swing.JComponent
TOOL_TIP_TEXT_KEY, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
PDFViewerBean()
          Construct the PDFViewerBean.
 
Method Summary
 void actionPerformed(ActionEvent e)
          Handles toolbar actions.
 void clearDocument()
          Clears any loaded document.
 void documentChanged(DocumentEvent de)
          Internal method, do not use.
 AcroForm getAcroForm()
          Returns the AcroForm in this document.
 JTree getBookmarkTree()
          Returns a reference to the bookmark tree component.
 IPDFDocument getDocument()
          Returns the PDF document currently displayed.
 int getPageByLocation(int x, int y)
          Returns the index of a page given an x, y location on the document.
 int getPageCount()
          Returns the number of pages in the current document.
 int getPageNumber()
          Returns the currently displayed page.
 JComponent getPageView(int pageNumber)
          Returns the page view component for the given page number.
 IPassword getPasswordHandler()
          Returns the currently registered password handler.
 String[] getPasswords()
          Standard implementation of the password handler.
 JRootPane getRootPane()
          Returns a reference to the PDFViewerBean root pane.
 int getRotation()
          Returns the current rotation.
 int getScale()
          Returns the current scale used to display the PDF document.
 JScrollPane getScrollPane()
          Returns a reference to the PDFViewerBean scroll pane.
 JPopupMenu getSelectContextMenu()
          This returns the context menu displayed when text is selected on a page.
 TextSelection getSelectedText()
          Gets the current text selection (if any page has selected text) in the document.
 SelectToolbar getSelectToolbar()
          Returns the selection toolbar.
 JSplitPane getSplitPane()
          Returns a reference to the split pane used to show bookmarks and layers.
 int getSplitPolicy()
          Gets the viewer's split window policy.
 JTabbedPane getSplitTabs()
          Returns a reference to the tabbed pane inside the split pane.
 PDFToolbar getToolbar()
          Returns the navigation toolbar.
static String getVersion()
          Returns version string for jPDFViewer.
 int getZoomMode()
          Get the current zoom mode.
 void handleAction(Action action)
          This method will handle PDF actions initiated by a click on a link.
 boolean isSplitOpen()
          Returns whether the split window is open or not.
 boolean isSplitVisible()
          Returns the state of the split window.
 void loadPDF(InputStream inStream)
          Load and display a PDF document using an InputStream.
 void loadPDF(String fileName)
          Load and display a PDF document using a file name.
 void loadPDF(URL url)
          Load and display a PDF document using a URL.
 void print(PrintSettings printSettings)
          Prints the PDF document to a printer.
 void rotateCCW()
          Rotates pages 90 degrees counter-clockwise.
 void rotateCW()
          Rotates pages 90 degrees clockwise.
 void scrollToPage(int pageNumber, int x, int y)
          Scroll to the given page and make sure that point (x,y) is visible on the page by scrolling to it if needed.
static boolean setAppletKey(String key, Applet applet)
          Sets product key when running jPDFViewer in an applet.
 void setDocument(IPDFDocument pdfDoc)
          Sets this bean's document.
 void setInvertMode(boolean invertMode)
          Sets the page to paint in inverted mode: Black will show as white and white as black.
static boolean setKey(String key)
          Sets product key.
 void setPage(int pageNumber)
          Changes the currently displayed page.
 void setPasswordHandler(IPassword pwdHandler)
          Override the standard password handler with a user password handler.
 void setPrintSettings(PrintSettings printSettings)
          Sets the print settings to be used when the print button is activated from the toolbar.
 void setRotation(int rotation)
          Sets the current rotation.
 void setScale(int scale)
          Changes the display scale for the PDF document.
 void setSplitOpen(boolean splitOpen)
          Opens or closes the split window.
 void setSplitPolicy(int splitPolicy)
          Sets the viewer's split window policy.
 void setSplitVisible(boolean visible, boolean open)
          Method to show / hide the split pane functionality.
 void setZoomMode(int zoomMode)
          Sets the current zoom mode.
 void startMagnifyRect()
          This method activates the magnification rectangle tool.
 void startTextSelection()
          This methods activates the text selection tool.
 void stopTextSelection()
          This method stops the text selection tool and activates the "hand" tool.
 void zoomToRect(Rectangle2D zoomRect)
           
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, setUI, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getRegisteredKeyStrokes, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paint, paintImmediately, paintImmediately, print, printAll, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, remove, remove, removeAll, removeContainerListener, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setLayout, transferFocusBackward, transferFocusDownCycle, validate
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, contains, createImage, createImage, createVolatileImage, createVolatileImage, dispatchEvent, enable, enableInputMethods, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SPLITPOLICY_USEDOCUMENTPAGEMODE

public static final int SPLITPOLICY_USEDOCUMENTPAGEMODE
See Also:
Constant Field Values

SPLITPOLICY_NEVER_VISIBLE

public static final int SPLITPOLICY_NEVER_VISIBLE
See Also:
Constant Field Values

ZOOMMODE_NORMAL

public static final int ZOOMMODE_NORMAL
Normal zoom mode means that the scale remains the same when the window is resized.

See Also:
Constant Field Values

ZOOMMODE_FITPAGE

public static final int ZOOMMODE_FITPAGE
FitPage zoom mode changes the scale when the window is resized so that the page is displayed using the maximum available space.

See Also:
Constant Field Values

ZOOMMODE_FITWIDTH

public static final int ZOOMMODE_FITWIDTH
FitWidth zoom mode changes the scale when the window is resized so that the full width of the page is displayed using the maximum available space.

See Also:
Constant Field Values
Constructor Detail

PDFViewerBean

public PDFViewerBean()
Construct the PDFViewerBean.

Method Detail

setDocument

public void setDocument(IPDFDocument pdfDoc)
Sets this bean's document. When this method is called, the bean will go back to page 1 of the document and reset the page control in the toolbar. The method will also start generating a new set of thumbviews for the pages.

Parameters:
pdfDoc - The new document.

handleAction

public void handleAction(Action action)
This method will handle PDF actions initiated by a click on a link. If the action is a URL, the method will open a new browser window to show the link. If the action is to launch a file, the method will call the operating system to open the file with the registered program.

This method can be overridden to handle actions in a custom manner. The parameter passed in will be an instance of an object derived from Action and can be tested using 'instanceof' to determine the kind of action.

Specified by:
handleAction in interface IPDFActionHandler
Parameters:
action - Object describing the action.

getAcroForm

public AcroForm getAcroForm()
Returns the AcroForm in this document. Returns null if the document has no fields. Use this method to get a reference to a PDF document's AcroForm to be able to get and set field data.

Returns:
AcroForm The AcroForm object for this document.

getDocument

public IPDFDocument getDocument()
Returns the PDF document currently displayed.

Returns:
The current document.

loadPDF

public void loadPDF(URL url)
             throws PDFException
Load and display a PDF document using a URL.

Parameters:
url - URL pointing to the location of the PDF document.
Throws:
PDFException

clearDocument

public void clearDocument()
Clears any loaded document.


loadPDF

public void loadPDF(InputStream inStream)
             throws PDFException
Load and display a PDF document using an InputStream.

Parameters:
inStream - InputStream used to read the PDF document.
Throws:
PDFException

loadPDF

public void loadPDF(String fileName)
             throws PDFException
Load and display a PDF document using a file name.

Parameters:
fileName - Name of the PDF document file.
Throws:
PDFException

setScale

public void setScale(int scale)
Changes the display scale for the PDF document.

Parameters:
scale - Magnification factor as a percentage. 100 is true size.

getScale

public int getScale()
Returns the current scale used to display the PDF document.

Returns:
Current display scale.

getToolbar

public PDFToolbar getToolbar()
Returns the navigation toolbar. Host applications can use this method to be able to change the behaviour of the toolbar, such as making buttons visible/invisible, adding listeners, etc. Please see PDFToolbar for details.

Returns:
The bean's toolbar.

getSelectToolbar

public SelectToolbar getSelectToolbar()
Returns the selection toolbar. This toolbar has two icons, the hand icon to allow users to drag the document around with the mouse and the text select icon to let the user select text in the document. As with the navigation toolbar, the host application can choose not to show this toolbar by calling getSelectToolbar().setVisible (false)

Returns:
Reference to the selection toolbar.

actionPerformed

public void actionPerformed(ActionEvent e)
Handles toolbar actions. This method is necessary to implement the ActionListener interface. The bean is registered with all buttons in the toolbar to handle their actions. A derived class can override this method to customize the behavior of the toolbar buttons.

Specified by:
actionPerformed in interface ActionListener
Parameters:
e - The action event.

getRotation

public int getRotation()
Returns the current rotation.

Returns:
The bean's current rotation in degrees, valid values are 0, 90, 180 and 270.

setRotation

public void setRotation(int rotation)
Sets the current rotation. Valid values are only 0, 90, 180 and 270.

Parameters:
rotation - Rotation angle in degrees.

rotateCW

public void rotateCW()
Rotates pages 90 degrees clockwise.


rotateCCW

public void rotateCCW()
Rotates pages 90 degrees counter-clockwise.


getPageNumber

public int getPageNumber()
Returns the currently displayed page.

Returns:
int Currently displayed page.

getPageView

public JComponent getPageView(int pageNumber)
Returns the page view component for the given page number.

Parameters:
pageNumber - Number of the page to return. Pages are indexed starting at 1.
Returns:
JComponent Reference to a component object that will display the given page. null if the pageNumber is out of range.

getPageCount

public int getPageCount()
Returns the number of pages in the current document.

Returns:
int Number of pages in the PDF document.

getPasswords

public String[] getPasswords()
Standard implementation of the password handler. This method shows a simple dialog with a password field to allow the user to enter a password.

The standard password handler can be overridden by calling setPasswordHandler

Specified by:
getPasswords in interface IPassword
Returns:
byte array containing the password.

print

public void print(PrintSettings printSettings)
           throws PDFPermissionException,
                  PrinterException
Prints the PDF document to a printer. This method will first prompt the user to choose a system printer and then it will print the document.

If finer control of printing is necessary, the IPDFDocument object return by the getDocument() method implements both Printable and Pageable. The host application can then create its own PrinterJob and set any settings necessary, then call either setPageable or setPrintable with the IPDFDocument object and then start the print job.

Parameters:
printSettings - Print job settings. Use this object to tell jPDFViewer how to print the document. If null, the library will print using default settings.
Throws:
PDFPermissionException
PrinterException

setPrintSettings

public void setPrintSettings(PrintSettings printSettings)
Sets the print settings to be used when the print button is activated from the toolbar. The print settings will be cloned so that any modifications to the PrintSettings object will not be reflected when printing.

Parameters:
printSettings - Print job settings. Use this object to tell jPDFViewer how to print the document.

setPage

public void setPage(int pageNumber)
Changes the currently displayed page.

Parameters:
pageNumber - Page to display. Pages are indexed starting at 1.

getVersion

public static String getVersion()
Returns version string for jPDFViewer. If this is the demo version, the return string will contain 'Demo Version' at the end.


setPasswordHandler

public void setPasswordHandler(IPassword pwdHandler)
Override the standard password handler with a user password handler. If a document is password protected, jPDFViewer will call the password handler to obtain a password.

Parameters:
pwdHandler - User object that implements IPasswordHandler.

getPasswordHandler

public IPassword getPasswordHandler()
Returns the currently registered password handler. The password handler is used when the library opens a document that is protected by passwords.

Returns:
The current password handler.

setKey

public static boolean setKey(String key)
Sets product key. Use this method after purchasing to remove evaluation restrictions.

Parameters:
key - Product key.

setAppletKey

public static boolean setAppletKey(String key,
                                   Applet applet)
Sets product key when running jPDFViewer in an applet. Use this method after purchasing to remove evaluation restrictions.

Parameters:
key - Product key.

setSplitVisible

public void setSplitVisible(boolean visible,
                            boolean open)
Method to show / hide the split pane functionality. When visible, the left side of the bean will show any bookmarks and layers in the PDF file. When not visible, bookmarks and layers will not be displayed to the user and the split bar functionality goes away.

Parameters:
visible - The new split pane visibility.
open - Determines whether the split pane should be open or closed

isSplitVisible

public boolean isSplitVisible()
Returns the state of the split window.

Returns:
Visibility state of the split window.

isSplitOpen

public boolean isSplitOpen()
Returns whether the split window is open or not.

Returns:
Open state of the split window.

setSplitOpen

public void setSplitOpen(boolean splitOpen)
Opens or closes the split window.

Parameters:
splitOpen - Desired open state of the split window.

scrollToPage

public void scrollToPage(int pageNumber,
                         int x,
                         int y)
Scroll to the given page and make sure that point (x,y) is visible on the page by scrolling to it if needed. (x,y) are given in crop box coordinates, the crop box being the visible area of the page.

Parameters:
pageNumber - the page number starting at 1.
x - the horizontal position to scroll to on the page
y - the vertical position to scroll to on the page

documentChanged

public void documentChanged(DocumentEvent de)
Internal method, do not use.

Specified by:
documentChanged in interface IDocumentListener

getScrollPane

public JScrollPane getScrollPane()
Returns a reference to the PDFViewerBean scroll pane. Use this method when finer control of the scroll pane and scroll behaviour is need.

Returns:
The scroll pane used inside the PDFViewerBean.

getRootPane

public JRootPane getRootPane()
Returns a reference to the PDFViewerBean root pane. The document is displayed within a root pane that is then added to a scroll pane. Use this method when finer control of the root pane is needed.


getBookmarkTree

public JTree getBookmarkTree()
Returns a reference to the bookmark tree component. The host application can use this to change the look of the tree.


getSplitTabs

public JTabbedPane getSplitTabs()
Returns a reference to the tabbed pane inside the split pane.

Returns:
the tabbed pane.

getSplitPane

public JSplitPane getSplitPane()
Returns a reference to the split pane used to show bookmarks and layers.

Returns:
The split pane.

getSplitPolicy

public int getSplitPolicy()
Gets the viewer's split window policy. The policy can be one of two values:

SPLITPOLICY_USEDOCUMENTPAGEMODE - When a document is open, the viewer will use the document page mode to decide how the split window should be displayed and what tab (outlines, thumbnails) should be selected. When no page mode is defined for the document, the viewer will select the thumnails in the split window.
SPLITPOLICY_NEVER_VISIBLE - The split window is never visible for any document.

Returns:
The current split window policy.

setSplitPolicy

public void setSplitPolicy(int splitPolicy)
Sets the viewer's split window policy. The policy can be one of two values:

SPLITPOLICY_USEDOCUMENTPAGEMODE - When a document is open, the viewer will use the document page mode to decide how the split window should be displayed and what tab (outlines, thumbnails) should be selected / visible. When no page mode is defined for the document, the viewer will select the thumnails in the split window.
SPLITPOLICY_NEVER_VISIBLE - The split window is never visible for any document.

Parameters:
splitPolicy - The new split window policy.

getPageByLocation

public int getPageByLocation(int x,
                             int y)
Returns the index of a page given an x, y location on the document.

Parameters:
x - The x coordinate of the point.
y - The y coordinate of the point.
Returns:
The index of the page, -1 if the point does not fall on any page.

setInvertMode

public void setInvertMode(boolean invertMode)
Sets the page to paint in inverted mode: Black will show as white and white as black. Colors will be inverted as well by XOR'ing them with all white.

Parameters:
invertMode - The new invert mode.

getZoomMode

public int getZoomMode()
Get the current zoom mode.

Returns:
The current zoom mode, this can either be ZOOMMODE_NORMAL or ZOOMMODE_FITPAGE.

setZoomMode

public void setZoomMode(int zoomMode)
Sets the current zoom mode.

Parameters:
zoomMode - The new zoom mode. This value has to be either ZOOMMODE_NORMAL or ZOOMMODE_FITPAGE. If zoomMode is ZOOMMODE_FITPAGE, the method will call setScale immediately to adjust the scale so that the page fits in the display window optimally.

zoomToRect

public void zoomToRect(Rectangle2D zoomRect)

getSelectedText

public TextSelection getSelectedText()
Gets the current text selection (if any page has selected text) in the document.

Returns:
Object containing information about any text that is selected.

startMagnifyRect

public void startMagnifyRect()
This method activates the magnification rectangle tool.


startTextSelection

public void startTextSelection()
This methods activates the text selection tool.


stopTextSelection

public void stopTextSelection()
This method stops the text selection tool and activates the "hand" tool. If the bean is not in text selection mode, the method does nothing.


getSelectContextMenu

public JPopupMenu getSelectContextMenu()
This returns the context menu displayed when text is selected on a page. The host application can call this method to add custom entries to the context menu.

Returns:
Reference to the popup menu.