private ArrayList<TextPiece> combineLines(ArrayList<TextPiece> wordsOfAPage) {
	ArrayList<TextPiece> linesOfAPage = new ArrayList<TextPiece>();

	/*
	 * loops over all words
	 */
	TextPiece line = null;
	for (int i = 0; i < wordsOfAPage.size(); i++) {
	    TextPiece prevWord = (i == 0) ? null : wordsOfAPage.get(i - 1);
	    TextPiece currentWord = wordsOfAPage.get(i);
	    TextPiece nextWord = (i == wordsOfAPage.size() - 1) ? null
		    : wordsOfAPage.get(i + 1);

	    float gapToPrevWord = (prevWord == null) ? Integer.MAX_VALUE
		    : currentWord.getX() - prevWord.getX()
			    - prevWord.getWidth();
	    float gapToNextWord = (nextWord == null) ? Integer.MAX_VALUE
		    : nextWord.getX() - currentWord.getX()
			    - currentWord.getWidth();

	    /*
	     * Decides whether this word is the beginning of a new line
	     */
	    boolean isNewLine;
	    if (prevWord != null
		    && (Math.abs(currentWord.getY() - prevWord.getY()) <= Math
			    .max(prevWord.getYScale(), prevWord.getFontSize()))
		    && (currentWord.getX() > prevWord.getX())
		    && ((gapToPrevWord < m_docInfo.getAverageCharWidth() * 1.5) || (gapToPrevWord < 5.0f))) {
		isNewLine = false;
	    } else
		isNewLine = true;

	    boolean isSpaceOrEmpty = currentWord.getText() == " "
		    || currentWord.getText() == "";
	    if (isNewLine || line == null) {
		if (isSpaceOrEmpty) {
		} // ignore empty new line
		else {
		    /*
		     * Creates a new line and add it to collection
		     */
		    line = new TextPiece();
		    linesOfAPage.add(line);
		    /*
		     * Gets info of this line
		     */
		    line.setX(currentWord.getX());
		    line.setY(currentWord.getY());
		    line.setEndX(currentWord.getX() + currentWord.getWidth());
		    line.setEndY(currentWord.getY()
			    + Math.max(currentWord.getYScale(),
				    currentWord.getFontSize()));
		    line.setXScale(currentWord.getXScale());
		    line.setWidth(currentWord.getWidth());
		    line.setHeight(Math.max(currentWord.getFontSize(),
			    currentWord.getYScale()));
		    line.setText(currentWord.getText());
		    line.setFontSize(currentWord.getFontSize());
		}
	    } else {
		/*
		 * This word is still in the same line: combines it line will
		 * not be null here, otherwise it's a bug
		 */
		if (line.getText().length() == 1
			&& currentWord.getXScale() > prevWord.getXScale()
			&& currentWord.getX() > prevWord.getX()
			&& currentWord.getY() > prevWord.getY()
			&& prevWord.getY() + prevWord.getXScale() > currentWord
				.getY()) {
		    line.setSuperScriptBeginning(true);
		}
		if (currentWord.getX() - line.getEndX() > 1.5) { // try to fix
								 // the space
								 // problem
								 // inherited
								 // from PDFBOX
		    line.setText(line.getText() + " " + currentWord.getText());
		} else
		    line.setText(line.getText() + currentWord.getText());
		line.setWidth(line.getWidth() + currentWord.getWidth());

		/*
		 * Updates the end-x
		 */
		line.setEndX(currentWord.getX() + currentWord.getWidth());

		if (currentWord.getXScale() > line.getXScale())
		    line.setXScale(currentWord.getXScale());

		line.setY(currentWord.getY());
		line.setEndY(currentWord.getY()
			+ Math.max(currentWord.getYScale(),
				currentWord.getFontSize()));
	    }
	}

	return linesOfAPage;
    }
private ArrayList < TextPiece > combineLines ( ArrayList < TextPiece > wordsOfAPage ) { ArrayList < TextPiece > linesOfAPage = new ArrayList < TextPiece > ( ) ; TextPiece line = null ; for ( int i = 0 ; i < wordsOfAPage . size ( ) ; i ++ ) { TextPiece prevWord = ( i == 0 ) ? null : wordsOfAPage . get ( i - 1 ) ; TextPiece currentWord = wordsOfAPage . get ( i ) ; TextPiece nextWord = ( i == wordsOfAPage . size ( ) - 1 ) ? null : wordsOfAPage . get ( i + 1 ) ; float gapToPrevWord = ( prevWord == null ) ? Integer . MAX_VALUE : currentWord . getX ( ) - prevWord . getX ( ) - prevWord . getWidth ( ) ; float gapToNextWord = ( nextWord == null ) ? Integer . MAX_VALUE : nextWord . getX ( ) - currentWord . getX ( ) - currentWord . getWidth ( ) ; boolean isNewLine ; if ( prevWord != null && ( Math . abs ( currentWord . getY ( ) - prevWord . getY ( ) ) <= Math . max ( prevWord . getYScale ( ) , prevWord . getFontSize ( ) ) ) && ( currentWord . getX ( ) > prevWord . getX ( ) ) && ( ( gapToPrevWord < m_docInfo . getAverageCharWidth ( ) * 1.5 ) || ( gapToPrevWord < 5.0f ) ) ) { isNewLine = false ; } else isNewLine = true ; boolean isSpaceOrEmpty = currentWord . getText ( ) == " " || currentWord . getText ( ) == "" ; if ( isNewLine || line == null ) { if ( isSpaceOrEmpty ) { } else { line = new TextPiece ( ) ; linesOfAPage . add ( line ) ; line . setX ( currentWord . getX ( ) ) ; line . setY ( currentWord . getY ( ) ) ; line . setEndX ( currentWord . getX ( ) + currentWord . getWidth ( ) ) ; line . setEndY ( currentWord . getY ( ) + Math . max ( currentWord . getYScale ( ) , currentWord . getFontSize ( ) ) ) ; line . setXScale ( currentWord . getXScale ( ) ) ; line . setWidth ( currentWord . getWidth ( ) ) ; line . setHeight ( Math . max ( currentWord . getFontSize ( ) , currentWord . getYScale ( ) ) ) ; line . setText ( currentWord . getText ( ) ) ; line . setFontSize ( currentWord . getFontSize ( ) ) ; } } else { if ( line . getText ( ) . length ( ) == 1 && currentWord . getXScale ( ) > prevWord . getXScale ( ) && currentWord . getX ( ) > prevWord . getX ( ) && currentWord . getY ( ) > prevWord . getY ( ) && prevWord . getY ( ) + prevWord . getXScale ( ) > currentWord . getY ( ) ) { line . setSuperScriptBeginning ( true ) ; } if ( currentWord . getX ( ) - line . getEndX ( ) > 1.5 ) { line . setText ( line . getText ( ) + " " + currentWord . getText ( ) ) ; } else line . setText ( line . getText ( ) + currentWord . getText ( ) ) ; line . setWidth ( line . getWidth ( ) + currentWord . getWidth ( ) ) ; line . setEndX ( currentWord . getX ( ) + currentWord . getWidth ( ) ) ; if ( currentWord . getXScale ( ) > line . getXScale ( ) ) line . setXScale ( currentWord . getXScale ( ) ) ; line . setY ( currentWord . getY ( ) ) ; line . setEndY ( currentWord . getY ( ) + Math . max ( currentWord . getYScale ( ) , currentWord . getFontSize ( ) ) ) ; } } return linesOfAPage ; }
Combines words into lines in a document page except the table area

@param wordsOfAPage
a list of words of one page
@return a list of lines of one page