title: Text coordinates when stripping from PDFBox
body: <p>i'm trying to extract text with coordinates from a pdf file using PDFBox.</p>
  
  <p>I mixed some methods/info found on internet (stackoverflow too), but the problem i have the coordinates doesnt'seems to be right. When i try to use coordinates for drawing a rectangle on top of tex, for example, the rect is painted elsewhere.</p>
  
  <p>This is my code (please don't judge the style, was written very fast just to test)</p>
  
  <p><strong>TextLine.java</strong></p>
  
  <pre><code>    import java.util.List;
      import org.apache.pdfbox.text.TextPosition;
  
      /**
       *
       * @author samue
       */
      public class TextLine {
          public List&lt;TextPosition&gt; textPositions = null;
          public String text = "";
      }
  </code></pre>
  
  <p><strong>myStripper.java</strong></p>
  
  <pre><code>    import java.io.IOException;
      import java.util.ArrayList;
      import java.util.List;
      import org.apache.pdfbox.pdmodel.PDDocument;
      import org.apache.pdfbox.pdmodel.PDPage;
      import org.apache.pdfbox.text.PDFTextStripper;
      import org.apache.pdfbox.text.TextPosition;
  
      /*
       * To change this license header, choose License Headers in Project Properties.
       * To change this template file, choose Tools | Templates
       * and open the template in the editor.
       */
  
      /**
       *
       * @author samue
       */
      public class myStripper extends PDFTextStripper {
          public myStripper() throws IOException
          {
          }
  
          @Override
          protected void startPage(PDPage page) throws IOException
          {
              startOfLine = true;
              super.startPage(page);
          }
  
          @Override
          protected void writeLineSeparator() throws IOException
          {
              startOfLine = true;
              super.writeLineSeparator();
          }
  
          @Override
          public String getText(PDDocument doc) throws IOException
          {
              lines = new ArrayList&lt;TextLine&gt;();
              return super.getText(doc);
          }
  
          @Override
          protected void writeWordSeparator() throws IOException
          {
              TextLine tmpline = null;
  
              tmpline = lines.get(lines.size() - 1);
              tmpline.text += getWordSeparator();
  
              super.writeWordSeparator();
          }
  
  
          @Override
          protected void writeString(String text, List&lt;TextPosition&gt; textPositions) throws IOException
          {
              TextLine tmpline = null;
  
              if (startOfLine) {
                  tmpline = new TextLine();
                  tmpline.text = text;
                  tmpline.textPositions = textPositions;
                  lines.add(tmpline);
              } else {
                  tmpline = lines.get(lines.size() - 1);
                  tmpline.text += text;
                  tmpline.textPositions.addAll(textPositions);
              }
  
              if (startOfLine)
              {
                  startOfLine = false;
              }
              super.writeString(text, textPositions);
          }
  
          boolean startOfLine = true;
          public ArrayList&lt;TextLine&gt; lines = null;
  
      }
  </code></pre>
  
  <p><strong>click event on AWT button</strong></p>
  
  <pre><code> private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
      // TODO add your handling code here:
      try {
          File file = new File("C:\\Users\\samue\\Desktop\\mwb_I_201711.pdf");
          PDDocument doc = PDDocument.load(file);
  
          myStripper stripper = new myStripper();
  
          stripper.setStartPage(1); // fix it to first page just to test it
          stripper.setEndPage(1);
          stripper.getText(doc);
  
          TextLine line = stripper.lines.get(1); // the line i want to paint on
  
          float minx = -1;
          float maxx = -1;
  
          for (TextPosition pos: line.textPositions)
          {
              if (pos == null)
                  continue;
  
              if (minx == -1 || pos.getTextMatrix().getTranslateX() &lt; minx) {
                  minx = pos.getTextMatrix().getTranslateX();
              }
              if (maxx == -1 || pos.getTextMatrix().getTranslateX() &gt; maxx) {
                  maxx = pos.getTextMatrix().getTranslateX();
              }
          }
  
          TextPosition firstPosition = line.textPositions.get(0);
          TextPosition lastPosition = line.textPositions.get(line.textPositions.size() - 1);
  
          float x = minx;
          float y = firstPosition.getTextMatrix().getTranslateY();
          float w = (maxx - minx) + lastPosition.getWidth();
          float h = lastPosition.getHeightDir();
  
          PDPageContentStream contentStream = new PDPageContentStream(doc, doc.getPage(0), PDPageContentStream.AppendMode.APPEND, false);
  
          contentStream.setNonStrokingColor(Color.RED);
          contentStream.addRect(x, y, w, h);
          contentStream.fill();
          contentStream.close();
  
          File fileout = new File("C:\\Users\\samue\\Desktop\\pdfbox.pdf");
          doc.save(fileout);
          doc.close();
      } catch (Exception ex) {
  
      }
  }                                     
  </code></pre>
  
  <p>any suggestion? what am i doing wrong?</p>