public class PDFViewer extends JPanel implements Scrollable
The PDFViewer
class is a simple Swing PDF viewer application.
It demonstrates the DocumentPanel
class, and can be run directly
from the JAR (via the PDFTool
class) like so.
java -jar bfopdf.jar filename
The filename argument is optional, but if supplied will load the specified PDF.
The viewer is also a Swing component, and can be used like any other JComponent by adding it to a Swing hierarchy. All of the operations on the PDFViewer object (and any of the objects it references, like features, DocumentPanels and so on) require that the viewer has been added to the Swing a hierarchy. Attempting to use any of these features will fail
See the viewer tutorial for more detail on how to use this class and the "viewer" package.
currentUser | The name of the current user - optional, but if set this will be set as the author field on any annotations created by the user |
---|---|
unpromptedDirtyClose | Whether to prompt the user for confirmation when closing a window containng a PDF that has been modified |
dpi | The resolution of the screen. This can be set to override Toolkit.getScreenResolution() if required |
This code is copyright the Big Faceless Organization. You're welcome to use, modify and distribute it in any form in your own projects, provided those projects continue to make use of the Big Faceless PDF library.
JPanel.AccessibleJPanel
JComponent.AccessibleJComponent
Container.AccessibleAWTContainer
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
Constructor and Description |
---|
PDFViewer(Collection<ViewerFeature> features)
Creates new PDFViewer with the specified features
|
Modifier and Type | Method and Description |
---|---|
void |
addDocumentPanelListener(DocumentPanelListener listener)
Add a
DocumentPanelListener to any DocumentPanel objects
created by this PDFViewer. |
void |
close()
Close the PDFViewer.
|
void |
closeDocumentPanel(DocumentPanel panel)
Close the specified
DocumentPanel |
DocumentPanel |
getActiveDocumentPanel()
Return the active
DocumentPanel |
String |
getCurrentUser()
Get the name of the current user, as set by
setCurrentUser(java.lang.String) |
DocumentPanelListener[] |
getDocumentPanelListeners()
Return a list of
DocumentPanelListener objects added to this class
with the addDocumentPanelListener(org.faceless.pdf2.viewer2.DocumentPanelListener) method |
DocumentPanel[] |
getDocumentPanels()
Return all the
DocumentPanel s in the viewer |
<T extends ViewerFeature> |
getFeature(Class<T> clazz)
Return the first
ViewerFeature of the specified class, or
null if it doesn't exist for this viewer. |
ViewerFeature |
getFeature(String feature)
|
ViewerFeature[] |
getFeatures()
Return the list of all features set in the viewer
|
JSManager |
getJSManager()
Return the JSManager object for this PDFViewer.
|
KeyStoreManager |
getKeyStoreManager()
Get the
KeyStoreManager in use by this PDFViewer |
int |
getMaxZoom()
Returns the maximum zoom level that should be used.
|
JMenu |
getMenu(String name)
Return the specified Menu.
|
int |
getMinZoom()
Returns the minimum zoom level that should be used.
|
JComponent |
getNamedComponent(String name)
Return a Component created by a
ViewerFeature |
Preferences |
getPreferences()
Return the
Preferences object that should be used to store user preferences
about the viewer, or null if preferences cannot be saved. |
Dimension |
getPreferredScrollableViewportSize()
Returns
JComponent.getPreferredSize() ; for the Scrollable interface. |
PropertyManager |
getPropertyManager()
Get the
PropertyManager in use by this PDFViewer |
int |
getScrollableBlockIncrement(Rectangle visibleRect,
int orientation,
int direction)
Returns 0; for the
Scrollable interface. |
boolean |
getScrollableTracksViewportHeight()
Returns true; for the
Scrollable interface. |
boolean |
getScrollableTracksViewportWidth()
Returns true; for the
Scrollable interface. |
int |
getScrollableUnitIncrement(Rectangle visibleRect,
int orientation,
int direction)
Returns 0; for the
Scrollable interface. |
boolean |
getUnpromptedDirtyClose()
Return the value set by
setUnpromptedDirtyClose(boolean) |
int[] |
getZoomIntervals()
Returns the zoom intervals that should be used to increase or
decrease zoom level.
|
boolean |
hasFeature(String feature)
Return true if the Viewer has a certain feature enabled.
|
boolean |
hasFeature(ViewerFeature feature)
Return true if the Viewer has a certain feature enabled.
|
void |
loadPDF(File ffile)
Load a PDF into the viewer from a file.
|
void |
loadPDF(File file,
EncryptionHandler handler)
Deprecated.
this method has been superceded by the
PDFImporter class |
void |
loadPDF(File file,
EncryptionHandler[] handlers)
Deprecated.
this method has been superceded by the
PDFImporter class |
void |
loadPDF(File file,
int pagenumber)
Load a PDF into the viewer from a file.
|
void |
loadPDF(InputStream in,
EncryptionHandler[] handlers,
String title,
File file)
Deprecated.
this method has been superceded by the
PDFImporter class |
void |
loadPDF(PDFParser parser,
String name)
Load a pre-loaded PDF into the viewer.
|
void |
loadPDF(PDFParser parser,
String name,
int pagenumber)
Load a pre-loaded PDF into the viewer.
|
void |
loadPDF(PDFParser parser,
String name,
int pagenumber,
File file,
boolean addtomostrecent,
DocumentPanelListener listener)
Load a pre-loaded PDF into the viewer.
|
void |
loadPDF(PDF pdf,
String name)
Load a pre-loaded PDF into the viewer.
|
void |
loadPDF(URL url)
Load a PDF into the viewer from a URL.
|
static void |
main(String[] args)
The main() method can be invoked to run this class from the command line.
|
static PDFViewer |
newPDFViewer()
Create a new PDFViewer object in a frame of it's own.
|
static PDFViewer |
newPDFViewer(Collection<ViewerFeature> features)
Create a new PDFViewer object in a frame of its own with the specified features.
|
void |
putNamedComponent(String name,
JComponent value)
Add a named component to the viewers list
|
void |
removeDocumentPanelListener(DocumentPanelListener listener)
Remove a
DocumentPanelListener previously added to the class
with the addDocumentPanelListener(org.faceless.pdf2.viewer2.DocumentPanelListener) method. |
void |
setCurrentUser(String user)
Set the name of the current user (for Annotations).
|
void |
setKeyStoreManager(KeyStoreManager manager)
Set the
KeyStoreManager used by this PDFViewer |
JMenuItem |
setMenu(String name,
char mnemonic,
boolean documentrequired,
ActionListener listener)
Add or replace a menu item in the viewer.
|
void |
setPreferences(Preferences preferences)
Set the
Preferences object returned from getPreferences() . |
void |
setPropertyManager(PropertyManager manager)
Set the
PropertyManager in use by this PDFViewer |
void |
setUnpromptedDirtyClose(boolean ignore)
Set whether to allow a window containing a modified PDF to be closed
without prompting.
|
void |
setZoomIntervals(int[] intervals)
Set the list of zoom intervals that should be used to increase or
decrease zoom level.
|
void |
setZoomRange(int min,
int max)
Set the minumum and maximum zoom values.
|
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
public PDFViewer(Collection<ViewerFeature> features)
features
- a Collection of ViewerFeature
objects that are enabledViewerFeature
public Preferences getPreferences()
Preferences
object that should be used to store user preferences
about the viewer, or null
if preferences cannot be saved.public void setPreferences(Preferences preferences)
Preferences
object returned from getPreferences()
. There's
normally no reason to do this unless particularly don't want to use the normal
system preferences in the viewerpublic JSManager getJSManager()
public final void setPropertyManager(PropertyManager manager)
PropertyManager
in use by this PDFViewerpublic final PropertyManager getPropertyManager()
PropertyManager
in use by this PDFViewerpublic KeyStoreManager getKeyStoreManager()
KeyStoreManager
in use by this PDFViewerpublic void setKeyStoreManager(KeyStoreManager manager)
KeyStoreManager
used by this PDFViewerpublic boolean hasFeature(ViewerFeature feature)
feature
- the feature to look forpublic boolean hasFeature(String feature)
feature
- the name of the feature to look forpublic ViewerFeature[] getFeatures()
public ViewerFeature getFeature(String feature)
feature
- the name of the featurepublic <T extends ViewerFeature> T getFeature(Class<T> clazz)
ViewerFeature
of the specified class, or
null
if it doesn't exist for this viewer. For example
PDFImporter importer = viewer.getFeature(PDFImporter.class);
clazz
- the Class of the ViewerFeature.public JMenuItem setMenu(String name, char mnemonic, boolean documentrequired, ActionListener listener)
Action
then the Action.NAME
and Action.ACCELERATOR_KEY
(if set) will override the name
and
mnemonic
supplied here, and the documentrequired
parameter will
be ignored, as the Action is expected to manage it's own enabled
state.name
- the name of the menu, if the form "File\tOpen" or "File\tQuit(999)". The tab
character is used to separate the items in the menu hierarchy, and the optional bracketed
value is used to control ordering in the menu.mnemonic
- the mnemonic to assign to this menu - lowercase or uppercase letter, or
(char)0
for no mnenonicdocumentrequired
- whether this menu item should only be enabled if a document is loadedlistener
- the ActionListener
to run when this menu item is activatedpublic JMenu getMenu(String name)
name
- the name of the Menu, before it's been localizedpublic void putNamedComponent(String name, JComponent value)
public JComponent getNamedComponent(String name)
ViewerFeature
public Dimension getPreferredScrollableViewportSize()
JComponent.getPreferredSize()
; for the Scrollable
interface.getPreferredScrollableViewportSize
in interface Scrollable
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
Scrollable
interface.getScrollableUnitIncrement
in interface Scrollable
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
Scrollable
interface.getScrollableBlockIncrement
in interface Scrollable
public boolean getScrollableTracksViewportWidth()
Scrollable
interface.getScrollableTracksViewportWidth
in interface Scrollable
public boolean getScrollableTracksViewportHeight()
Scrollable
interface.getScrollableTracksViewportHeight
in interface Scrollable
public void loadPDF(File ffile)
Importer
objects in order until it finds one that matches the file,
then will use that to load the PDF into the viewer. Prompting for passwords
etc. is left to the appropriate Importer
- see the PDFImporter
class for details on the default behaviour.ffile
- the PDF file to load, or null
to select it with a chooserpublic void loadPDF(File file, int pagenumber)
Importer
objects in order until it finds one that matches the file,
then will use that to load the PDF into the viewer. Prompting for passwords
etc. is left to the appropriate Importer
- see the PDFImporter
class for details on the default behaviour.file
- the PDF file to load, or null
to select it with a chooserpagenumber
- the initial page number to request. May be overriddenpublic void loadPDF(File file, EncryptionHandler handler)
PDFImporter
classPDFImporter
passed in to the
PDF Constructor.file
- the PDF Filehandler
- the EncryptionHandler to usepublic void loadPDF(File file, EncryptionHandler[] handlers)
PDFImporter
classPDFImporter
passed in to the
PDF Constructor.file
- the PDF Filehandlers
- the list of EncryptionHandlerspublic void loadPDF(InputStream in, EncryptionHandler[] handlers, String title, File file)
PDFImporter
classLoad a PDF into the viewer from an InputStream. Note that this method will return immediately, which means the calling thread must not close the InputStream. Doing so will result in an Exception. The stream will be closed by the reading thread when it is completed.
This method is now deprecated - the correct way to load a PDF from an
InputStream is to call the Importer.getImporter(PDFViewer, InputStream, String, File)
method. For example:
PDFImporter importer = viewer.getFeature(PDFImporter.class); importer.getImporter(viewer, inputstream, title, file).start(viewer, "Loading");
in
- the InputStream to load the PDF fromhandlers
- the EncryptionHandlers to use to try to decrypt the PDFtitle
- The name of the Window. May be null.file
- If using a save dialog, the initial value to set the dialog to. May be null.public void loadPDF(URL url) throws IOException
url
- the URL to loadIOException
PDFImporter
public void loadPDF(PDF pdf, String name)
loadPDF(new PDFParser(pdf), name)
.pdf
- the PDF to loadname
- the name of the PDF, to display in the title bar.public void loadPDF(PDFParser parser, String name)
parser
- the PDFParser referencing the PDF to loadname
- the name of the PDF, to display in the title bar.public void loadPDF(PDFParser parser, String name, int pagenumber)
parser
- the PDFParser referencing the PDF to loadname
- the name of the PDF, to display in the title bar.pagenumber
- the initial page to displaypublic void loadPDF(PDFParser parser, String name, int pagenumber, File file, boolean addtomostrecent, DocumentPanelListener listener)
parser
- the PDFParser referencing the PDF to loadname
- the name of the PDF, to display in the title barpagenumber
- the initial page to displayfile
- the file to pass to DocumentPanel.setAssociatedFile(java.io.File)
. May be null.addtomostrecent
- whether to add the File containing the PDF to the list of recently opened fileslistener
- an optional callback object. If specified, this listener will be called with a
"created" DocumentPanelEvent
specifying the DocumentPanel created by this method. The listener
will not be added to the PDFViewer's list
, and the event will not
be fired in the normal way to those listener. This approach can be used to run a one-off action on
the panel immediately when it's created, without needing to add listeners and wait for the event to be
fired.public DocumentPanel getActiveDocumentPanel()
DocumentPanel
public DocumentPanel[] getDocumentPanels()
DocumentPanel
s in the viewerpublic void close()
public void closeDocumentPanel(DocumentPanel panel)
DocumentPanel
panel
- the panel to close - usually the return value of getActiveDocumentPanel()
public void addDocumentPanelListener(DocumentPanelListener listener)
DocumentPanelListener
to any DocumentPanel
objects
created by this PDFViewer. The listener will be attached to any existing
DocumentPanel
objects, and added to any new ones that are created later.public void removeDocumentPanelListener(DocumentPanelListener listener)
DocumentPanelListener
previously added to the class
with the addDocumentPanelListener(org.faceless.pdf2.viewer2.DocumentPanelListener)
method.public DocumentPanelListener[] getDocumentPanelListeners()
DocumentPanelListener
objects added to this class
with the addDocumentPanelListener(org.faceless.pdf2.viewer2.DocumentPanelListener)
methodpublic static PDFViewer newPDFViewer()
public static PDFViewer newPDFViewer(Collection<ViewerFeature> features)
features
- a Collection of ViewerFeature
objects that are to be supportedViewerFeature
public static void main(String[] args)
public void setCurrentUser(String user)
currentUser
initialization parameterpublic String getCurrentUser()
setCurrentUser(java.lang.String)
public void setUnpromptedDirtyClose(boolean ignore)
unpromptedDirtyClose
initialization parameterpublic boolean getUnpromptedDirtyClose()
setUnpromptedDirtyClose(boolean)
public int getMinZoom()
public int getMaxZoom()
public void setZoomRange(int min, int max)
min
- the minimum zoom level - the default is 1max
- the minimum zoom level - the default is 9999public int[] getZoomIntervals()
public void setZoomIntervals(int[] intervals)
setZoomRange(int, int)
intervals
- the list of zoom intervalsCopyright © 2001-2017 Big Faceless Organization