If height is 0 (can happen in some documents) the variable ""int numberOfLines" will be 2147483647 (Integer.MAX_VALUE). This will resolut in adding too much empty lines.
quick dirty fix but it would be better to find out why height is sometimes 0.
if(height==0){height=1;};
before
int numberOfLines = (int) (Math.floor( textYPosition - previousTextYPosition) / height );