Index: src/main/java/org/apache/pdfbox/util/TextPositionComparator.java
===================================================================
--- src/main/java/org/apache/pdfbox/util/TextPositionComparator.java	(revision 718405)
+++ src/main/java/org/apache/pdfbox/util/TextPositionComparator.java	(working copy)
@@ -18,29 +18,18 @@
 
 import java.util.Comparator;
 
-import org.apache.pdfbox.pdmodel.PDPage;
-
 /**
- * This class is a comparator for TextPosition operators.
+ * This class is a comparator for TextPosition operators.  It handles
+ * pages with text in different directions by grouping the text based
+ * on direction and sorting in that direction. This allows continuous text
+ * in a given direction to be more easily grouped together.  
  *
  * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
  * @version $Revision: 1.7 $
  */
 public class TextPositionComparator implements Comparator
 {
-    private PDPage thePage = null;
-
     /**
-     * Constuctor, comparison of TextPosition depends on the rotation
-     * of the page.
-     * @param page The page that the text position is on.
-     */
-    public TextPositionComparator( PDPage page )
-    {
-        thePage = page;
-    }
-
-    /**
      * {@inheritDoc}
      */
     public int compare(Object o1, Object o2)
@@ -48,42 +37,23 @@
         int retval = 0;
         TextPosition pos1 = (TextPosition)o1;
         TextPosition pos2 = (TextPosition)o2;
-        int rotation = thePage.findRotation();
-        float x1 = 0;
-        float x2 = 0;
-        float pos1YBottom = 0;
-        float pos2YBottom = 0;
-        if( rotation == 0 )
-        {
-            x1 = pos1.getX();
-            x2 = pos2.getX();
-            pos1YBottom = pos1.getY();
-            pos2YBottom = pos2.getY();
-        }
-        else if( rotation == 90 )
-        {
-            x1 = pos1.getY();
-            x2 = pos2.getX();
-            pos1YBottom = pos1.getX();
-            pos2YBottom = pos2.getY();
-        }
-        else if( rotation == 180 )
-        {
-            x1 = -pos1.getX();
-            x2 = -pos2.getX();
-            pos1YBottom = -pos1.getY();
-            pos2YBottom = -pos2.getY();
-        }
-        else if( rotation == 270 )
-        {
-            x1 = -pos1.getY();
-            x2 = -pos2.getY();
-            pos1YBottom = -pos1.getX();
-            pos2YBottom = -pos2.getX();
-        }
-        float pos1YTop = pos1YBottom - pos1.getHeight();
-        float pos2YTop = pos2YBottom - pos2.getHeight();
 
+        /* Only compare text that is in the same direction. */
+        if (pos1.getDir() < pos2.getDir())
+        	return -1;
+        else if (pos1.getDir() > pos2.getDir())
+        	return 1;
+        	
+        // Get the text direction adjusted coordinates
+        float x1 = pos1.getXDirAdj();
+        float x2 = pos2.getXDirAdj();
+        
+        float pos1YBottom = pos1.getYDirAdj();
+        float pos2YBottom = pos2.getYDirAdj();
+        // note that the coordinates have been adjusted so 0,0 is in upper left
+        float pos1YTop = pos1YBottom - pos1.getHeightDir();
+        float pos2YTop = pos2YBottom - pos2.getHeightDir();
+
         float yDifference = Math.abs( pos1YBottom-pos2YBottom);
         //we will do a simple tolerance comparison.
         if( yDifference < .1 ||
@@ -111,8 +81,6 @@
         {
             return 1;
         }
-
         return retval;
     }
-
 }
