Link

Select text in the Java PDF viewer

The JPedal API contains methods to allow the user to control highlights to appear on the page. This is all handled by the class TextLines which can be accessed via PdfDecoderInt.getTextLines().

Adding Highlights

Other than using the mouse to highlight text within the Viewer you can also add highlights programmatically using TextLines.addHighlights(int[][] highlights, boolean areaSelect, int page). The 3 inputs function as follows.

  • highlights - An array of integer arrays defining highlights, each containing 4 values in the order x, y, width, height
    Please note: PDF co-ordinates, which treat the bottom left of the page at origin (0,0) are used and not Java co-ordinates (where the origin is top left). You can easily see PDF co-ordinates displayed in the Java PDF viewer.
  • areaSelect - A boolean defining if we should use the highlight areas as the area to highlight (true), or the start and end point for selecting continuous text.
  • page - An integer representing the page number for the page to add the highlights to. It should be noted that highlights are removed on multiple even such as page change, mouse click, search, and other mouse interactions. When new highlights are added TextLines.hasHighlightAreasUpdated() will return true until the highlights are returned using the methods detailed below.

Getting Highlights

Highlights can be returned from one of two methods.

The integer array returned by the map and TextLines.getHighlightedAreasAs2DArray(int page) is an array of integer arrays defining highlights, each containing 4 values in the order x, y, width, height.
Please note: PDF co-ordinates, which treat the bottom left of the page at origin (0,0) are used and not Java co-ordinates (where the origin is top left). You can easily see PDF co-ordinates displayed in the Java PDF viewer.

After calling these methods TextLines.hasHighlightAreasUpdated() will start returning false.

Removing Highlights

The highlights can be only be cleared in their entirety using TextLines.clearHighlights(). This method removes all highlights, on all pages, after which TextLines.hasHighlightAreasUpdated() will return true until the highlights are returned using the methods detailed above.

Highlight Colour options

The highlighting color can be done in one of two ways. Either a semi-transparent coloured shaped is layered over the text or the colours of the highlighted area are inverted.
Both options can be set using PdfDecoder.modifyJPedalParameters(Map<Integer,Object> values) to set values for these options.

  • Set a color for the highlights using JPedalSettings.TEXT_HIGHLIGHT_COLOUR as the key in the Map with an integer representation of an RGB value (can be retrieved using Color.getRGB()).
    Color color = new Color(0, 255, 0);
    Map mapValues=new HashMap();  
    mapValues.put(JPedalSettings.TEXT_HIGHLIGHT_COLOUR, Color.getRGB());  
    PdfDecoder.modifyJPedalParameters(mapValues);  
    
  • Set the highlights to invert the page colors using JPedalSettings.INVERT_HIGHLIGHT as the key in the Map with a Boolean value to control this.
    Map mapValues=new HashMap();  
    mapValues.put(JPedalSettings.INVERT_HIGHLIGHT, Boolean.TRUE);  
    PdfDecoder.modifyJPedalParameters(mapValues);  
    

Start Your Free Trial


Customer Downloads

Select Download