title: Using PDFbox to determine the coordinates of words in a document
body: <p>I'm using PDFbox to extract the coordinates of words/strings in a PDF document, and have so far had success determining the position of individual characters. this is the code thus far, from the PDFbox doc:</p>
  
  <pre><code>package printtextlocations;
  
  import java.io.*;
  import org.apache.pdfbox.exceptions.InvalidPasswordException;
  
  import org.apache.pdfbox.pdmodel.PDDocument;
  import org.apache.pdfbox.pdmodel.PDPage;
  import org.apache.pdfbox.pdmodel.common.PDStream;
  import org.apache.pdfbox.util.PDFTextStripper;
  import org.apache.pdfbox.util.TextPosition;
  
  import java.io.IOException;
  import java.util.List;
  
  public class PrintTextLocations extends PDFTextStripper {
  
      public PrintTextLocations() throws IOException {
          super.setSortByPosition(true);
      }
  
      public static void main(String[] args) throws Exception {
  
          PDDocument document = null;
          try {
              File input = new File("C:\\path\\to\\PDF.pdf");
              document = PDDocument.load(input);
              if (document.isEncrypted()) {
                  try {
                      document.decrypt("");
                  } catch (InvalidPasswordException e) {
                      System.err.println("Error: Document is encrypted with a password.");
                      System.exit(1);
                  }
              }
              PrintTextLocations printer = new PrintTextLocations();
              List allPages = document.getDocumentCatalog().getAllPages();
              for (int i = 0; i &lt; allPages.size(); i++) {
                  PDPage page = (PDPage) allPages.get(i);
                  System.out.println("Processing page: " + i);
                  PDStream contents = page.getContents();
                  if (contents != null) {
                      printer.processStream(page, page.findResources(), page.getContents().getStream());
                  }
              }
          } finally {
              if (document != null) {
                  document.close();
              }
          }
      }
  
      /**
       * @param text The text to be processed
       */
      @Override /* this is questionable, not sure if needed... */
      protected void processTextPosition(TextPosition text) {
          System.out.println("String[" + text.getXDirAdj() + ","
                  + text.getYDirAdj() + " fs=" + text.getFontSize() + " xscale="
                  + text.getXScale() + " height=" + text.getHeightDir() + " space="
                  + text.getWidthOfSpace() + " width="
                  + text.getWidthDirAdj() + "]" + text.getCharacter());
      }
  }
  </code></pre>
  
  <p>This produces a series of lines containing the position of each character, including spaces, that looks like this:</p>
  
  <pre><code>String[202.5604,41.880127 fs=1.0 xscale=13.98 height=9.68814 space=3.8864403 width=9.324661]P
  </code></pre>
  
  <p>Where 'P' is the character. I have not been able to find a function in PDFbox to find words, and I am not familiar enough with Java to be able to accurately concatenate these characters back into words to search through even though the spaces are also included. Has anyone else been in a similar situation, and if so how did you approach it? I really only need the coordinate of the first character in the word so that parts simplified, but as to how I'm going to match a string against that kind of output is beyond me.</p>