Strategies

Overview

To offer as much flexibility as possible the extraction process is not build into the main class but outsourced into individual strategy classes. Strategy instances have to be passed to the main class via the setStrategy() method. 

The different strategies will allow you to control the detail level of the extracted data (currently only text). The result of the default strategy is only plain text while other strategy results include details about positions of their individual result type.

Currently there are 4 strategies available:

Result Types

Each extraction strategy returns an individual result type. Currently there are two result types: string and SetaPDF_Extractor_Result_Segment. While the string is a standard PHP data type the Segment result is something special:

The Segment is a kind of container or collection for individual results and implements beside several PHP interfaces (Itarator, ArrayAccess and Countable) also the SetaPDF_Extractor_Result_HasBoundsInterface interface. This interface allows you to get the outer most bounding box of all items in the result: 

Description
public SetaPDF_Extractor_Result_Bounds[] SetaPDF_Extractor_Result_Segment::getBounds ( void )

Get the outer most bounds of all items in this segment.

This method will only return values of non-rotated items.

An instance of SetaPDF_Extractor_Result_Segment will be returned by the Glyph and Word strategies. 

Encoding

By default all strategies will return resolved text and/or characters in UTF-8 encoding.

A string result can be converted to another encoding by using the Encoding class of the Core component:

PHP
$result = SetaPDF_Core_Encoding::convert($result, 'UTF-8', 'UTF-16BE');

Object result types like Words or Glyphs will allow you to get their string value in a specific encoding by passing the encoding as an argument to the getString() method: 

PHP
$string = $word->getString('UTF-16BE');

Sorters

The SetaPDF-Extractor component extracts text based on its rendered position on a PDF page and not on it's position/definition in a pages content stream. To sort the individual items (glyphs, words, fragments) all available strategies make use of Sorter classes.

All sorters will group all items into rotation groups (defined by their rotation value). That means that a rotated item will actually never be part of a non-rotated item or one with another rotation value. 

By default a strategy uses the Baseline sorter class. This class sorts all text items by their baseline value. It will identify an item to be on a new/other line if the difference of their baseline value is higher than 0.7pt.

The other sorter class is the FlexLine sorter class. It will try to estimate if two items are part of the same line by taking things like height and font-size into consideration. It will e.g. allow you to keep sub- or superscripts on a line.

A sorter instance can be passed to a strategy through the setSorter() method: 

PHP
$sorter = new SetaPDF_Extractor_Sorter_FlexLine();
$strategy->setSorter($sorter);