private void setFirstParagraph ( ParsedPage pp ) { int nr = pp . nrOfParagraphs ( ) ; // the paragraph with the lowest number, must not be the first, maybe it // is only an Image... for ( int i = 0 ; i < nr ; i ++ ) { Paragraph p = pp . getParagraph ( i ) ; // get the Text from the paragraph SpanManager ptext = new SpanManager ( p . getText ( ) ) ; List < Span > delete = new ArrayList < Span > ( ) ; ptext . manageList ( delete ) ; // getting the spans to remove from the text, for templates List < Template > tl = p . getTemplates ( ) ; for ( int j = tl . size ( ) - 1 ; j >= 0 ; j -- ) { delete . add ( tl . get ( j ) . getPos ( ) ) ; } // getting the spans to remove from the text, for Tags List < Span > sl = p . getFormatSpans ( FormatType . TAG ) ; for ( int j = sl . size ( ) - 1 ; j >= 0 ; j -- ) { delete . add ( sl . get ( j ) ) ; } // getting the spans to remove from the text, for image text if ( showImageText ) { List < Link > ll = p . getLinks ( Link . type . IMAGE ) ; for ( int j = ll . size ( ) - 1 ; j >= 0 ; j -- ) { delete . add ( ll . get ( j ) . getPos ( ) ) ; } } // delete the spans in reverse order, the spans are managed, so // there is no need to sort them for ( int j = delete . size ( ) - 1 ; j >= 0 ; j -- ) { ptext . delete ( delete . remove ( j ) ) ; } // removing line separators if exist, so the result can be trimmed // in the next step int pos = ptext . indexOf ( lineSeparator ) ; while ( pos != - 1 ) { ptext . delete ( pos , pos + lineSeparator . length ( ) ) ; pos = ptext . indexOf ( lineSeparator ) ; } // if the result is not an empty string, we got the number of the // first paragraph if ( ! ptext . toString ( ) . trim ( ) . equals ( "" ) ) { pp . setFirstParagraphNr ( i ) ; return ; } } }
Algorithm to identify the first paragraph of a ParsedPage