Class TextHighlighter
- java.lang.Object
-
- org.faceless.pdf2.viewer2.ViewerFeature
-
- 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 thesetPattern(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 theTextTool
to highlight text. Although bothTextTool
and this class can be used to highlight text matching a regular expression, this class does not allow you to copy the selected text to the clipboard. This difference means the selection can be done page by page, whereas TextTool has to extract all the text from the document first.Given the non-interactive nature of this class 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<ViewerFeature> 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 following initialization parameters can be specified to configure this feature.highlightColor A 32-bit color value, eg 0x80FF0000 (for translucent red) highlightType One of block
,underline
,outline
,doubleunderline
,strikeout
ordoublestrikeout
highlightMargin A floating point value >= 0 highlightMargin A floating point value >= 0 Here's an example showing how to set those attributes:
java -Dorg.faceless.pdf2.viewer2.TextHighlighter.word0=apples \ -Dorg.faceless.pdf2.viewer2.TextHighlighter.word1=oranges \ -jar bfopdf.jar
The name of this feature is TextHighlighterThis 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
-
-
Constructor Summary
Constructors Constructor Description TextHighlighter()
Create a new TextHighlighter
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addWord(String word)
Add a word to highlight to this TextHighlighter.void
documentUpdated(DocumentPanelEvent event)
Called when anDocumentPanelEvent
is raisedvoid
initialize(PDFViewer viewer)
Called when the feature is first added to a viewervoid
pageUpdated(PagePanelEvent event)
Called when aPagePanelEvent
is raisedvoid
setHighlightType(int type, Paint paint, 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
getAllEnabledFeatures, getAllFeatures, getCustomJavaScript, getFeatureProperty, getFeatureURLProperty, getName, isEnabledByDefault, setFeatureName, teardown, toString
-
-
-
-
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 theaddWord(java.lang.String)
method to be ignored - you should call one or the other, not both.- Parameters:
pattern
- the Pattern to match, ornull
to match whataver words have been added viaaddWord(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 callsetPattern(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 classViewerFeature
-
documentUpdated
public void documentUpdated(DocumentPanelEvent event)
Description copied from interface:DocumentPanelListener
Called when anDocumentPanelEvent
is raised- Specified by:
documentUpdated
in interfaceDocumentPanelListener
-
setHighlightType
public void setHighlightType(int type, Paint paint, Stroke stroke, float margin)
Set the type of Highlight to use.- Parameters:
type
- one ofTextTool.TYPE_BLOCK
,TextTool.TYPE_OUTLINE
,TextTool.TYPE_UNDERLINE
,TextTool.TYPE_DOUBLEUNDERLINE
,TextTool.TYPE_STRIKEOUT
orTextTool.TYPE_DOUBLESTRIKEOUT
paint
- the paint to use - must not be null. ForTextTool.TYPE_BLOCK
highlights this color will typically be translucent.stroke
- the stroke to use for outline or underlining, ornull
to choose automatically. Not used withTextTool.TYPE_BLOCK
.margin
- how many points around the text to use as a margin.
-
pageUpdated
public void pageUpdated(PagePanelEvent event)
Description copied from interface:PagePanelListener
Called when aPagePanelEvent
is raised- Specified by:
pageUpdated
in interfacePagePanelListener
-
-