title: Apache PdfBox: Confusion about coordinates
body: <p>I try to extract some text out of a PDF. For that I need to define a rectangle that contains the text.</p>
  
  <p>I recognized that the coordinates may have a different meaning when I compare the coordinates from extraction of text to coordinates of drawing.</p>
  
  <pre><code>package MyTest.MyTest;
  
  import org.apache.pdfbox.pdmodel.*;
  import org.apache.pdfbox.pdmodel.PDPageContentStream.*;
  import org.apache.pdfbox.text.*;
  import java.awt.*;
  import java.io.*;
  
  public class MyTest 
  {   
    public static void main (String [] args) throws Exception
    { 
      PDDocument pd = PDDocument.load (new File ("my.pdf"));  
      PDFTextStripperByArea st = new PDFTextStripperByArea ();
      PDPage pg = pd.getPage (0);
  
      float h = pg.getMediaBox ().getHeight ();
      float w = pg.getMediaBox ().getWidth ();
      System.out.println (h + " x " + w + " in internal units");
      h = h / 72 * 2.54f * 10;
      w = w / 72 * 2.54f * 10;
      System.out.println (h + " x " + w + " in mm");
  
  
  
      int X = 85;
      int Y = 175;
      int dX = 250;
      int dY = 15;
  
      // extract some text
      st.addRegion ("a", new Rectangle (X, Y, dX, dY));
      st.extractRegions (pg);
      String text = st.getTextForRegion ("a");
      System.out.println("text="+text);
  
  
      // fill a rectangle
      PDPageContentStream contents = new PDPageContentStream (pd, pg,AppendMode.APPEND, false);
      contents.setNonStrokingColor (Color.RED);  
      contents.addRect (X, Y, dX, dY);
      contents.fill ();
      contents.close ();
      pd.save ("x.pdf");
    }
  }
  </code></pre>
  
  <p>The text I extract (output of text= in the console) is not the text I overdraw with my red rectangle (generated x.pdf).</p>
  
  <p>Why??</p>
  
  <p>For testing try some PDF you already have. To avoid a lot of try/error in aiming for a rectangle with text in it use a file with a lot of text.</p>