/**
   * Computes a series of bounding boxes (PDRectangle) from a list of TextPositions. It will create
   * a new bounding box if the vertical tolerance is exceeded
   *
   * @param positions
   * @throws IOException
   */
  public List<PDRectangle> getTextBoundingBoxes(final List<TextPosition> positions) {
    final List<PDRectangle> boundingBoxes = new ArrayList<PDRectangle>();

    float lowerLeftX = -1, lowerLeftY = -1, upperRightX = -1, upperRightY = -1;
    boolean first = true;
    for (int i = 0; i < positions.size(); i++) {
      final TextPosition position = positions.get(i);
      if (position == null) {
        continue;
      }
      final Matrix textPos = position.getTextPos();
      final float height = position.getHeight() * getHeightModifier();
      if (first) {
        lowerLeftX = textPos.getXPosition();
        upperRightX = lowerLeftX + position.getWidth();

        lowerLeftY = textPos.getYPosition();
        upperRightY = lowerLeftY + height;
        first = false;
        continue;
      }

      // we are still on the same line
      if (Math.abs(textPos.getYPosition() - lowerLeftY) <= getVerticalTolerance()) {
        upperRightX = textPos.getXPosition() + position.getWidth();
        upperRightY = textPos.getYPosition() + height;
      } else {
        final PDRectangle boundingBox =
            boundingBox(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
        boundingBoxes.add(boundingBox);

        // new line
        lowerLeftX = textPos.getXPosition();
        upperRightX = lowerLeftX + position.getWidth();

        lowerLeftY = textPos.getYPosition();
        upperRightY = lowerLeftY + height;
      }
    }
    if (!(lowerLeftX == -1 && lowerLeftY == -1 && upperRightX == -1 && upperRightY == -1)) {
      final PDRectangle boundingBox = boundingBox(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
      boundingBoxes.add(boundingBox);
    }
    return boundingBoxes;
  }