Breaking a string into substrings I wrote some code which divides a line through the words of the text so that each substring is no longer than MaxWidth. It works well, but it's very slow.

Pattern pattern = Pattern.compile("(.{1," + symbols + "}(\\b|\\s))"); // symbols - MaxWidth
Pattern pattern2 = Pattern.compile("\\s.*");

while ((line = in.readLine()) != null) {  // reading file by lines                  
    String dopLine = "";
    if (pattern2.matcher(line).matches()) {
        // If a line begins with a space, it is the beginning of a paragraph and need to add \ n
        if(!tempDopLine.equals("")) { // tempDopLine - Some part of the previous line, which is not full screen
            tempStringBuffer.append(tempDopLine);
            tempStringBuffer.append("\n");
            lineCounter++;
            if (lineCounter == lines) {
                addPage(tempStringBuffer); // create page
                tempStringBuffer = new StringBuilder("");
                lineCounter = 0;
                numberOfPages++;
            }

        }
        tempDopLine = "";
        dopLine = line;
    } else {   
        dopLine = tempDopLine + " " + line; // if this line
    }
    Matcher matcher = pattern.matcher(dopLine); // divide a string into a substrings

    HashMap&lt;Integer, String&gt; temp = new HashMap&lt;Integer, String&gt;();
    int i = 0;
    while (matcher.find()) {
        temp.put(i, matcher.group());
        i++;
    }
    for(i = 0; i &lt; temp.size(); i++) {
        if (i&lt;(temp.size()-1)) {
            String tempL = temp.get(i);

            tempStringBuffer.append(tempL);

            tempStringBuffer.append("\n");
            lineCounter++;
            if (lineCounter == lines) {
                addPage(tempStringBuffer);
                tempStringBuffer = new StringBuilder("");
                lineCounter = 0;
                numberOfPages++;
            }

        } else {
            tempDopLine = temp.get(i); // The last part of the string remember to display it along with the next line
        }
    }                       
}
Few thought,

You do a while loop  ( while( matcher.find() )  to find all matchs, then you do a for loop to deal with it. I think that can be done in only one loop.

in you last for loop, you could remove the first if :

   for(i = 0; i &lt; (temp.size() -1); i++) {

     String tempL = temp.get(i);

     tempStringBuffer.append(tempL);

     tempStringBuffer.append("\n");
     lineCounter++;
     if (lineCounter == lines) {
            addPage(tempStringBuffer);
            tempStringBuffer = new StringBuilder("");
            lineCounter = 0;
            numberOfPages++;
     }

   } // End for loop

   tempDopLine = temp.get(temp.size()-1); 

}       


Other suggestion  the HashMap temp  got be only a array, because right now your code do a lot of autoboxing  ( from int to Integer) add and to retrieve information from your HashMap.

Edit: Here a quick example to merge your two loops:

String tempL = matcher.group();
while (matcher.find()) {

   tempStringBuffer.append(tempL);

   tempStringBuffer.append("\n");
   lineCounter++;
   if (lineCounter == lines) {
       addPage(tempStringBuffer);
       tempStringBuffer = new StringBuilder("");
       lineCounter = 0;
       numberOfPages++;
    }
    tempL =  matcher.group();


} 

tempDopLine = tempL;