title: Why do I get this error "java.io.IOException: Error: Header doesn't contain versioninfo" when using PDFBox?
body: <p>This is my first time posting here so sorry if I didn't do it correctly. I am trying to write a program that finds certain patterns in a folder of pdfs and then prints them as a list using PDFBox. Currently, it will find pattern matches in some of the files, but not all of them, even though I checked and there should be one in each file. It also returns an error before printing that says "java.io.IOException: Error: Header doesn't contain versioninfo". I am wondering if this is the reason that it doesn't return some of the pattern matches? And if it is, how do I fix it? The whole try/catch/finally part was kind of confusing to me, so perhaps it has to do with that. (I removed some code that isn't necessary for the question)</p>
  
  <pre><code>public class PDF
  {
      public static void main(String[] args) throws IOException
  
      {
          String number;
          String name;
          String state;
          String numberR =  "\\d{3}([.-])?\\d{3}\\1?\\d{4}|\\(\\d{3}\\)([.-])?\\d{3}\\2?\\d{4}|\\(\\d{3}\\)\\s\\d{3}([.-])?\\d{4}";
          String stateR = "[,]\\sAL\\s|[,]\\sMT\\s|[,]\\sAK\\s|[,]\\sNE\\s|[,]\\sAZ\\s|[,]\\sNV\\s|[,]\\sAR\\s|[,]\\sNH\\s|[,]\\sCA\\s|[,]\\sNJ\\s|[,]\\sCO\\s|[,]\\sNM\\s|[,]\\sCT\\s|[,]\\sNY\\s|[,]\\sDE\\s|[,]\\sNC\\s|[,]\\sFL\\s|[,]\\sND\\s|[,]\\sGA\\s|[,]\\sOH\\s|[,]\\sHI\\s|[,]\\sOK\\s|[,]\\sID\\s|[,]\\sOR\\s|[,]\\sIL\\s|[,]\\sPA\\s|[,]\\sIN\\s|[,]\\sRI\\s|[,]\\sIA\\s|[,]\\sSC\\s|[,]\\sKS\\s|[,]\\sSD\\s|[,]\\sKY\\s|[,]\\sTN\\s|[,]\\sLA\\s|[,]\\sTX\\s|[,]\\sME\\s|[,]\\sUT\\s|[,]\\sMD\\s|[,]\\sVT\\s|[,]\\sMA\\s|[,]\\sVA\\s|[,]\\sMI\\s|[,]\\sWA\\s|[,]\\sMN\\s|[,]\\sWV\\s|[,]\\sMS\\s|[,]\\sWI\\s|[,]\\sMO\\s|[,]\\sWY\\s|s";
          File folder = new File("/Users/bob/Desktop/");
          File[] listOfFiles = folder.listFiles();
          for (File file : listOfFiles) 
          {
              if (file.isFile())
              {
                  name = file.getName();
                  state = findInfo(stateR, name);
                  number = findInfo(numberR, name);
                  System.out.print(name + "       " + number + "     " + state.substring(1));
                  System.out.println();
              }
          }
      }
  
      public static String findInfo(String regex,String name) throws IOException
      {
          File input = new File("/Users/bob/Desktop/" + name);
          PDDocument pd = null;
          try 
          {
              pd = PDDocument.load(input);
              StringBuilder sb = new StringBuilder();
              PDFTextStripper stripper = new PDFTextStripper();
              stripper.setStartPage(1);
              sb.append(stripper.getText(pd));
              Pattern p = Pattern.compile(regex);
              Matcher m = p.matcher(sb);
  
              while (m.find())
              {
                  return m.group();
              }
  
          } 
          catch (Exception e)
          {
              e.printStackTrace();
              return " ";
          }
          finally
          {   
              if (pd != null)
              {
                  pd.close();
              }
          }
          return " ";
       }      
  }
  </code></pre>
  
  <p>Edit: Perhaps it has to do with how PDFBox is reading the files? I tried printing out one of the files that didn't work and got this:</p>
  
  <p>"</p>
  
  <pre><code>         !"#    $%&amp;'(! (
  </code></pre>
  
  <p>)            *$</p>
  
  <pre><code>    +   )
  ,-
  </code></pre>
  
  <p>."/!01%%23/4$56667
  "
          $$4$% 8 %85 % $8    -   %
  ,(# 58"89$"
  ...</p>