title: How to extract text from a PDF file with Apache PDFBox
body: <p>I would like to extract text from a given PDF file with Apache PDFBox.</p>
  
  <p>I wrote this code:</p>
  
  <pre><code>PDFTextStripper pdfStripper = null;
  PDDocument pdDoc = null;
  COSDocument cosDoc = null;
  File file = new File(filepath);
  
  PDFParser parser = new PDFParser(new FileInputStream(file));
  parser.parse();
  cosDoc = parser.getDocument();
  pdfStripper = new PDFTextStripper();
  pdDoc = new PDDocument(cosDoc);
  pdfStripper.setStartPage(1);
  pdfStripper.setEndPage(5);
  String parsedText = pdfStripper.getText(pdDoc);
  System.out.println(parsedText);
  </code></pre>
  
  <p>However, I got the following error:</p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
  at org.apache.fontbox.afm.AFMParser.main(AFMParser.java:304)
  </code></pre>
  
  <p>I added pdfbox-1.8.5.jar and fontbox-1.8.5.jar to the class path.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I added <code>System.out.println("program starts");</code> to the beginning of the program.</p>
  
  <p>I ran it, then I got the same error as mentioned above and <code>program starts</code> did not appear in the console.</p>
  
  <p>Thus, I think I have a problem with the class path or something.</p>
  
  <p>Thank you.</p>