org.faceless.pdf2.viewer2.feature
Class TextHighlighter

java.lang.Object
  extended by org.faceless.pdf2.viewer2.ViewerFeature
      extended by org.faceless.pdf2.viewer2.feature.TextHighlighter
All Implemented Interfaces:
DocumentPanelListener, PagePanelListener

public class TextHighlighter
extends ViewerFeature
implements DocumentPanelListener, PagePanelListener

A feature that allows the highlighting of text in the viewer. This takes a fairly simplistic approach, allowing words to be matched manually via the addWord(java.lang.String) method, or to a regular expression via the setPattern(java.util.regex.Pattern) method. A highlight will then be applied whenever a page containing that word is displayed.

The SearchPanel has similar functionality but takes a more integrated approach, using the TextTool to highlight text, so it's likely that this class will be used in a more standalone environment. Here's an example of how to do this:

   Pattern pattern = Pattern.compile("(apples|oranges|[a-z]*berries)");
   TextHighlighter hl = new TextHighlighter();
   hl.setPattern(pattern);
   final Collection features = new ArrayList(ViewerFeature.getAllFeatures());
   features.add(hl);
   SwingUtilities.invokeLater(new Runnable() {
     void run() {
       PDFViewer viewer = PDFViewer.newPDFViewer(features);
       viewer.loadPDF(new File("CropReport.pdf"));
     }
   });
 }
 
The name of this feature is "TextHighlighter".

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.

Since:
2.8.1
See Also:
SearchPanel, SearchField, TextTool

Field Summary
static int TYPE_FILL
          A parameter to setHighlightType() that will cause the highlight to be filled.
static int TYPE_OUTLINE
          A parameter to setHighlightType() that will cause the highlight to be outlined.
static int TYPE_UNDERLINE
          A parameter to setHighlightType() that will cause the highlight to be outlined.
 
Constructor Summary
TextHighlighter()
          Create a new TextHighlighter
 
Method Summary
 void addWord(String word)
          Add a word to highlight to this TextHighlighter.
 void documentUpdated(DocumentPanelEvent event)
          Called when an DocumentPanelEvent is raised
 void drawHighlight(Graphics2D g, int width, int height)
          Draw the highlight.
 void initialize(PDFViewer viewer)
          Called when the feature is first added to a viewer
 void pageUpdated(PagePanelEvent event)
          Called when a PagePanelEvent is raised
 void setHighlightType(int type, Paint color, Stroke stroke, float margin)
          Set the type of Highlight to use
 void setPattern(Pattern pattern)
          Set the Regular Expression used to determine which words to highlight.
 
Methods inherited from class org.faceless.pdf2.viewer2.ViewerFeature
getAllFeatures, getCustomJavaScript, getFeatureProperty, getName, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

TYPE_FILL

public static final int TYPE_FILL
A parameter to setHighlightType() that will cause the highlight to be filled. An example would be setHighlightType(TYPE_FILL, new Color(0x70FFFF00, true), null, 0), to highlight the text with a translucent yellow (the default).

See Also:
Constant Field Values

TYPE_OUTLINE

public static final int TYPE_OUTLINE
A parameter to setHighlightType() that will cause the highlight to be outlined. An example would be setHighlightType(TYPE_OUTLINE, Color.red, new BasicStroke(), 2) to draw a solid red outline around the text with a 2 point margin

See Also:
Constant Field Values

TYPE_UNDERLINE

public static final int TYPE_UNDERLINE
A parameter to setHighlightType() that will cause the highlight to be outlined. An example would be setHighlightType(TYPE_UNDERLINE, Color.red, new BasicStroke(), 2) to draw a solid red underline two points below the text.

See Also:
Constant Field Values
Constructor Detail

TextHighlighter

public TextHighlighter()
Create a new TextHighlighter

Method Detail

setPattern

public void setPattern(Pattern pattern)
Set the Regular Expression used to determine which words to highlight. Calling this method will cause any words added by the addWord(java.lang.String) method to be ignored - you should call one or the other, not both.

Parameters:
pattern - the Pattern to match, or null to match whataver words have been added via addWord(java.lang.String)
Since:
2.11

addWord

public void addWord(String word)
Add a word to highlight to this TextHighlighter. For more than a couple of words it's likely to be more efficient to call setPattern(java.util.regex.Pattern), and this is the approach we recommend for new implementations.

Parameters:
word - the new word to highlight if found

initialize

public void initialize(PDFViewer viewer)
Description copied from class: ViewerFeature
Called when the feature is first added to a viewer

Overrides:
initialize in class ViewerFeature

documentUpdated

public void documentUpdated(DocumentPanelEvent event)
Description copied from interface: DocumentPanelListener
Called when an DocumentPanelEvent is raised

Specified by:
documentUpdated in interface DocumentPanelListener

setHighlightType

public void setHighlightType(int type,
                             Paint color,
                             Stroke stroke,
                             float margin)
Set the type of Highlight to use

Parameters:
type - one of TYPE_FILL, TYPE_OUTLINE or TYPE_UNDERLINE
color - the color to use - must not be null
stroke - the stroke to use for outline or underlining. May be null.
margin - how many points around the text to use as a margin

drawHighlight

public void drawHighlight(Graphics2D g,
                          int width,
                          int height)
Draw the highlight. This method may be overridden by subclasses to highlight the text in a different way

Parameters:
g - the Graphics object to paint to
width - the width of the object
height - the height of the object

pageUpdated

public void pageUpdated(PagePanelEvent event)
Description copied from interface: PagePanelListener
Called when a PagePanelEvent is raised

Specified by:
pageUpdated in interface PagePanelListener


Copyright © 2001-2008 Big Faceless Organization