title: Efficiently determining the number of pages in a large pdf using pdfbox 2.x
body: <p>Is there an efficient way to get the number of pages in a pdf using pdfbox 2.x?  Currently I execute the shell command pdfinfo from our java web app to get this information.  When I do the same thing with pdfbox, the code below gets me the correct number of pages, however it is much slower with large files than pdfinfo.  A 670 MB pdf file takes 270 ms in pdfinfo, and 7300 ms in pdfbox.</p>
  
  <pre><code>public static void main(String[] args) {
  
      Date startDate = new Date();
      PDDocument document = null;
      try (FileInputStream in = new FileInputStream("D:\\pdftest\\test.pdf")){
          MemoryUsageSetting memoryUsageSetting = MemoryUsageSetting.setupMixed(1024*1024*500);
          document = PDDocument.load(in, memoryUsageSetting);
          System.out.println(String.format("number of pages: %d", document.getNumberOfPages()));
  
      } catch (IOException e) {
          System.out.println(e.getMessage());
      }
      finally {
          if (document != null) {
              try {
                  document.close();
              } catch (IOException e) {
                  System.out.println("error closing the pdf file.");
              }
          }
      }
  
      Date endDate = new Date();
  
      System.out.println(endDate.getTime() - startDate.getTime());
  }
  </code></pre>