code: @SuppressWarnings("unchecked")
      public ArrayList<ArrayList<TextPiece>> getTextPiecesByPage(File pdfFile) {
          assert (pdfFile.exists()) : "pdfFile does not exist";
          m_wordsByPage = new ArrayList<ArrayList<TextPiece>>();
  
          PDDocument document = null;
          try {
              document = PDDocument.load(pdfFile);
  
              resetEngine();
              m_currentPageNo = 0;
  
              // No support for encrypted file in the current version
              if (document.isEncrypted()) {
                  // TODO: log
              } else {
                  ArrayList<PDPage> pages = (ArrayList<PDPage>) document
                          .getDocumentCatalog().getAllPages();
  
                  for (PDPage page : pages) {
                      m_currentPageNo++;
                      PDStream contentStream = page.getContents();
                      if (contentStream != null) {
                          COSStream contents = contentStream.getStream();
                          processPage(page, contents);
                      }
                  }
  
              }
          } catch (Exception e) {
              System.out.printf("[Error] Fail to extract words\n");
              m_wordsByPage = null;
          } catch (OutOfMemoryError e) {
              System.out.printf("[Error] Out Of Memory\n");
              m_wordsByPage = null;
          } finally {
              if (document != null) {
                  try {
                      document.close();
                  } catch (IOException e) {
                      // ignore
                  }
              }
          }
  
          return m_wordsByPage;
      }
docs: Get text pieces from a PDF document
  
  @param pdfFile
  input PDF file
  @return lists of text pieces (one list per page)
  @throws IOException